-
Notifications
You must be signed in to change notification settings - Fork 15
/
logger.go
811 lines (656 loc) · 17.7 KB
/
logger.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
*
* Copyright (C) 2020 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Logging
*/
package main
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime/debug"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/OpenPrinting/goipp"
)
const (
// LogMinFileSize specifies a minimum value for the
// max-file-size parameter
LogMinFileSize = 16 * 1024
)
// Standard loggers
var (
// This is the default logger
Log = NewLogger().ToMainFile()
// Console logger always writes to console
Console = NewLogger().ToConsole()
// Initlog used only on initialization time
// It writes to Stdout or Stderr, depending
// on log level
InitLog = NewLogger().ToStdOutErr()
)
// LogLevel enumerates possible log levels
type LogLevel int
// LogLevel constants
const (
LogError LogLevel = 1 << iota
LogInfo
LogDebug
LogTraceIPP
LogTraceESCL
LogTraceHTTP
LogTraceUSB
LogAll = LogError | LogInfo | LogDebug | LogTraceAll
LogTraceAll = LogTraceIPP | LogTraceESCL | LogTraceHTTP | LogTraceUSB
)
// Adjust LogLevel mask, so more detailed log levels
// imply less detailed
func (levels *LogLevel) Adjust() {
switch {
case *levels&LogTraceAll != 0:
*levels |= LogDebug | LogInfo | LogError
case *levels&LogDebug != 0:
*levels |= LogInfo | LogError
case *levels&LogInfo != 0:
*levels |= LogError
}
}
// loggerMode enumerates possible Logger modes
type loggerMode int
const (
loggerNoMode loggerMode = iota // Mode not yet set; log is buffered
loggerDiscard // Log goes to nowhere
loggerConsole // Log goes to console
loggerColorConsole // Log goes to console and uses ANSI colors
loggerFile // Log goes to disk file
)
// Logger implements logging facilities
type Logger struct {
LogMessage // "Root" log message
levels LogLevel // Levels generated by this logger
ccLevels LogLevel // Sum of Cc's levels
paused int32 // Logger paused, if counter > 0
mode loggerMode // Logger mode
lock sync.Mutex // Write lock
path string // Path to log file
cc []*Logger // Loggers to send carbon copy to
out io.Writer // Output stream, may be *os.File
outhook func(io.Writer, // Output hook
LogLevel, []byte)
// Don't reexport these methods from the root message
Commit, Flush, Reject struct{}
}
// NewLogger creates new logger. Logger mode is not set,
// so logs written to this logger a buffered until mode
// (and direction) is set
func NewLogger() *Logger {
l := &Logger{
mode: loggerNoMode,
levels: LogAll,
ccLevels: 0,
outhook: func(w io.Writer, _ LogLevel, line []byte) {
w.Write(line)
},
}
l.LogMessage.logger = l
return l
}
// ToNowhere redirects log to nowhere
func (l *Logger) ToNowhere() *Logger {
l.mode = loggerDiscard
l.out = ioutil.Discard
return l
}
// ToConsole redirects log to console
func (l *Logger) ToConsole() *Logger {
l.mode = loggerConsole
l.out = os.Stdout
return l
}
// ToColorConsole redirects log to console with ANSI colors
func (l *Logger) ToColorConsole() *Logger {
if logIsAtty(os.Stdout) {
l.outhook = logColorConsoleWrite
}
return l.ToConsole()
}
// ToStdOutErr redirects log to Stdout or Stderr, depending
// on LogLevel
func (l *Logger) ToStdOutErr() *Logger {
l.outhook = func(out io.Writer, level LogLevel, line []byte) {
if level == LogError {
out = os.Stderr
}
out.Write(line)
}
return l.ToConsole()
}
// ToFile redirects log to arbitrary log file
func (l *Logger) ToFile(path string) *Logger {
l.path = path
l.mode = loggerFile
l.out = nil // Will be opened on demand
return l
}
// ToMainFile redirects log to the main log file
func (l *Logger) ToMainFile() *Logger {
return l.ToFile(PathLogFile)
}
// ToDevFile redirects log to per-device log file
func (l *Logger) ToDevFile(info UsbDeviceInfo) *Logger {
return l.ToFile(filepath.Join(PathLogDir, info.Ident()+".log"))
}
// Cc adds Logger to send "carbon copy" to.
func (l *Logger) Cc(to *Logger) *Logger {
l.cc = append(l.cc, to)
l.ccLevels |= to.levels
return l
}
// Close the logger
func (l *Logger) Close() {
if l.mode == loggerFile && l.out != nil {
if file, ok := l.out.(*os.File); ok {
file.Close()
}
}
}
// SetLevels set logger's log levels
func (l *Logger) SetLevels(levels LogLevel) *Logger {
levels.Adjust()
l.levels = levels
return l
}
// Pause the logger. All output will be buffered,
// and flushed to destination when logger is resumed
func (l *Logger) Pause() *Logger {
atomic.AddInt32(&l.paused, 1)
return l
}
// Resume the logger. All buffered output will be
// flushed
func (l *Logger) Resume() *Logger {
if atomic.AddInt32(&l.paused, -1) == 0 {
l.LogMessage.Flush()
}
return l
}
// Panic writes to log a panic message, including
// call stack, and terminates a program
func (l *Logger) Panic(v interface{}) {
l.Error('!', "panic: %v", v)
l.Error('!', "")
w := l.LineWriter(LogError, '!')
w.Write(debug.Stack())
w.Close()
os.Exit(1)
}
// Format a time prefix
func (l *Logger) fmtTime() *logLineBuf {
buf := logLineBufAlloc(0, 0)
if l.mode == loggerFile {
now := time.Now()
year, month, day := now.Date()
hour, min, sec := now.Clock()
fmt.Fprintf(buf, "%2.2d-%2.2d-%4.4d %2.2d:%2.2d:%2.2d:",
day, month, year,
hour, min, sec)
}
return buf
}
// Handle log rotation
func (l *Logger) rotate() {
// Do we need to rotate?
file, ok := l.out.(*os.File)
if !ok {
return
}
stat, err := file.Stat()
if err != nil || stat.Size() <= Conf.LogMaxFileSize {
return
}
// Perform rotation
if Conf.LogMaxBackupFiles > 0 {
prevpath := ""
for i := Conf.LogMaxBackupFiles; i > 0; i-- {
nextpath := fmt.Sprintf("%s.%d.gz", l.path, i-1)
if i == Conf.LogMaxBackupFiles {
os.Remove(nextpath)
} else {
os.Rename(nextpath, prevpath)
}
prevpath = nextpath
}
err := l.gzip(l.path, prevpath)
if err != nil {
return
}
}
file.Truncate(0)
}
// gzip the log file
func (l *Logger) gzip(ipath, opath string) error {
// Open input file
ifile, err := os.Open(ipath)
if err != nil {
return err
}
defer ifile.Close()
// Open output file
ofile, err := os.OpenFile(opath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return err
}
// gzip ifile->ofile
w := gzip.NewWriter(ofile)
_, err = io.Copy(w, ifile)
err2 := w.Close()
err3 := ofile.Close()
switch {
case err == nil && err2 != nil:
err = err2
case err == nil && err3 != nil:
err = err3
}
// Cleanup and exit
if err != nil {
os.Remove(opath)
}
return err
}
// LogMessage represents a single (possible multi line) log
// message, which will appear in the output log atomically,
// and will be not interrupted in the middle by other log activity
type LogMessage struct {
logger *Logger // Underlying logger
parent *LogMessage // Parent message
lines []*logLineBuf // One buffer per line
}
// logMessagePool manages a pool of reusable LogMessages
var logMessagePool = sync.Pool{New: func() interface{} { return &LogMessage{} }}
// Begin returns a child (nested) LogMessage. Writes to this
// child message appended to the parent message
func (msg *LogMessage) Begin() *LogMessage {
msg2 := logMessagePool.Get().(*LogMessage)
msg2.logger = msg.logger
msg2.parent = msg
return msg2
}
// Add formats a next line of log message, with level and prefix char
func (msg *LogMessage) Add(level LogLevel, prefix byte,
format string, args ...interface{}) *LogMessage {
if (msg.logger.levels|msg.logger.ccLevels)&level != 0 {
buf := logLineBufAlloc(level, prefix)
fmt.Fprintf(buf, format, args...)
msg.appendLineBuf(buf)
}
return msg
}
// Nl adds empty line to the log message
func (msg *LogMessage) Nl(level LogLevel) *LogMessage {
return msg.Add(level, ' ', "")
}
// addBytes adds a next line of log message, taking slice of bytes as input
func (msg *LogMessage) addBytes(level LogLevel, prefix byte, line []byte) *LogMessage {
if (msg.logger.levels|msg.logger.ccLevels)&level != 0 {
buf := logLineBufAlloc(level, prefix)
buf.Write(line)
msg.appendLineBuf(buf)
}
return msg
}
// appendLineBuf appends line buffer to msg.lines
func (msg *LogMessage) appendLineBuf(buf *logLineBuf) {
if msg.parent == nil {
// Note, many threads may write to the root
// message simultaneously
msg.logger.lock.Lock()
msg.lines = append(msg.lines, buf)
msg.logger.lock.Unlock()
msg.Flush()
} else {
msg.lines = append(msg.lines, buf)
}
}
// Debug appends a LogDebug line to the message
func (msg *LogMessage) Debug(prefix byte, format string, args ...interface{}) *LogMessage {
return msg.Add(LogDebug, prefix, format, args...)
}
// Info appends a LogInfo line to the message
func (msg *LogMessage) Info(prefix byte, format string, args ...interface{}) *LogMessage {
return msg.Add(LogInfo, prefix, format, args...)
}
// Error appends a LogError line to the message
func (msg *LogMessage) Error(prefix byte, format string, args ...interface{}) *LogMessage {
return msg.Add(LogError, prefix, format, args...)
}
// Exit appends a LogError line to the message, flushes the message and
// all its parents and terminates a program by calling os.Exit(1)
func (msg *LogMessage) Exit(prefix byte, format string, args ...interface{}) {
if msg.logger.mode == loggerNoMode {
msg.logger.ToConsole()
}
msg.Error(prefix, format, args...)
for msg.parent != nil {
msg.Flush()
msg = msg.parent
}
os.Exit(1)
}
// Check calls msg.Exit(), if err is not nil
func (msg *LogMessage) Check(err error) {
if err != nil {
msg.Exit(0, "%s", err)
}
}
// HexDump appends a HEX dump to the log message
func (msg *LogMessage) HexDump(level LogLevel, prefix byte,
data []byte) *LogMessage {
if (msg.logger.levels|msg.logger.ccLevels)&level == 0 {
return msg
}
hex := logLineBufAlloc(0, 0)
chr := logLineBufAlloc(0, 0)
defer hex.free()
defer chr.free()
off := 0
for len(data) > 0 {
hex.Reset()
chr.Reset()
sz := len(data)
if sz > 16 {
sz = 16
}
i := 0
for ; i < sz; i++ {
c := data[i]
fmt.Fprintf(hex, "%2.2x", data[i])
if i%4 == 3 {
hex.Write([]byte(":"))
} else {
hex.Write([]byte(" "))
}
if 0x20 <= c && c < 0x80 {
chr.WriteByte(c)
} else {
chr.WriteByte('.')
}
}
for ; i < 16; i++ {
hex.WriteString(" ")
}
msg.Add(level, prefix, "%4.4x: %s %s", off, hex, chr)
off += sz
data = data[sz:]
}
return msg
}
// HTTPRequest dumps HTTP request (except body) to the log message
func (msg *LogMessage) HTTPRequest(level LogLevel, prefix byte,
session int, rq *http.Request) *LogMessage {
if (msg.logger.levels|msg.logger.ccLevels)&level == 0 {
return msg
}
// Clone request, drop body
rq = rq.WithContext(context.Background())
rq.Body = struct{ io.ReadCloser }{http.NoBody}
// Write it to the log
msg.Add(level, prefix, "HTTP[%3.3d]: HTTP request header:", session)
buf := &bytes.Buffer{}
rq.Write(buf)
for _, l := range bytes.Split(buf.Bytes(), []byte("\n")) {
if sz := len(l); sz > 0 && l[sz-1] == '\r' {
l = l[:sz-1]
}
msg.Add(level, prefix, " %s", l)
if len(l) == 0 {
break
}
}
return msg
}
// HTTPResponse dumps HTTP response (expect body) to the log message
func (msg *LogMessage) HTTPResponse(level LogLevel, prefix byte,
session int, rsp *http.Response) *LogMessage {
if (msg.logger.levels|msg.logger.ccLevels)&level == 0 {
return msg
}
// Clone response header. Avoid rsp.Header.Clone(),
// because Go 11 doesn't support it yet
hdr := make(http.Header, len(rsp.Header))
for k, v := range rsp.Header {
hdr[k] = v
}
// Go stdlib strips Transfer-Encoding header, so reconstruct it
if rsp.TransferEncoding != nil {
hdr.Add("Transfer-Encoding",
strings.Join(rsp.TransferEncoding, ", "))
}
// Write it to the log
msg.Add(level, prefix, "HTTP[%3.3d]: HTTP response header:", session)
msg.Add(level, prefix, " %s %s", rsp.Proto, rsp.Status)
keys := make([]string, 0, len(hdr))
for k := range hdr {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
msg.Add(level, prefix, " %s: %s", k, hdr.Get(k))
}
msg.Add(level, prefix, " ")
return msg
}
// HTTPRqParams dumps HTTP request parameters into the log message
func (msg *LogMessage) HTTPRqParams(level LogLevel, prefix byte,
session int, rq *http.Request) *LogMessage {
msg.Add(level, prefix, "HTTP[%3.3d]: %s %s", session, rq.Method, rq.URL)
return msg
}
// HTTPRspStatus dumps HTTP response status into the log message
func (msg *LogMessage) HTTPRspStatus(level LogLevel, prefix byte,
session int, rq *http.Request, rsp *http.Response) *LogMessage {
msg.Add(level, prefix, "HTTP[%3.3d]: %s %s - %s",
session, rq.Method, rq.URL, rsp.Status)
return msg
}
// HTTPError writes HTTP error into the log message
func (msg *LogMessage) HTTPError(prefix byte,
session int, format string, args ...interface{}) *LogMessage {
msg.Error(prefix, "HTTP[%3.3d]: %s", session, fmt.Sprintf(format, args...))
return msg
}
// HTTPDebug writes HTTP debug line into the log message
func (msg *LogMessage) HTTPDebug(prefix byte,
session int, format string, args ...interface{}) *LogMessage {
msg.Debug(prefix, "HTTP[%3.3d]: %s", session, fmt.Sprintf(format, args...))
return msg
}
// IppRequest dumps IPP request into the log message
func (msg *LogMessage) IppRequest(level LogLevel, prefix byte,
m *goipp.Message) *LogMessage {
if (msg.logger.levels|msg.logger.ccLevels)&level != 0 {
m.Print(msg.LineWriter(level, prefix), true)
}
return msg
}
// IppResponse dumps IPP response into the log message
func (msg *LogMessage) IppResponse(level LogLevel, prefix byte,
m *goipp.Message) *LogMessage {
if (msg.logger.levels|msg.logger.ccLevels)&level != 0 {
m.Print(msg.LineWriter(level, prefix), false)
}
return msg
}
// LineWriter creates a LineWriter that writes to the LogMessage,
// using specified LogLevel and prefix
func (msg *LogMessage) LineWriter(level LogLevel, prefix byte) *LineWriter {
return &LineWriter{
Func: func(line []byte) { msg.addBytes(level, prefix, line) },
}
}
// Commit message to the log
func (msg *LogMessage) Commit() {
msg.Flush()
msg.free()
}
// Flush message content to the log
//
// This is equal to committing the message and starting
// the new message, with the exception that old message
// pointer remains valid. Message logical atomicity is not
// preserved between flushes
func (msg *LogMessage) Flush() {
// Lock the logger
msg.logger.lock.Lock()
defer msg.logger.lock.Unlock()
// Ignore empty messages
if len(msg.lines) == 0 {
return
}
// If message has a parent, simply flush our content there
if msg.parent != nil {
msg.parent.lines = append(msg.parent.lines, msg.lines...)
msg.lines = msg.lines[:0]
// If our parent is root, we need to flush root as well
if msg.parent.parent == nil {
msg = msg.parent
} else {
return
}
}
// Do nothing, if logger is paused
if atomic.LoadInt32(&msg.logger.paused) != 0 {
return
}
// Open log file on demand
if msg.logger.out == nil && msg.logger.mode == loggerFile {
os.MkdirAll(PathLogDir, 0755)
msg.logger.out, _ = os.OpenFile(msg.logger.path,
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}
if msg.logger.out == nil {
return
}
// Rotate now
if msg.logger.mode == loggerFile {
msg.logger.rotate()
}
// Prepare to carbon-copy
var cclist []struct {
levels LogLevel
msg *LogMessage
}
for _, cc := range msg.logger.cc {
cclist = append(cclist, struct {
levels LogLevel
msg *LogMessage
}{cc.levels, cc.Begin()})
}
// Send message content to the logger
buf := msg.logger.fmtTime()
defer buf.free()
timeLen := buf.Len()
for _, l := range msg.lines {
l.trim()
// Generate own output
buf.Truncate(timeLen)
if l.level&msg.logger.levels != 0 {
if !l.empty() {
if timeLen != 0 {
buf.WriteByte(' ')
}
buf.Write(l.Bytes())
}
buf.WriteByte('\n')
msg.logger.outhook(msg.logger.out, l.level, buf.Bytes())
}
// Send carbon copies
for _, cc := range cclist {
if (cc.levels & l.level) != 0 {
cc.msg.addBytes(l.level, 0, l.Bytes())
}
}
l.free()
}
// Commit carbon copies
for _, cc := range cclist {
cc.msg.Commit()
}
// Reset the message
msg.lines = msg.lines[:0]
}
// Reject the message
func (msg *LogMessage) Reject() {
msg.free()
}
// Return message to the logMessagePool
func (msg *LogMessage) free() {
// Free all lines
for _, l := range msg.lines {
l.free()
}
// Reset the message and put it to the pool
if len(msg.lines) < 16 {
msg.lines = msg.lines[:0] // Keep memory, reset content
} else {
msg.lines = nil // Drop this large buffer
}
msg.logger = nil
logMessagePool.Put(msg)
}
// logLineBuf represents a single log line buffer
type logLineBuf struct {
bytes.Buffer // Underlying buffer
level LogLevel // Log level the line was written on
}
// logLinePool manages a pool of reusable logLines
var logLineBufPool = sync.Pool{New: func() interface{} {
return &logLineBuf{
Buffer: bytes.Buffer{},
}
}}
// logLineAlloc() allocates a logLineBuf
func logLineBufAlloc(level LogLevel, prefix byte) *logLineBuf {
buf := logLineBufPool.Get().(*logLineBuf)
buf.level = level
if prefix != 0 {
buf.Write([]byte{prefix, ' '})
}
return buf
}
// free returns the logLineBuf to the pool
func (buf *logLineBuf) free() {
if buf.Cap() <= 256 {
buf.Reset()
logLineBufPool.Put(buf)
}
}
// trim removes trailing spaces
func (buf *logLineBuf) trim() {
bytes := buf.Bytes()
var i int
loop:
for i = len(bytes); i > 0; i-- {
c := bytes[i-1]
switch c {
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
default:
break loop
}
}
buf.Truncate(i)
}
// empty returns true if logLineBuf is empty (no text, no prefix)
func (buf *logLineBuf) empty() bool {
return buf.Len() == 0
}