-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlogger.go
331 lines (304 loc) · 8.81 KB
/
mlogger.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
package mlogger
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
// LoggerData is the data sent to the logger from a client
type LoggerData struct {
Id string // an unique identifier is necessary for aggregating messages
Message string // possible description
Data []int // effective data
Aggregate bool // true if the log entry is be cumulative on its data
}
// logMessage is an internal type for the logging message
type logMessage struct {
id int // logger identifier
level string // unique message level
msg LoggerData // message for the logger
}
//
type logfile struct {
idLength int
levelLength int
messageLength int
filename string
file *os.File
}
// Exported variables
var BufDepth = 50 // buffer depth for the channel to the logger thread
// Internal variables
var declaredLogs map[int]logfile
var lock sync.RWMutex
var loggerChan chan logMessage
var once sync.Once
var consoleLog = false
var index = 0
var verbose = false
var unrolled = false
// DeclareLog is used to declare a new log of name 'fn'. If 'dt' is true the name will become fn_current date.logfile.
// Furthermore, a new logfile will be created everyday. Otherwise it will be fn.logfile and will not be created everyday.
// It returns the log identifier (int) and the eventual error
func DeclareLog(fn string, dt bool) (int, error) {
lock.Lock()
defer lock.Unlock()
SetUpLogger(consoleLog)
pwd, err := os.Getwd()
if err != nil {
return -1, err
}
err = os.MkdirAll("log", os.ModePerm)
if err != nil {
return -1, err
}
if dt {
ct := time.Now().Local()
declaredLogs[index] = logfile{filename: filepath.Join(pwd, "log", fn+"_"+ct.Format("2006-01-02")+".logfile")}
//availableLogs = append(availableLogs, filepath.Join(pwd, "log", fn+"_"+ct.Format("2006-01-02")+".logfile"))
} else {
declaredLogs[index] = logfile{filename: filepath.Join(pwd, "log", fn+".logfile")}
//availableLogs = append(availableLogs, filepath.Join(pwd, "log", fn+".logfile"))
}
index += 1
return index - 1, nil
}
// Close close all open files
func Close() (e error) {
lock.Lock()
for _, file := range declaredLogs {
if er := file.file.Close(); er != nil && e == nil {
e = errors.New("error inc closing logfiles")
}
}
lock.Unlock()
return
}
// Enables verbose (affects all logs)
func Verbose(v bool) {
verbose = v
}
// Enables unroll (affects all logs)
func Unroll(v bool, fn string) {
if v {
file, err := os.OpenFile(fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
log.SetOutput(file)
}
unrolled = v
}
// SetTextLimit sets formatting limits for message (lm), id (li) and level (ll) in number of characters for logfile tag
func SetTextLimit(tag, lm, li, ll int) error {
lock.Lock()
defer lock.Unlock()
if c, ok := declaredLogs[tag]; ok {
c.levelLength = ll
c.idLength = li
c.messageLength = lm
declaredLogs[tag] = c
return nil
} else {
return errors.New("cannot set parameters of a not declared logfile")
}
}
// SetUpLogger set-up the loggerChan. a sync.Once is used to avoid issues with multiple modules
// cf controls the console flag that determines if the application writes logging errors to the consoles or ignores them
func SetUpLogger(cf bool) {
once.Do(func() {
//lock.Lock()
//defer lock.Unlock()
consoleLog = cf
declaredLogs = make(map[int]logfile)
loggerChan = make(chan logMessage, BufDepth)
go logger(loggerChan)
})
}
// Log writes a generic comment to the logfile lg
func Log(lg int, dt LoggerData) { loggerChan <- logMessage{lg, "LOG", dt} }
// Error writes an error level line to the logfile lg
func Error(lg int, dt LoggerData) { loggerChan <- logMessage{lg, "ERROR", dt} }
// Info writes an info level line to the logfile lg
func Info(lg int, dt LoggerData) { loggerChan <- logMessage{lg, "INFO", dt} }
// Warning writes a warning level line to the logfile lg
func Warning(lg int, dt LoggerData) { loggerChan <- logMessage{lg, "WARNING", dt} }
// Recovered writes a panic recovery level line to the logfile lg
func Recovered(lg int, dt LoggerData) { loggerChan <- logMessage{lg, "RECOVERED", dt} }
// Panic writes a panic level line to the logfile lg and stop execution (if quit is true)
func Panic(lg int, dt LoggerData, quit bool) {
loggerChan <- logMessage{lg, "PANIC", dt}
if quit {
time.Sleep(5 * time.Second)
lock.RLock()
file := strings.Split(declaredLogs[lg].filename, "/")
lock.RUnlock()
fmt.Println("Panic error, execution terminated. See log", file[len(file)-1])
os.Exit(0)
}
}
// SetError, depending if the error is not nil, sets the error or not
func SetError(lg int, es string, id string, e error, data []int, aggregate bool) bool {
if id == "" {
id = "n/a"
}
if e != nil {
Error(lg, LoggerData{id, es, data, aggregate})
return false
}
return true
}
// logger is the core thread handling all the writings to log files
func logger(data chan logMessage) {
logEntryGenerator := func(file logfile, olddate string, d logMessage, dt ...[]int) (msg string) {
date := time.Now().Format("Mon Jan:_2 15:04 2006")
if file.messageLength != 0 {
if len(d.msg.Message) > file.messageLength {
d.msg.Message = d.msg.Message[0:file.messageLength]
} else {
for len(d.msg.Message) < file.messageLength {
d.msg.Message += " "
}
}
}
if file.idLength != 0 {
if len(d.msg.Id) > file.idLength {
d.msg.Id = d.msg.Id[0:file.idLength]
} else {
for len(d.msg.Id) < file.idLength {
d.msg.Id += " "
}
}
}
if file.levelLength != 0 {
if len(d.level) > file.levelLength {
d.level = d.level[0:file.levelLength]
} else {
for len(d.level) < file.levelLength {
d.level += " "
}
}
}
msg = d.msg.Id + "\t\t" + d.level + "\t\t" + d.msg.Message + ""
if len(dt) == 0 {
msg = date + "\t\t" + msg
if len(d.msg.Data) != 0 {
msg += "\t\t["
for _, v := range d.msg.Data {
msg += strconv.Itoa(v) + ","
}
msg = strings.Trim(msg, ",")
msg += "]"
}
} else {
msg += "\t\t["
if len(dt[0]) != len(d.msg.Data) {
return ""
}
for i, v := range d.msg.Data {
msg += strconv.Itoa(v+dt[0][i]) + ","
}
msg = strings.Trim(msg, ",")
msg += "]"
msg = olddate + "\t\t" + msg + "\t\t" + date
}
msg = strings.Trim(msg, " ")
return
}
consoleEntryGenerator := func(d logMessage, skipDate bool) (msg string) {
date := time.Now().Format("Mon Jan:_2 15:04 2006")
msg = d.level + " -- " + d.msg.Id + ": " + d.msg.Message + ""
if !skipDate {
msg = date + " -- " + msg
}
msg = strings.Trim(msg, " ")
return
}
defer func() {
if e := recover(); e != nil {
if consoleLog {
fmt.Printf("support.logger: recovering for crash, %v\n ", e)
}
go logger(data)
}
}()
for {
d := <-data
lock.RLock()
if d.id < index {
file := declaredLogs[d.id]
lock.RUnlock()
if verbose {
fmt.Println(consoleEntryGenerator(d, false))
}
if unrolled {
log.Println(consoleEntryGenerator(d, true))
}
if input, err := ioutil.ReadFile(file.filename); err != nil {
if fn, err := os.Create(file.filename); err != nil {
if consoleLog {
fmt.Println("support.logger: error creating log: ", err)
}
} else {
if _, err := fn.WriteString(logEntryGenerator(file, "", d) + "\n"); err != nil {
if consoleLog {
fmt.Println("support.logger: error creating log: ", err)
}
}
//noinspection GoUnhandledErrorResult
fn.Close()
}
} else {
// read file and add or replace level
newC := ""
adFile := true
for _, v := range strings.Split(strings.Trim(string(input), " "), "\n") {
spv := strings.Split(v, "\t\t")
if len(spv) >= 4 {
if strings.Trim(spv[1], " ") == d.msg.Id && strings.Trim(spv[2], " ") == d.level && d.msg.Aggregate {
var nd []int
skip := false
data := strings.Split(spv[4][1:len(spv[4])-1], ",")
mainloop:
for _, dt := range data {
if val, e := strconv.Atoi(strings.Trim(dt, " ")); e == nil {
nd = append(nd, val)
} else {
cd := logMessage{d.id, "System Warning", LoggerData{"logger", "error converting accruing data from log " + d.msg.Id,
[]int{}, false}}
newC += logEntryGenerator(file, "", cd) + "\n"
newC += logEntryGenerator(file, "", d) + "\n"
skip = true
adFile = false
break mainloop
}
}
if !skip {
newC += logEntryGenerator(file, spv[0], d, nd) + "\n"
adFile = false
}
} else {
if tmp := strings.Trim(v, " "); tmp != "" {
newC += tmp + "\n"
}
}
}
}
if adFile {
newC += logEntryGenerator(file, "", d) + "\n"
}
if err = ioutil.WriteFile(file.filename, []byte(newC), 0644); err != nil {
log.Println("support.logger: error writing log: ", err)
}
}
} else {
lock.RUnlock()
}
}
}