-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
347 lines (302 loc) · 8.58 KB
/
output.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
package logng
import (
"bytes"
"fmt"
"io"
"sync"
"sync/atomic"
"unsafe"
)
// Output is an interface for Logger output.
// All of Output implementations must be safe for concurrency.
type Output interface {
Log(log *Log)
}
type multiOutput []Output
func (o multiOutput) Log(log *Log) {
for _, o1 := range o {
o1.Log(log)
}
}
// MultiOutput creates an output that clones its logs to all the provided outputs.
func MultiOutput(outputs ...Output) Output {
o := make(multiOutput, len(outputs))
copy(o, outputs)
return o
}
// QueuedOutput is intermediate Output implementation between Logger and given Output.
// QueuedOutput has queueing for unblocking Log() method.
type QueuedOutput struct {
output Output
queue chan *Log
closing int32
wg sync.WaitGroup
logWg sync.WaitGroup
blocking uint32
onQueueFull *func()
}
// NewQueuedOutput creates a new QueuedOutput by the given output.
func NewQueuedOutput(output Output, queueLen int) (o *QueuedOutput) {
o = &QueuedOutput{
output: output,
queue: make(chan *Log, queueLen),
}
o.wg.Add(1)
go o.worker()
return
}
// Close stops accepting new logs to the underlying QueuedOutput and waits for the queue to empty.
// Unused QueuedOutput must be closed for freeing resources.
func (o *QueuedOutput) Close() error {
if !atomic.CompareAndSwapInt32(&o.closing, 0, 1) {
return nil
}
o.logWg.Wait()
close(o.queue)
o.wg.Wait()
return nil
}
// Log is the implementation of Output.
// If blocking is true, Log method blocks execution until the underlying output has finished execution.
// Otherwise, Log method sends the log to the queue if the queue is available.
// When the queue is full, it tries to call OnQueueFull function.
func (o *QueuedOutput) Log(log *Log) {
o.logWg.Add(1)
defer o.logWg.Done()
if o.closing != 0 {
return
}
if o.blocking != 0 {
o.queue <- log
return
}
select {
case o.queue <- log:
default:
onQueueFull := o.onQueueFull
if onQueueFull != nil && *onQueueFull != nil {
(*onQueueFull)()
}
}
}
// SetBlocking sets QueuedOutput behavior when the queue is full.
// It returns the underlying QueuedOutput.
func (o *QueuedOutput) SetBlocking(blocking bool) *QueuedOutput {
var b uint32
if blocking {
b = 1
}
atomic.StoreUint32(&o.blocking, b)
return o
}
// SetOnQueueFull sets a function to call when the queue is full.
// It returns the underlying QueuedOutput.
func (o *QueuedOutput) SetOnQueueFull(f func()) *QueuedOutput {
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&o.onQueueFull)), unsafe.Pointer(&f))
return o
}
func (o *QueuedOutput) worker() {
defer o.wg.Done()
for log := range o.queue {
o.output.Log(log)
}
}
// TextOutput is an implementation of Output by writing texts to io.Writer w.
type TextOutput struct {
mu sync.RWMutex
w io.Writer
flags TextOutputFlag
onError *func(error)
}
// NewTextOutput creates a new TextOutput.
func NewTextOutput(w io.Writer, flags TextOutputFlag) *TextOutput {
return &TextOutput{
w: w,
flags: flags,
}
}
// Log is the implementation of Output.
func (o *TextOutput) Log(log *Log) {
var err error
defer func() {
onError := o.onError
if err == nil || onError == nil || *onError == nil {
return
}
(*onError)(err)
}()
o.mu.RLock()
defer o.mu.RUnlock()
buf := bytes.NewBuffer(make([]byte, 0, 4096))
if o.flags&(TextOutputFlagDate|TextOutputFlagTime|TextOutputFlagMicroseconds) != 0 {
tm := log.Time.Local()
if o.flags&TextOutputFlagUTC != 0 {
tm = tm.UTC()
}
b := make([]byte, 0, 128)
if o.flags&TextOutputFlagDate != 0 {
year, month, day := tm.Date()
itoa(&b, year, 4)
b = append(b, '/')
itoa(&b, int(month), 2)
b = append(b, '/')
itoa(&b, day, 2)
b = append(b, ' ')
}
if o.flags&(TextOutputFlagTime|TextOutputFlagMicroseconds) != 0 {
hour, min, sec := tm.Clock()
itoa(&b, hour, 2)
b = append(b, ':')
itoa(&b, min, 2)
b = append(b, ':')
itoa(&b, sec, 2)
if o.flags&TextOutputFlagMicroseconds != 0 {
b = append(b, '.')
itoa(&b, log.Time.Nanosecond()/1e3, 6)
}
b = append(b, ' ')
}
buf.Write(b)
}
if o.flags&TextOutputFlagSeverity != 0 {
buf.WriteString(log.Severity.String())
buf.WriteString(" - ")
}
var padding []byte
if o.flags&TextOutputFlagPadding != 0 {
padding = bytes.Repeat([]byte(" "), buf.Len())
}
if o.flags&(TextOutputFlagLongFunc|TextOutputFlagShortFunc) != 0 {
fn := "???"
if log.StackCaller.Function != "" {
fn = log.StackCaller.Function
}
if o.flags&TextOutputFlagShortFunc != 0 {
fn = trimDirs(fn)
}
buf.WriteString(fn)
buf.WriteString("()")
buf.WriteString(" - ")
}
if o.flags&(TextOutputFlagLongFile|TextOutputFlagShortFile) != 0 {
file, line := "???", 0
if log.StackCaller.File != "" {
file = log.StackCaller.File
if o.flags&TextOutputFlagShortFile != 0 {
file = trimDirs(file)
}
}
if log.StackCaller.Line > 0 {
line = log.StackCaller.Line
}
buf.WriteString(file)
buf.WriteRune(':')
b := make([]byte, 0, 128)
itoa(&b, line, -1)
buf.Write(b)
buf.WriteString(" - ")
}
for idx, line := range bytes.Split(log.Message, []byte("\n")) {
if idx > 0 {
buf.Write(padding)
}
buf.Write(line)
buf.WriteRune('\n')
}
extended := false
extend := func() {
if !extended {
extended = true
buf.WriteString("\t\n")
}
}
if o.flags&TextOutputFlagFields != 0 && len(log.Fields) > 0 {
extend()
buf.WriteRune('\t')
buf.WriteString("+ ")
for idx, field := range log.Fields {
if idx > 0 {
buf.WriteRune(' ')
}
buf.WriteString(fmt.Sprintf("%q=%q", field.Key, fmt.Sprintf("%v", field.Value)))
}
buf.WriteString("\n\t")
buf.WriteRune('\n')
}
if o.flags&(TextOutputFlagStackTrace|TextOutputFlagStackTraceShortFile) != 0 && log.StackTrace != nil {
extend()
f := "%+1.1s"
if o.flags&TextOutputFlagStackTraceShortFile != 0 {
f = "%+#1.1s"
}
buf.WriteString(fmt.Sprintf(f, log.StackTrace))
buf.WriteString("\n\t")
buf.WriteRune('\n')
}
_, err = io.Copy(o.w, buf)
if err != nil {
err = fmt.Errorf("unable to write to writer: %w", err)
return
}
}
// SetWriter sets writer.
// It returns the underlying TextOutput.
func (o *TextOutput) SetWriter(w io.Writer) *TextOutput {
o.mu.Lock()
defer o.mu.Unlock()
o.w = w
return o
}
// SetFlags sets flags to override every single Log.Flags if argument flags is different from 0.
// It returns the underlying TextOutput.
func (o *TextOutput) SetFlags(flags TextOutputFlag) *TextOutput {
o.mu.Lock()
defer o.mu.Unlock()
o.flags = flags
return o
}
// SetOnError sets a function to call when error occurs.
// It returns the underlying TextOutput.
func (o *TextOutput) SetOnError(f func(error)) *TextOutput {
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&o.onError)), unsafe.Pointer(&f))
return o
}
// TextOutputFlag holds single or multiple flags of TextOutput.
// A TextOutput instance uses these flags which are stored by TextOutputFlag type.
type TextOutputFlag int
const (
// TextOutputFlagDate prints the date in the local time zone: 2009/01/23.
TextOutputFlagDate TextOutputFlag = 1 << iota
// TextOutputFlagTime prints the time in the local time zone: 01:23:23.
TextOutputFlagTime
// TextOutputFlagMicroseconds prints microsecond resolution: 01:23:23.123123.
// assumes TextOutputFlagTime.
TextOutputFlagMicroseconds
// TextOutputFlagUTC uses UTC rather than the local time zone if TextOutputFlagDate or TextOutputFlagTime is set.
TextOutputFlagUTC
// TextOutputFlagSeverity prints the severity.
TextOutputFlagSeverity
// TextOutputFlagPadding prints padding with multiple lines.
TextOutputFlagPadding
// TextOutputFlagLongFunc prints full package name and function name: a/b/c/d.Func1().
TextOutputFlagLongFunc
// TextOutputFlagShortFunc prints final package name and function name: d.Func1().
// overrides TextOutputFlagLongFunc.
TextOutputFlagShortFunc
// TextOutputFlagLongFile prints full file name and line number: a/b/c/d.go:23.
TextOutputFlagLongFile
// TextOutputFlagShortFile prints final file name element and line number: d.go:23.
// overrides TextOutputFlagLongFile.
TextOutputFlagShortFile
// TextOutputFlagFields prints fields if given.
TextOutputFlagFields
// TextOutputFlagStackTrace prints the stack trace if given.
TextOutputFlagStackTrace
// TextOutputFlagStackTraceShortFile prints with file name element only.
// assumes TextOutputFlagStackTrace.
TextOutputFlagStackTraceShortFile
// TextOutputFlagDefault holds predefined default flags.
// it used by the default Logger.
TextOutputFlagDefault = TextOutputFlagDate | TextOutputFlagTime | TextOutputFlagSeverity |
TextOutputFlagPadding | TextOutputFlagFields | TextOutputFlagStackTraceShortFile
)