-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogtools.go
374 lines (324 loc) · 11.8 KB
/
logtools.go
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
69
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
96
97
98
99
100
101
102
103
104
105
106
107
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
===========================================================================
MIT License
Copyright (c) 2021 Manish Meganathan, Mariyam A.Ghani
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.
===========================================================================
FyrMesh gopkg tools
===========================================================================
*/
package tools
import (
"fmt"
"strings"
"time"
)
// A function that returns the current time as an ISO8601 string without the timezone.
func CurrentISOtime() string {
return time.Now().UTC().Format("2006-01-02T15:04:05")
}
// A function that deserializes a a string with a format akin
// to 'key1-value1=key2-value2..' into a map[string]string """
func Deepdeserialize(str string) map[string]string {
// Split the string into individual key-value pairs
pairs := strings.Split(str, "=")
// Define a new map[string]string oject
dict := make(map[string]string)
// Iterate over the key-value pairs
for _, pair := range pairs {
// Split each key-value pair
set := strings.Split(pair, "-")
// Add the key value pair into the map
dict[set[0]] = set[1]
}
// Return the map
return dict
}
// A struct that defines a log that is
// generated within the orchestrator.
type OrchLog struct {
Logsource string
Logtype string
Logtime string
Logmessage string
Logmetadata map[string]string
}
// A struct that defines a log that is
// sent out of the server for observation.
// The Logmessage here is a fully stringified Log.
// The Logsource and Logtype are used for filtering.
type ObserverLog struct {
Logsource string
Logtype string
Logmessage string
}
// An interface that defines a common interface that
// can be used by OrchLog and the ComplexLog proto
type Log interface {
GetLogsource() string
GetLogtype() string
GetLogtime() string
GetLogmessage() string
GetLogmetadata() map[string]string
}
// A getter function for the Logsource field of OrchLog
func (orchlog *OrchLog) GetLogsource() string {
if orchlog != nil {
return orchlog.Logsource
}
return ""
}
// A getter function for the Logtype field of OrchLog
func (orchlog *OrchLog) GetLogtype() string {
if orchlog != nil {
return orchlog.Logtype
}
return ""
}
// A getter function for the Logtime field of OrchLog
func (orchlog *OrchLog) GetLogtime() string {
if orchlog != nil {
return orchlog.Logtime
}
return ""
}
// A getter function for the Logmessage field of OrchLog
func (orchlog *OrchLog) GetLogmessage() string {
if orchlog != nil {
return orchlog.Logmessage
}
return ""
}
// A getter function for the Logmetadata field of OrchLog
func (orchlog *OrchLog) GetLogmetadata() map[string]string {
if orchlog != nil {
return orchlog.Logmetadata
}
return nil
}
// A constructor function that generates and returns an OrchLog with
// the 'serverlog' type. The message passed is set as the Logmessage.
func NewOrchServerlog(message string) *OrchLog {
// Construct a new OrchLog
orchlog := OrchLog{}
// Set the values of the OrchLog
orchlog.Logsource = "ORCH"
orchlog.Logtype = "serverlog"
orchlog.Logtime = CurrentISOtime()
orchlog.Logmessage = message
orchlog.Logmetadata = make(map[string]string)
// Return the OrchLog
return &orchlog
}
// A constructor function that generates and returns an OrchLog with the
// 'protolog' type. The message passed is set as the Logmessage and the
// server, service and err values are set in the Logmetadata map.
func NewOrchProtolog(message string, server string, service string, err error) *OrchLog {
// Construct a new OrchLog
orchlog := OrchLog{}
// Set the values of the OrchLog
orchlog.Logsource = "ORCH"
orchlog.Logtype = "protolog"
orchlog.Logtime = CurrentISOtime()
orchlog.Logmessage = message
orchlog.Logmetadata = make(map[string]string)
// Set the values of the OrchLog Metadata
orchlog.Logmetadata["server"] = server
orchlog.Logmetadata["service"] = service
orchlog.Logmetadata["error"] = fmt.Sprintf("%v", err)
// Return the OrchLog
return &orchlog
}
// A constructor function that generates and returns an OrchLog with
// the 'cloudlog' type. The message passed is set as the Logmessage.
func NewOrchCloudlog(message string) *OrchLog {
// Construct a new OrchLog
orchlog := OrchLog{}
// Set the values of the OrchLog
orchlog.Logsource = "ORCH"
orchlog.Logtype = "cloudlog"
orchlog.Logtime = CurrentISOtime()
orchlog.Logmessage = message
orchlog.Logmetadata = make(map[string]string)
// Return the OrchLog
return &orchlog
}
// A constructor function that generates and returns an OrchLog with
// the 'schedlog' type. The message passed is set as the Logmessage.
func NewOrchSchedlog(message string) *OrchLog {
// Construct a new OrchLog
orchlog := OrchLog{}
// Set the values of the OrchLog
orchlog.Logsource = "ORCH"
orchlog.Logtype = "schedlog"
orchlog.Logtime = CurrentISOtime()
orchlog.Logmessage = message
orchlog.Logmetadata = make(map[string]string)
// Return the OrchLog
return &orchlog
}
// A constructor function that generates and returns an OrchLog with
// the 'obstoggle' type. The command passed is set as the Logmessage.
func NewObsCommand(command string) *OrchLog {
// Construct a new OrchLog
orchlog := OrchLog{}
// Set the values of the OrchLog
orchlog.Logsource = "OBS"
orchlog.Logtype = "toggle"
orchlog.Logtime = CurrentISOtime()
orchlog.Logmessage = command
orchlog.Logmetadata = make(map[string]string)
// Return the OrchLog
return &orchlog
}
// A constructore function that generates and returns an ObserverLog that is
// built using an existing Log. The Logmessage is a stringified version of the
// Log object being used. The Logsource and Logtype are taken from the Log struct.
func NewObserverLog(log Log) *ObserverLog {
// Construct a new ObserverLog
obslog := ObserverLog{}
// Set the value of Logsource and Logtype
obslog.Logsource = log.GetLogsource()
obslog.Logtype = log.GetLogtype()
// Stringify and set the Logmessage
obslog.Logmessage = FormatLog(log)
// Return the ObserverLog
return &obslog
}
// A function that simplifies and formats a Log into a string.
// Every logtype has a different format but the general structure
// of the string log is - '[source][time][type] message. metadata..'
func FormatLog(log Log) string {
// Retrieve all the Log data
logsource := log.GetLogsource()
logtype := log.GetLogtype()
logtime := log.GetLogtime()
logmessage := log.GetLogmessage()
logmetadata := log.GetLogmetadata()
// Declare a string log
var strlog string
// Define the common prefix of all logs
logprefix := fmt.Sprintf("[%s][%s]%11s", logsource, logtime, logtype)
// Check the logtype and set the appropriate format
switch logtype {
case "serverlog", "cloudlog", "schedlog":
strlog = fmt.Sprintf("%v || %v |", logprefix, logmessage)
case "protolog":
strlog = fmt.Sprintf("%v || %v | server - %v | service - %v| error - %v |", logprefix, logmessage, logmetadata["server"], logmetadata["service"], logmetadata["error"])
case "message":
strlog = fmt.Sprintf("%v || (%v) %v | type - %v |", logprefix, logmetadata["format"], logmessage, logmetadata["type"])
case "meshsync":
strlog = fmt.Sprintf("%v || (meshevent) %v | event - %v |", logprefix, logmessage, logmetadata["synctype"])
case "nodesync":
strlog = fmt.Sprintf("%v || (meshevent) %v | offset - %v |", logprefix, logmessage, logmetadata["offset"])
case "handshake":
strlog = fmt.Sprintf("%v || (meshevent) %v | node - %v |", logprefix, logmessage, logmetadata["node"])
case "sensordata":
sensordata := Deepdeserialize(logmetadata["sensors"])
strlog = fmt.Sprintf("%v || (data) %v | sensors - %v | node - %v | ping - %v |", logprefix, logmessage, sensordata, logmetadata["node"], logmetadata["ping"])
case "configdata":
strlog = fmt.Sprintf("%v || (data) %v | node - %v | ping - %v |", logprefix, logmessage, logmetadata["node"], logmetadata["ping"])
case "ctrldata":
strlog = fmt.Sprintf("%v || (data) %v | node - %v |", logprefix, logmessage, logmetadata["nodeID"])
case "nodelist":
strlog = fmt.Sprintf("%v || (data) %v", logprefix, logmessage)
}
// Return the string log
return strlog
}
// A function that handles the output of the logs recieved
// over a given logqueue. Currently only prints to stdout.
func LogHandler(meshorchestrator *MeshOrchestrator) {
// Declare the observer toggle
observertoggle := false
// log the beginning of the loghandler
fmt.Println(FormatLog(NewOrchServerlog("(startup) log handler has started")))
// Iterate over the logqueue until it closes.
for log := range meshorchestrator.LogQueue {
// Check the source of the log
logtype := log.GetLogtype()
switch logtype {
case "serverlog", "protolog", "cloudlog", "schedlog", "message", "nodesync":
// Stringify and print
fmt.Println(FormatLog(log))
// Send into observer queue if toggle is set
if observertoggle {
meshorchestrator.ObserverQueue <- *NewObserverLog(log)
}
case "handshake", "meshsync":
// Call the method to update the meshorchestrator's NodeIDlist
go meshorchestrator.UpdateNodeIDlist()
// Stringify and print
fmt.Println(FormatLog(log))
// Send into observer queue if toggle is set
if observertoggle {
meshorchestrator.ObserverQueue <- *NewObserverLog(log)
}
case "sensordata":
// Set the sensor node data to be added into the accumulation queue
go meshorchestrator.SetSensorData(log)
// Stringify and print
fmt.Println(FormatLog(log))
// Send into observer queue if toggle is set
if observertoggle {
meshorchestrator.ObserverQueue <- *NewObserverLog(log)
}
case "configdata":
// Set the node configuration on the meshorchestrator's Nodelist
go meshorchestrator.SetNode(log)
// Stringify and print
fmt.Println(FormatLog(log))
// Send into observer queue if toggle is set
if observertoggle {
meshorchestrator.ObserverQueue <- *NewObserverLog(log)
}
case "ctrldata":
// Set the meshorchestrator's Controlnode
go meshorchestrator.SetControlnode(log)
// Stringify and print
fmt.Println(FormatLog(log))
// Send into observer queue if toggle is set
if observertoggle {
meshorchestrator.ObserverQueue <- *NewObserverLog(log)
}
case "nodelist":
// Set the meshorchestrator's NodeIDlist
go meshorchestrator.SetNodeIDlist(log)
// Stringify and print
fmt.Println(FormatLog(log))
// Send into observer queue if toggle is set
if observertoggle {
meshorchestrator.ObserverQueue <- *NewObserverLog(log)
}
case "toggle":
// Check the toggle command
observertogglecommand := log.GetLogmessage()
switch observertogglecommand {
case "enable-observe":
// Enable the observerqueue
observertoggle = true
// Generate a server log
fmt.Println(FormatLog(NewOrchServerlog("(toggle) observer queue enabled")))
case "disable-observe":
// Disable the observerqueue
observertoggle = false
// Generate a server log
fmt.Println(FormatLog(NewOrchServerlog("(toggle) observer queue disabled")))
}
}
}
}