-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlog.go
250 lines (210 loc) · 5.66 KB
/
dlog.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
// Package log is a wrapper around standard go logger that adds the debug output
// functions.
package dlog
import (
"context"
"fmt"
"io"
"log"
"os"
"sync"
)
type Logger struct {
*log.Logger
debug bool
mu sync.Mutex
}
var std *Logger
type key int
var loggerKey key
func init() {
isDebug := (os.Getenv("DEBUG") != "")
flags := log.LstdFlags
if isDebug {
flags |= log.Lshortfile
}
std = New(os.Stderr, "", flags, isDebug)
}
func New(out io.Writer, prefix string, flag int, debug bool) *Logger {
l := Logger{Logger: log.New(out, prefix, flag), debug: false}
l.SetDebug(debug)
return &l
}
func (l *Logger) Debug(v ...interface{}) {
if l.Logger == nil {
l.Logger = defaultLogger()
}
if l.debug {
l.Output(2, fmt.Sprint(v...))
}
}
func (l *Logger) Debugln(v ...interface{}) {
if l.Logger == nil {
l.Logger = defaultLogger()
}
if l.debug {
l.Output(2, fmt.Sprintln(v...))
}
}
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.Logger == nil {
l.Logger = defaultLogger()
}
if l.debug {
l.Output(2, fmt.Sprintf(format, v...))
}
}
// NewContext returns a new Context that has logger attached.
func NewContext(ctx context.Context, l *Logger) context.Context {
return context.WithValue(ctx, loggerKey, l)
}
// FromContext returns the Logger value stored in ctx, if any. If no Logger
// is present, it returns the standard logger instance.
func FromContext(ctx context.Context) *Logger {
l, ok := ctx.Value(loggerKey).(*Logger)
if l == nil || !ok {
l = std
}
return l
}
// SetDebug sets/resets the debugging output.
func (l *Logger) SetDebug(b bool) {
if l.Logger == nil {
l.Logger = defaultLogger()
}
l.mu.Lock()
defer l.mu.Unlock()
l.debug = b
if b {
l.SetFlags(l.Flags() | log.Lshortfile)
} else {
l.SetFlags(l.Flags() &^ (1 << log.Lshortfile))
}
}
// IsDebug returns true if the debugging output is enabled.
func (l *Logger) IsDebug() bool {
return l.debug
}
// SetOutput sets the output destination for the standard logger.
func SetOutput(w io.Writer) {
std.Logger.SetOutput(w)
}
// Flags returns the output flags for the standard logger.
// The flag bits are Ldate, Ltime, and so on.
func Flags() int {
return std.Flags()
}
// SetFlags sets the output flags for the standard logger.
// The flag bits are Ldate, Ltime, and so on.
func SetFlags(flag int) {
std.SetFlags(flag)
}
// Prefix returns the output prefix for the standard logger.
func Prefix() string {
return std.Prefix()
}
// SetPrefix sets the output prefix for the standard logger.
func SetPrefix(prefix string) {
std.SetPrefix(prefix)
}
// SetDebug sets/resets the debugging output.
func SetDebug(b bool) {
std.SetDebug(b)
}
// IsDebug returns true if the debugging output is enabled.
func IsDebug() bool {
return std.IsDebug()
}
func defaultLogger() *log.Logger {
return log.New(os.Stderr, "", log.LstdFlags)
}
// Writer returns the output destination for the standard logger.
func Writer() io.Writer {
return std.Writer()
}
// These functions write to the standard logger.
// Print calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
std.Print(v...)
}
// Printf calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
std.Output(2, fmt.Sprintf(format, v...))
}
// Println calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Println.
func Println(v ...interface{}) {
std.Output(2, fmt.Sprintln(v...))
}
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
func Fatal(v ...interface{}) {
std.Output(2, fmt.Sprint(v...))
os.Exit(1)
}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func Fatalf(format string, v ...interface{}) {
std.Output(2, fmt.Sprintf(format, v...))
os.Exit(1)
}
// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
func Fatalln(v ...interface{}) {
std.Output(2, fmt.Sprintln(v...))
os.Exit(1)
}
// Panic is equivalent to Print() followed by a call to panic().
func (l *Logger) Panic(v ...interface{}) {
s := fmt.Sprint(v...)
l.Output(2, s)
panic(s)
}
// Panicf is equivalent to Printf() followed by a call to panic().
func (l *Logger) Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
l.Output(2, s)
panic(s)
}
// Panicln is equivalent to Println() followed by a call to panic().
func (l *Logger) Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
l.Output(2, s)
panic(s)
}
// Output writes the output for a logging event. The string s contains
// the text to print after the prefix specified by the flags of the
// Logger. A newline is appended if the last character of s is not
// already a newline. Calldepth is the count of the number of
// frames to skip when computing the file name and line number
// if Llongfile or Lshortfile is set; a value of 1 will print the details
// for the caller of Output.
func Output(calldepth int, s string) error {
return std.Output(calldepth+1, s) // +1 for this frame.
}
func Debug(v ...interface{}) {
if std.debug {
std.Output(2, fmt.Sprint(v...))
}
}
func Debugf(format string, v ...interface{}) {
if std.debug {
std.Output(2, fmt.Sprintf(format, v...))
}
}
func Debugln(v ...interface{}) {
if std.debug {
std.Output(2, fmt.Sprintln(v...))
}
}
// Panic is equivalent to Print() followed by a call to panic().
func Panic(v ...interface{}) {
std.Panic(v...)
}
// Panicf is equivalent to Printf() followed by a call to panic().
func Panicf(format string, v ...interface{}) {
std.Panicf(format, v...)
}
// Panicln is equivalent to Println() followed by a call to panic().
func Panicln(v ...interface{}) {
std.Panicln(v...)
}