-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
175 lines (153 loc) · 3.33 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
// util/log.go
// Copyright(c) 2017 Matt Pharr
// BSD licensed; see LICENSE for details.
package util
import (
"fmt"
"io"
"os"
"path"
"runtime"
"strings"
"sync"
)
// Logger provides a simple logging system with a few different log levels;
// debugging and verbose output may both be suppressed independently.
type Logger struct {
NErrors int
mu sync.Mutex
debug io.Writer
verbose io.Writer
warning io.Writer
err io.Writer
}
func NewLogger(verbose, debug bool) *Logger {
l := &Logger{}
if verbose {
l.verbose = os.Stderr
}
if debug {
l.debug = os.Stderr
}
l.warning = os.Stderr
l.err = os.Stderr
return l
}
func (l *Logger) Print(f string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "%s", format(f, args...))
}
func (l *Logger) Debug(f string, args ...interface{}) {
if l == nil {
fmt.Fprintf(os.Stderr, format(f, args...))
return
}
if l.debug == nil {
return
}
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprint(l.debug, format(f, args...))
}
func (l *Logger) Verbose(f string, args ...interface{}) {
if l == nil {
fmt.Fprintf(os.Stderr, format(f, args...))
return
}
if l.verbose == nil {
return
}
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprint(l.verbose, format(f, args...))
}
func (l *Logger) Warning(f string, args ...interface{}) {
if l == nil {
fmt.Fprintf(os.Stderr, format(f, args...))
return
}
l.mu.Lock()
defer l.mu.Unlock()
fmt.Fprint(l.warning, format(f, args...))
}
func (l *Logger) Error(f string, args ...interface{}) {
if l == nil {
fmt.Fprintf(os.Stderr, format(f, args...))
return
}
l.mu.Lock()
defer l.mu.Unlock()
l.NErrors++
fmt.Fprint(l.err, format(f, args...))
}
func (l *Logger) Fatal(f string, args ...interface{}) {
if l == nil {
fmt.Fprintf(os.Stderr, format(f, args...))
return
}
l.mu.Lock()
defer l.mu.Unlock()
l.NErrors++
fmt.Fprint(l.err, format(f, args...))
os.Exit(1)
}
// Checks the provided condition and prints a fatal error if it's false.
// The error message includes the source file and line number where the
// check failed. An optional message specified with printf-style
// formatting may be provided to print with the error message.
func (l *Logger) Check(v bool, msg ...interface{}) {
if v {
return
}
var w io.Writer
if l != nil {
l.mu.Lock()
defer l.mu.Unlock()
l.NErrors++
w = l.err
} else {
w = os.Stderr
}
if len(msg) == 0 {
fmt.Fprint(w, format("Check failed\n"))
} else {
f := msg[0].(string)
fmt.Fprint(w, format(f, msg[1:]...))
}
os.Exit(1)
}
// Similar to Check, CheckError prints a fatal error if the given error is
// non-nil. It also takes an optional format string.
func (l *Logger) CheckError(err error, msg ...interface{}) {
if err == nil {
return
}
var w io.Writer
if l != nil {
l.mu.Lock()
defer l.mu.Unlock()
l.NErrors++
w = l.err
} else {
w = os.Stderr
}
if len(msg) == 0 {
fmt.Fprint(w, format("Error: %+v\n", err))
} else {
f := msg[0].(string)
fmt.Fprint(w, format(f, msg[1:]...))
}
panic(err)
os.Exit(1)
}
func format(f string, args ...interface{}) string {
// Two levels up the call stack
_, fn, line, _ := runtime.Caller(2)
// Last two components of the path
fnline := path.Base(path.Dir(fn)) + "/" + path.Base(fn) + fmt.Sprintf(":%d", line)
s := fmt.Sprintf("%-25s: ", fnline)
s += fmt.Sprintf(f, args...)
if !strings.HasSuffix(s, "\n") {
s += "\n"
}
return s
}