-
Notifications
You must be signed in to change notification settings - Fork 0
/
logg.go
602 lines (568 loc) · 15 KB
/
logg.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package log
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"time"
"github.com/cybriq/atomic"
"github.com/davecgh/go-spew/spew"
"github.com/gookit/color"
)
const (
_Off = iota
_Fatal
_Error
_Chek
_Warn
_Info
_Debug
_Trace
)
type (
// LevelPrinter defines a set of terminal printing primitives that output with
// extra data, time, log logLevelList, and code location
LevelPrinter struct {
// Ln prints lists of interfaces with spaces in between
Ln func(a ...interface{})
// F prints like fmt.Println surrounded by log details
F func(format string, a ...interface{})
// S prints a spew.Sdump for an interface slice
S func(a ...interface{})
// C accepts a function so that the extra computation can be avoided if it is
// not being viewed
C func(closure func() string)
// Chk is a shortcut for printing if there is an error, or returning true
Chk func(e error) bool
}
Logger struct {
F, E, W, I, D, T LevelPrinter
}
logLevelList struct {
Off, Fatal, Error, Check, Warn, Info, Debug, Trace int32
}
LevelSpec struct {
ID int32
Name string
Colorizer func(format string, a ...interface{}) string
}
// Entry is a log entry to be printed as json to the log file
Entry struct {
Time time.Time
Level string
Package string
CodeLocation string
Text string
}
)
var (
logger_started = time.Now()
App = " main"
AppColorizer = color.Gray.Sprint
// sep is just a convenient shortcut for this very longwinded expression
sep = string(os.PathSeparator)
currentLevel = atomic.NewInt32(logLevels.Info)
// writer can be swapped out for any io.*writer* that you want to use instead of
// stdout.
writer io.Writer = os.Stderr
// allSubsystems stores all of the package subsystem names found in the current
// application
allSubsystems []string
// highlighted is a text that helps visually distinguish a log entry by category
highlighted = make(map[string]struct{})
// logFilter specifies a set of packages that will not print logs
logFilter = make(map[string]struct{})
// mutexes to prevent concurrent map accesses
highlightMx, logFilterMx sync.Mutex
// logLevels is a shorthand access that minimises possible Name collisions in the
// dot import
logLevels = logLevelList{
Off: _Off,
Fatal: _Fatal,
Error: _Error,
Check: _Chek,
Warn: _Warn,
Info: _Info,
Debug: _Debug,
Trace: _Trace,
}
// LevelSpecs specifies the id, string name and color-printing function
LevelSpecs = []LevelSpec{
{
logLevels.Off, "off ",
color.Bit24(0, 0, 0, false).Sprintf,
},
{
logLevels.Fatal, "fatal",
color.Bit24(128, 0, 0, false).Sprintf,
},
{
logLevels.Error, "error",
color.Bit24(255, 0, 0, false).Sprintf,
},
{
logLevels.Check, "check",
color.Bit24(255, 255, 0, false).Sprintf,
},
{
logLevels.Warn, "warn ",
color.Bit24(0, 255, 0, false).Sprintf,
},
{
logLevels.Info, "info ",
color.Bit24(255, 255, 0, false).Sprintf,
},
{
logLevels.Debug, "debug",
color.Bit24(0, 128, 255, false).Sprintf,
},
{
logLevels.Trace, "trace",
color.Bit24(128, 0, 255, false).Sprintf,
},
}
Levels = []string{
Off,
Fatal,
Error,
Check,
Warn,
Info,
Debug,
Trace,
}
LogChanDisabled = atomic.NewBool(true)
LogChan chan Entry
)
const (
Off = "off"
Fatal = "fatal"
Error = "error"
Warn = "warn"
Info = "info"
Check = "check"
Debug = "debug"
Trace = "trace"
)
// AddLogChan adds a channel that log entries are sent to
func AddLogChan() (ch chan Entry) {
LogChanDisabled.Store(false)
if LogChan != nil {
panic("warning warning")
}
// L.Writer.Write.Store( false
LogChan = make(chan Entry)
return LogChan
}
// Get returns a set of LevelPrinter with their subsystem preloaded
func Get(subsystem string) (l *Logger) {
return &Logger{
_getOnePrinter(_Fatal, subsystem),
_getOnePrinter(_Error, subsystem),
_getOnePrinter(_Warn, subsystem),
_getOnePrinter(_Info, subsystem),
_getOnePrinter(_Debug, subsystem),
_getOnePrinter(_Trace, subsystem),
}
}
func _getOnePrinter(level int32, subsystem string) LevelPrinter {
return LevelPrinter{
Ln: _ln(level, subsystem),
F: _f(level, subsystem),
S: _s(level, subsystem),
C: _c(level, subsystem),
Chk: _chk(level, subsystem),
}
}
// SetLogLevel sets the log level via a string, which can be truncated down to
// one character, similar to nmcli's argument processor, as the first letter is
// unique. This could be used with a linter to make larger command sets.
func SetLogLevel(l string) {
if l == "" {
l = "info"
}
// fmt.Fprintln(os.Stderr, "setting log level", l)
lvl := logLevels.Info
for i := range LevelSpecs {
if LevelSpecs[i].Name[:1] == l[:1] {
lvl = LevelSpecs[i].ID
}
}
currentLevel.Store(lvl)
}
// SetLogWriter atomically changes the log io.Writer interface
func SetLogWriter(wr io.Writer) {
// w := unsafe.Pointer(writer)
// c := unsafe.Pointer(wr)
// atomic.SwapPointer(&w, c)
writer = wr
}
func SetLogWriteToFile(path, appName string) (e error) {
// copy existing log file to dated log file as we will truncate it per
// session
path = filepath.Join(path, "log"+appName)
if _, e = os.Stat(path); e == nil {
var b []byte
b, e = ioutil.ReadFile(path)
if e == nil {
ioutil.WriteFile(path+fmt.Sprint(time.Now().Unix()), b, 0600)
}
}
var fileWriter *os.File
if fileWriter, e = os.OpenFile(
path, os.O_RDWR|os.O_CREATE|os.O_TRUNC,
0600,
); e != nil {
fmt.Fprintln(os.Stderr, "unable to write log to", path, "error:", e)
return
}
mw := io.MultiWriter(os.Stderr, fileWriter)
fileWriter.Write([]byte("logging to file '" + path + "'\n"))
mw.Write([]byte("logging to file '" + path + "'\n"))
SetLogWriter(mw)
return
}
// sortSubsystemsList sorts the list of subsystems, to keep the data read-only,
// call this function right at the top of the main, which runs after
// declarations and main/init. Really this is just here to alert the reader.
func sortSubsystemsList() {
sort.Strings(allSubsystems)
// fmt.Fprintln(
// os.Stderr,
// spew.Sdump(allSubsystems),
// spew.Sdump(highlighted),
// spew.Sdump(logFilter),
// )
}
// Add adds a subsystem to the list of known subsystems and returns the
// string so it is nice and neat in the package logg.go file
func Add(pathBase string) (subsystem string) {
// var split []string
var ok bool
var file string
_, file, _, ok = runtime.Caller(1)
if ok {
r := strings.Split(file, pathBase)
// fmt.Fprintln(os.Stderr, version.PathBase, r)
fromRoot := filepath.Base(file)
if len(r) > 1 {
fromRoot = r[1]
}
split := strings.Split(fromRoot, "/")
// fmt.Fprintln(os.Stderr, version.PathBase, "file", file, r, fromRoot, split)
subsystem = strings.Join(split[:len(split)-1], "/")
// fmt.Fprintln(os.Stderr, "adding subsystem", subsystem)
allSubsystems = append(allSubsystems, subsystem)
sortSubsystemsList()
}
return
}
// StoreHighlightedSubsystems sets the list of subsystems to highlight
func StoreHighlightedSubsystems(highlights []string) (found bool) {
highlightMx.Lock()
highlighted = make(map[string]struct{}, len(highlights))
for i := range highlights {
highlighted[highlights[i]] = struct{}{}
}
highlightMx.Unlock()
return
}
// LoadHighlightedSubsystems returns a copy of the map of highlighted subsystems
func LoadHighlightedSubsystems() (o []string) {
highlightMx.Lock()
o = make([]string, len(logFilter))
var counter int
for i := range logFilter {
o[counter] = i
counter++
}
highlightMx.Unlock()
sort.Strings(o)
return
}
// StoreSubsystemFilter sets the list of subsystems to filter
func StoreSubsystemFilter(filter []string) {
logFilterMx.Lock()
logFilter = make(map[string]struct{}, len(filter))
for i := range filter {
logFilter[filter[i]] = struct{}{}
}
logFilterMx.Unlock()
}
// LoadSubsystemFilter returns a copy of the map of filtered subsystems
func LoadSubsystemFilter() (o []string) {
logFilterMx.Lock()
o = make([]string, len(logFilter))
var counter int
for i := range logFilter {
o[counter] = i
counter++
}
logFilterMx.Unlock()
sort.Strings(o)
return
}
// _isHighlighted returns true if the subsystem is in the list to have attention
// getters added to them
func _isHighlighted(subsystem string) (found bool) {
highlightMx.Lock()
_, found = highlighted[subsystem]
highlightMx.Unlock()
return
}
// AddHighlightedSubsystem adds a new subsystem Name to the highlighted list
func AddHighlightedSubsystem(hl string) struct{} {
highlightMx.Lock()
highlighted[hl] = struct{}{}
highlightMx.Unlock()
return struct{}{}
}
// _isSubsystemFiltered returns true if the subsystem should not pr logs
func _isSubsystemFiltered(subsystem string) (found bool) {
logFilterMx.Lock()
_, found = logFilter[subsystem]
logFilterMx.Unlock()
return
}
// AddFilteredSubsystem adds a new subsystem Name to the highlighted list
func AddFilteredSubsystem(hl string) struct{} {
logFilterMx.Lock()
logFilter[hl] = struct{}{}
logFilterMx.Unlock()
return struct{}{}
}
func getTimeText(level int32) string {
// since := time.Now().Sub(logger_started).Round(time.Millisecond).String()
// diff := 12 - len(since)
// if diff > 0 {
// since = strings.Repeat(" ", diff) + since + " "
// }
return AppColorizer(logger_started.Format(time.StampMilli))
}
func _ln(level int32, subsystem string) func(a ...interface{}) {
return func(a ...interface{}) {
if level <= currentLevel.Load() && !_isSubsystemFiltered(subsystem) {
printer := fmt.Sprintf
if _isHighlighted(subsystem) {
printer = color.Bold.Sprintf
}
fmt.Fprintf(
writer,
printer(
"%-58v%s%s%-6v %s\n",
getLoc(2, level, subsystem),
getTimeText(level),
fmt.Sprint(AppColorizer(" "+App)),
LevelSpecs[level].Colorizer(
AppColorizer(" "+LevelSpecs[level].Name+" "),
),
AppColorizer(joinStrings(" ", a...)),
),
)
}
}
}
func _f(level int32, subsystem string) func(format string, a ...interface{}) {
return func(format string, a ...interface{}) {
if level <= currentLevel.Load() && !_isSubsystemFiltered(subsystem) {
printer := fmt.Sprintf
if _isHighlighted(subsystem) {
printer = color.Bold.Sprintf
}
fmt.Fprintf(
writer,
printer(
"%-58v%s%s%-6v %s\n",
getLoc(2, level, subsystem),
getTimeText(level),
LevelSpecs[level].Colorizer(" "+App),
LevelSpecs[level].Colorizer(
AppColorizer(" "+LevelSpecs[level].Name+" "),
),
AppColorizer(fmt.Sprintf(format, a...)),
),
)
}
}
}
func _s(level int32, subsystem string) func(a ...interface{}) {
return func(a ...interface{}) {
if level <= currentLevel.Load() && !_isSubsystemFiltered(subsystem) {
printer := fmt.Sprintf
if _isHighlighted(subsystem) {
printer = color.Bold.Sprintf
}
fmt.Fprintf(
writer,
printer(
"%-58v%s%s%s%s%s\n",
getLoc(2, level, subsystem),
getTimeText(level),
LevelSpecs[level].Colorizer(" "+App),
LevelSpecs[level].Colorizer(
AppColorizer(" "+LevelSpecs[level].Name+" "),
),
AppColorizer(" spew:"),
fmt.Sprint(
AppColorizer("\n\n"+spew.Sdump(a)),
"\n",
),
),
)
}
}
}
func _c(level int32, subsystem string) func(closure func() string) {
return func(closure func() string) {
if level <= currentLevel.Load() && !_isSubsystemFiltered(subsystem) {
printer := fmt.Sprintf
if _isHighlighted(subsystem) {
printer = color.Bold.Sprintf
}
fmt.Fprintf(
writer,
printer(
"%-58v%s%s%-6v %s\n",
getLoc(2, level, subsystem),
getTimeText(level),
AppColorizer(" "+App),
LevelSpecs[level].Colorizer(
AppColorizer(" "+LevelSpecs[level].Name+" "),
),
AppColorizer(closure()),
),
)
}
}
}
func _chk(level int32, subsystem string) func(e error) bool {
return func(e error) bool {
if level <= currentLevel.Load() && !_isSubsystemFiltered(subsystem) {
if e != nil {
printer := fmt.Sprintf
if _isHighlighted(subsystem) {
printer = color.Bold.Sprintf
}
fmt.Fprintf(
writer,
printer(
"%-58v%s%s%-6v %s\n",
getLoc(2, level, subsystem),
getTimeText(level),
AppColorizer(" "+App),
LevelSpecs[level].Colorizer(
AppColorizer(" "+LevelSpecs[level].Name+" "),
),
LevelSpecs[level].Colorizer(
joinStrings(
" ", e.Error(),
),
),
),
)
return true
}
}
return false
}
}
// joinStrings constructs a string from an slice of interface same as Println
// but without the terminal newline
func joinStrings(sep string, a ...interface{}) (o string) {
for i := range a {
o += fmt.Sprint(a[i])
if i < len(a)-1 {
o += sep
}
}
return
}
// getLoc calls runtime.Caller and formats as expected by source code editors
// for terminal hyperlinks
//
// Regular expressions and the substitution texts to make these clickable in
// Tilix and other RE hyperlink configurable terminal emulators:
//
// This matches the shortened paths generated in this command and printed at
// the very beginning of the line as this logger prints:
//
// ^((([\/a-zA-Z@0-9-_.]+/)+([a-zA-Z@0-9-_.]+)):([0-9]+))
//
// goland --line $5 $GOPATH/src/github.com/p9c/matrjoska/$2
//
// I have used a shell variable there but tilix doesn't expand them,
// so put your GOPATH in manually, and obviously change the repo subpath.
//
//
// Change the path to use with another repository's logging output (
// someone with more time on their hands could probably come up with
// something, but frankly the custom links feature of Tilix has the absolute
// worst UX I have encountered since the 90s...
// Maybe in the future this library will be expanded with a tool that more
// intelligently sets the path, ie from CWD or other cleverness.
//
// This matches full paths anywhere on the commandline delimited by spaces:
//
// ([/](([\/a-zA-Z@0-9-_.]+/)+([a-zA-Z@0-9-_.]+)):([0-9]+))
//
// goland --line $5 /$2
//
// Adapt the invocation to open your preferred editor if it has the capability,
// the above is for Jetbrains Goland
//
func getLoc(skip int, level int32, subsystem string) (output string) {
_, file, line, _ := runtime.Caller(skip)
defer func() {
if r := recover(); r != nil {
fmt.Fprintln(
os.Stderr, "getloc panic on subsystem", subsystem, file,
)
}
}()
split := strings.Split(file, subsystem)
if len(split) < 2 {
output = fmt.Sprint(
color.White.Sprint(subsystem),
color.Gray.Sprint(
file, ":", line,
),
)
} else {
output = fmt.Sprint(
color.White.Sprint(subsystem),
color.Gray.Sprint(
split[1], ":", line,
),
)
}
return
}
// DirectionString is a helper function that returns a string that represents
// the direction of a connection (inbound or outbound).
func DirectionString(inbound bool) string {
if inbound {
return "inbound"
}
return "outbound"
}
func PickNoun(n int, singular, plural string) string {
if n == 1 {
return singular
}
return plural
}
func FileExists(filePath string) bool {
_, e := os.Stat(filePath)
return e == nil
}
func Caller(comment string, skip int) string {
_, file, line, _ := runtime.Caller(skip + 1)
o := fmt.Sprintf("%s: %s:%d", comment, file, line)
// L.Debug(o)
return o
}