-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
588 lines (511 loc) · 13.2 KB
/
log.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
package log
import (
"fmt"
"time"
)
type LogEntry interface {
LogTime() time.Time
Stream() string
Level() LogLevel
Message() string
HasAssociatedError() bool
AssociatedError() error
HasTrace() bool
Trace() []*StackTraceEntry
}
type LogEntryFormatter interface {
Format(entry LogEntry) string
}
type LoggingContext interface {
HasStream(key string) bool
Stream(key string) (LogStream, bool)
DefaultLogLevel() LogLevel
SetDefaultLogLevel(level LogLevel)
DefaultLogListenerLevel() LogLevel
SetDefaultLogListenerLevel(level LogLevel)
AddGlobalLogListener(logListener LogListener, level LogLevel)
RemoveGlobalLogListener(logListener LogListener)
TracesByDefault() bool
SetTracesByDefault(traces bool)
GlobalListeners() []LogListener
DebuggingEnabled() bool
EnableDebugging(val bool)
}
type Log interface {
Log(level LogLevel, msg string)
Logf(level LogLevel, format string, args ...interface{})
LogTrace(level LogLevel, msg string)
LogTracef(level LogLevel, format string, args ...interface{})
Fatal(msg string)
Fatalf(format string, args ...interface{})
FatalTrace(msg string)
FatalTracef(format string, args ...interface{})
Error(err error)
Errorf(err error, format string, args ...interface{})
Warning(msg string)
Warningf(format string, args ...interface{})
WarningTrace(msg string)
WarningTracef(format string, args ...interface{})
Info(msg string)
Infof(format string, args ...interface{})
InfoTrace(msg string)
InfoTracef(format string, args ...interface{})
Debug(msg string)
Debugf(format string, args ...interface{})
DebugTrace(msg string)
DebugTracef(format string, args ...interface{})
Trace(msg string)
Tracef(format string, args ...interface{})
}
type LogStream interface {
Log
Context() LoggingContext
Name() string
DefaultLogLevel() LogLevel
SetDefaultLogLevel(level LogLevel)
DefaultLogListenerLevel() LogLevel
SetDefaultLogListenerLevel(level LogLevel)
AddLogListener(logListener LogListener, level LogLevel)
RemoveLogListener(logListener LogListener)
TracesByDefault() bool
SetTracesByDefault(traces bool)
IsActive() bool
Shutdown()
}
type LogLevel uint8
const (
All LogLevel = iota
FatalError
Error
Error2
Error3
Warning
Warning2
Warning3
Info
Info2
Info3
Debug
Debug2
Debug3
Debug4
Debug5
Trace
None
Default
)
func (ll LogLevel) String() string {
switch(ll) {
case All : return "All"
case FatalError: return "FatalError"
case Error: return "Error"
case Error2: return "Error-2"
case Error3: return "Error-3"
case Warning: return "Warning"
case Warning2: return "Warning-2"
case Warning3: return "Warning-3"
case Info: return "Info"
case Info2: return "Info-2"
case Info3: return "Info-3"
case Debug: return "Debug"
case Debug2: return "Debug-2"
case Debug3: return "Debug-3"
case Debug4: return "Debug-4"
case Debug5: return "Debug-5"
case Trace: return "Trace"
case None: return "None"
}
panic("invalid log level")
}
func (ll LogLevel) IsFatal() bool {
return ll == FatalError
}
func (ll LogLevel) IsError() bool {
switch(ll) {
case Error: return true
case Error2: return true
case Error3: return true
}
return false
}
func (ll LogLevel) IsWarning() bool {
switch(ll) {
case Warning: return true
case Warning2: return true
case Warning3: return true
}
return false
}
func (ll LogLevel) IsInfo() bool {
switch(ll) {
case Info: return true
case Info2: return true
case Info3: return true
}
return false
}
func (ll LogLevel) IsDebug() bool {
switch(ll) {
case Debug: return true
case Debug2: return true
case Debug3: return true
case Debug4: return true
case Debug5: return true
}
return false
}
func (ll LogLevel) IsTrace() bool {
return ll == Trace
}
///
type stdLoggingContext struct {
lock chan bool
debugging bool
streams map[string]*stdLogStream
defaultLogLevel LogLevel
defaultListenerLevel LogLevel
listeners map[LogListener]LogLevel
traces bool
}
type stdLogStream struct {
lock chan bool
ctx *stdLoggingContext
name string
defaultLevel LogLevel
defaultListenerLevel LogLevel
listeners map[LogListener]LogLevel
traces bool
active bool
}
type stdLogEntry struct {
ts time.Time
stream LogStream
level LogLevel
message string
associatedError error
stackTrace []*StackTraceEntry
}
func CreateLoggingContext() LoggingContext {
ctx := &stdLoggingContext{
lock: make(chan bool, 1),
streams: make(map[string]*stdLogStream),
defaultLogLevel: Info,
listeners: make(map[LogListener]LogLevel),
}
ctx.lock <- true
return ctx
}
func (ctx *stdLoggingContext) HasStream(key string) bool {
<-ctx.lock
defer func() { ctx.lock <- true }()
_, has := ctx.streams[key]
return has
}
func (ctx *stdLoggingContext) Stream(key string) (LogStream, bool) {
<-ctx.lock
defer func() { ctx.lock <- true }()
stream, has := ctx.streams[key]
if has {
return stream, false
}
// We will create a new log stream.
ns := &stdLogStream{
lock: make(chan bool, 1),
ctx: ctx,
name: key,
defaultLevel: Default,
defaultListenerLevel: Default,
listeners: make(map[LogListener]LogLevel),
traces: false,
active: true,
}
ns.lock <- true
return ns, true
}
func (ctx *stdLoggingContext) GlobalListeners() []LogListener {
<-ctx.lock
defer func() { ctx.lock <- true }()
res := make([]LogListener, 0, len(ctx.listeners))
for ll, _ := range(ctx.listeners) {
res = append(res, ll)
}
return res
}
func (ctx *stdLoggingContext) DebuggingEnabled() bool {
<-ctx.lock
defer func() { ctx.lock <- true }()
return ctx.debugging
}
func (ctx *stdLoggingContext) EnableDebugging(val bool) {
<-ctx.lock
defer func() { ctx.lock <- true }()
ctx.debugging = val
}
func (ctx *stdLoggingContext) DefaultLogLevel() LogLevel {
<-ctx.lock
defer func() { ctx.lock <- true }()
return ctx.defaultLogLevel
}
func (ctx *stdLoggingContext) SetDefaultLogLevel(level LogLevel) {
<-ctx.lock
defer func() { ctx.lock <- true }()
ctx.defaultLogLevel = level
}
func (ctx *stdLoggingContext) DefaultLogListenerLevel() LogLevel {
<-ctx.lock
defer func() { ctx.lock <- true }()
return ctx.defaultListenerLevel
}
func (ctx *stdLoggingContext) SetDefaultLogListenerLevel(level LogLevel) {
<-ctx.lock
defer func() { ctx.lock <- true }()
ctx.defaultListenerLevel = level
}
func (ctx *stdLoggingContext) AddGlobalLogListener(logListener LogListener, level LogLevel) {
<-ctx.lock
defer func() { ctx.lock <- true }()
delete(ctx.listeners, logListener)
ctx.listeners[logListener] = level
}
func (ctx *stdLoggingContext) RemoveGlobalLogListener(logListener LogListener) {
<-ctx.lock
defer func() { ctx.lock <- true }()
delete(ctx.listeners, logListener)
}
func (ctx *stdLoggingContext) TracesByDefault() bool {
<-ctx.lock
defer func() { ctx.lock <- true }()
return ctx.traces
}
func (ctx *stdLoggingContext) SetTracesByDefault(traces bool) {
<-ctx.lock
defer func() { ctx.lock <- true }()
ctx.traces = traces
}
func (ls *stdLogStream) Context() LoggingContext {
<-ls.lock
defer func() { ls.lock <- true }()
return ls.ctx
}
func (ls *stdLogStream) DefaultLogLevel() LogLevel {
<-ls.lock
defer func() { ls.lock <- true }()
return ls.defaultLevel
}
func (ls *stdLogStream) SetDefaultLogLevel(level LogLevel) {
<-ls.lock
defer func() { ls.lock <- true }()
ls.defaultLevel = level
}
func (ls *stdLogStream) DefaultLogListenerLevel() LogLevel {
<-ls.lock
defer func() { ls.lock <- true }()
return ls.defaultListenerLevel
}
func (ls *stdLogStream) SetDefaultLogListenerLevel(level LogLevel) {
<-ls.lock
defer func() { ls.lock <- true }()
ls.defaultListenerLevel = level
}
func (ls *stdLogStream) AddLogListener(logListener LogListener, level LogLevel) {
<-ls.lock
defer func() { ls.lock <- true }()
delete(ls.listeners, logListener)
ls.listeners[logListener] = level
}
func (ls *stdLogStream) RemoveLogListener(logListener LogListener) {
<-ls.lock
defer func() { ls.lock <- true }()
delete(ls.listeners, logListener)
}
func (ls *stdLogStream) TracesByDefault() bool {
<-ls.lock
defer func() { ls.lock <- true }()
return ls.traces
}
func (ls *stdLogStream) SetTracesByDefault(traces bool) {
<-ls.lock
defer func() { ls.lock <- true }()
ls.traces = traces
}
func (ls *stdLogStream) IsActive() bool {
<-ls.lock
defer func() { ls.lock <- true }()
return ls.active
}
func (ls *stdLogStream) Name() string {
return ls.name
}
func (ls *stdLogStream) Shutdown() {
panic("stdLogStream.Shutdown() unimplemented")
}
func lockChan(c chan bool) { <- c }
func unlockChan(c chan bool) {
select {
case c <- true:
default:
}
}
func (ls *stdLogStream) Log(level LogLevel, msg string) {
ls.dispatchLog(level, false, nil, msg)
}
func (ls *stdLogStream) Logf(level LogLevel, format string, args ...interface{}) {
ls.dispatchLog(level, false, nil, format, args...)
}
func (ls *stdLogStream) dispatchLog(level LogLevel, generateTrace bool, setError error, format string, args ...interface{}) {
ts := time.Now()
// First assess interest - no point in doing the formatting
// if no loggers will receive.
lockChan(ls.lock)
defer unlockChan(ls.lock)
lockChan(ls.ctx.lock)
defer unlockChan(ls.ctx.lock)
interest := make([]LogListener, 0, 8)
for ll, lv := range ls.listeners {
if lv >= level || (lv == Default && ls.ctx.defaultListenerLevel <= level) || level == All {
interest = append(interest, ll)
}
}
for ll, lv := range ls.ctx.listeners {
//fmt.Printf("lv: %s level: %s show: ", lv.String(), level.String())
//fmt.Println(lv >= level)
if lv >= level || (lv == Default && ls.ctx.defaultListenerLevel <= level) || level == All {
interest = append(interest, ll)
}
}
unlockChan(ls.ctx.lock)
if len(interest) > 0 {
var msg string
if len(args) > 0 {
msg = fmt.Sprintf(format, args...)
} else {
msg = format
}
entry := &stdLogEntry{
ts: ts,
stream: ls,
level: level,
message: msg,
}
if ls.traces || ls.ctx.traces || generateTrace {
entry.stackTrace = GenerateStackTrace()
}
if setError != nil {
entry.associatedError = setError
}
unlockChan(ls.lock)
for _, ll := range interest {
// go ll.Receive(entry)
ll.Receive(entry)
}
}
}
func (ls *stdLogStream) LogTrace(level LogLevel, msg string) {
ls.dispatchLog(level, true, nil, msg)
}
func (ls *stdLogStream) LogTracef(level LogLevel, format string, args ...interface{}) {
ls.dispatchLog(level, true, nil, format, args...)
}
func (ls *stdLogStream) Fatal(msg string) {
ls.dispatchLog(FatalError, false, nil, msg)
}
func (ls *stdLogStream) Fatalf(format string, args ...interface{}) {
ls.dispatchLog(FatalError, false, nil, format, args...)
}
func (ls *stdLogStream) FatalTrace(msg string) {
ls.dispatchLog(FatalError, true, nil, msg)
}
func (ls *stdLogStream) FatalTracef(format string, args ...interface{}) {
ls.dispatchLog(FatalError, true, nil, format, args...)
}
func (ls *stdLogStream) Error(err error) {
ls.dispatchLog(Error, false, err, err.Error())
}
func (ls *stdLogStream) Errorf(err error, format string, args ...interface{}) {
ls.dispatchLog(Error, false, err, format, args...)
}
func (ls *stdLogStream) Warning(msg string) {
ls.dispatchLog(Warning, false, nil, msg)
}
func (ls *stdLogStream) Warningf(format string, args ...interface{}) {
ls.dispatchLog(Warning, false, nil, format, args...)
}
func (ls *stdLogStream) WarningTrace(msg string) {
ls.dispatchLog(Warning, true, nil, msg)
}
func (ls *stdLogStream) WarningTracef(format string, args ...interface{}) {
ls.dispatchLog(Warning, true, nil, format, args...)
}
func (ls *stdLogStream) Info(msg string) {
ls.dispatchLog(Info, false, nil, msg)
}
func (ls *stdLogStream) Infof(format string, args ...interface{}) {
ls.dispatchLog(Info, false, nil, format, args...)
}
func (ls *stdLogStream) InfoTrace(msg string) {
ls.dispatchLog(Info, true, nil, msg)
}
func (ls *stdLogStream) InfoTracef(format string, args ...interface{}) {
ls.dispatchLog(Info, true, nil, format, args...)
}
func (ls *stdLogStream) Debug(msg string) {
if ls.ctx.debugging {
ls.dispatchLog(Debug, false, nil, msg)
}
}
func (ls *stdLogStream) Debugf(format string, args ...interface{}) {
if ls.ctx.debugging {
ls.dispatchLog(Debug, false, nil, format, args...)
}
}
func (ls *stdLogStream) DebugTrace(msg string) {
if ls.ctx.debugging {
ls.dispatchLog(Debug, true, nil, msg)
}
}
func (ls *stdLogStream) DebugTracef(format string, args ...interface{}) {
if ls.ctx.debugging {
ls.dispatchLog(Debug, true, nil, format, args...)
}
}
func (ls *stdLogStream) Trace(msg string) {
if ls.ctx.debugging {
ls.dispatchLog(Trace, true, nil, msg)
}
}
func (ls *stdLogStream) Tracef(format string, args ...interface{}) {
if ls.ctx.debugging {
ls.dispatchLog(Trace , true, nil, format, args...)
}
}
func (le *stdLogEntry) LogTime() time.Time {
return le.ts
}
func (le *stdLogEntry) Stream() string {
return le.stream.Name()
}
func (le *stdLogEntry) Message() string {
return le.message
}
func (le *stdLogEntry) HasAssociatedError() bool {
return le.associatedError != nil
}
func (le *stdLogEntry) AssociatedError() error {
return le.associatedError
}
func (le *stdLogEntry) HasTrace() bool {
if le.associatedError != nil {
}
return le.stackTrace != nil
}
func (le *stdLogEntry) Level() LogLevel {
return le.level
}
func (le *stdLogEntry) Trace() []*StackTraceEntry {
if le.stackTrace == nil {
return nil
}
res := make([]*StackTraceEntry, len(le.stackTrace))
copy(res, le.stackTrace)
return res
}