-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
80 lines (61 loc) · 1.32 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
package main
import (
"fmt"
"io"
"os"
"strings"
"golang.org/x/sys/unix"
)
var (
sugar Logger
maxLogLines = 1024
)
type Logger struct {
Buffer []string
c chan string
f io.ReadWriter
}
func NewLogger(kmesgF string) (l Logger, err error) {
l.Buffer = make([]string, maxLogLines)
l.c = make(chan string)
l.f, err = os.OpenFile(kmesgF, os.O_RDWR|unix.O_CLOEXEC|unix.O_NONBLOCK|unix.O_NOCTTY, 0o666) // #nosec: G302,G304
if err != nil {
return
}
go l.Start()
return
}
func (l *Logger) Start() {
for msg := range l.c {
if l.f != nil {
fmt.Fprint(l.f, msg)
}
l.Buffer = append([]string{msg}, l.Buffer[:maxLogLines-1]...)
}
}
func (l Logger) Infow(msg string, kvs ...interface{}) {
l.addLog("info", msg, kvs...)
}
func (l Logger) Errorw(msg string, kvs ...interface{}) {
l.addLog("error", msg, kvs...)
}
func (l Logger) Warnw(msg string, kvs ...interface{}) {
l.addLog("warn", msg, kvs...)
}
func (l Logger) addLog(level, msg string, kvs ...interface{}) {
elems := make([]string, 0)
elems = append(elems, fmt.Sprintf("vinit %s: %q", level, msg))
switch len(kvs) {
case 0, 2:
case 1:
kvs = nil
default:
if len(kvs)%2 != 0 {
kvs = kvs[:len(kvs)-1]
}
}
for i := 0; i < len(kvs); i += 2 {
elems = append(elems, fmt.Sprintf(`%v="%v"`, kvs[i], kvs[i+1]))
}
l.c <- strings.Join(elems, ", ")
}