logging.go 5.28 KB
Newer Older
Stefan Dähling's avatar
Stefan Dähling committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Copyright 2020 Institute for Automation of Complex Power Systems,
E.ON Energy Research Center, RWTH Aachen University

This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.

Apache License, Version 2.0:

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package agency

import (
	"errors"
	"log"
	"os"
	"sync"
	"time"

	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/logger/client"
	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/schemas"
)

// Logger logs data to logging service
type Logger struct {
	agentID  int
	client   *loggerClient
	mutex    *sync.Mutex
	config   schemas.LogConfig
	logError *log.Logger
	logInfo  *log.Logger
}

// NewLog sends a new logging message to the logging service
69
func (log *Logger) NewLog(logType string, message string, data string) (err error) {
Stefan Dähling's avatar
Stefan Dähling committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	if logType != "error" && logType != "debug" && logType != "status" && logType != "msg" &&
		logType != "app" {
		err = errors.New("UnknownLogType")
		return
	}
	log.mutex.Lock()
	if (logType == "msg" && !log.config.Msg) || (logType == "app" && !log.config.App) ||
		(logType == "debug" && !log.config.Debug) || (logType == "status" && !log.config.Status) {
		log.mutex.Unlock()
		return
	}
	time.Sleep(time.Millisecond * 5)
	tStamp := time.Now()
	log.mutex.Unlock()
	msg := schemas.LogMessage{
		AgentID:        log.agentID,
		Timestamp:      tStamp,
		LogType:        logType,
		Message:        message,
		AdditionalData: data}
	log.client.logIn <- msg

	return
}

// UpdateState overrides the state stored in database
96
func (log *Logger) UpdateState(state string) (err error) {
Stefan Dähling's avatar
Stefan Dähling committed
97
98
99
100
101
102
103
104
105
106
	agState := schemas.State{
		MASID:     log.client.masID,
		AgentID:   log.agentID,
		Timestamp: time.Now(),
		State:     state}
	_, err = client.PutState(agState)
	return
}

// RestoreState loads state saved in database and return it
107
func (log *Logger) RestoreState() (state string, err error) {
Stefan Dähling's avatar
Stefan Dähling committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
	var agState schemas.State
	agState, _, err = client.GetState(log.client.masID, log.agentID)
	state = agState.State
	return
}

// storeLogs periodically requests the logging service to store log messages
func (log *loggerClient) storeLogs() (err error) {
	if log.active {
		for {
			if len(log.logIn) > 0 {
				numMsg := len(log.logIn)
				logMsgs := make([]schemas.LogMessage, numMsg, numMsg)
				for i := 0; i < numMsg; i++ {
					logMsgs[i] = <-log.logIn
					logMsgs[i].MASID = log.masID
				}
				_, err = client.PostLogs(log.masID, logMsgs)
				if err != nil {
					log.logError.Println(err)
					for i := range logMsgs {
						log.logIn <- logMsgs[i]
					}
					continue
				}
			}
			tempTime := time.Now()
			for {
				time.Sleep(100 * time.Millisecond)
				if time.Since(tempTime).Seconds() > 15 || len(log.logIn) > 50 {
					break
				}
			}
		}
	}
	return
}

// newLogger craetes a new object of type Logger
func newLogger(agentID int, client *loggerClient, config schemas.LogConfig, logErr *log.Logger,
	logInf *log.Logger) (log *Logger) {
	log = &Logger{
		agentID:  agentID,
		client:   client,
		mutex:    &sync.Mutex{},
		config:   config,
		logError: logErr,
		logInfo:  logInf,
	}
	return
}

// loggerClient is the agency client for the logger
type loggerClient struct {
	masID    int
	logIn    chan schemas.LogMessage // logging inbox
	active   bool                    // indicates if logging is active (switch via env)
	logError *log.Logger
	logInfo  *log.Logger
}

// newLoggerClient creates an agency logger client
func newLoggerClient(masID int, logErr *log.Logger, logInf *log.Logger) (log *loggerClient) {
	log = &loggerClient{
		masID:    masID,
		active:   false,
		logError: logErr,
		logInfo:  logInf,
	}
	temp := os.Getenv("CLONEMAP_LOGGING")
	if temp == "ON" {
		log.active = true
	}
	log.logIn = make(chan schemas.LogMessage, 10000)
	go log.storeLogs()
	log.logInfo.Println("Created new logger client; status: ", log.active)
	return
}