-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdwrapper.go
67 lines (55 loc) · 1.41 KB
/
stdwrapper.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
// stdwrapper.go - wrapper around my logger to make it compatible
// with stdlib log.Logger.
//
// Changes Copyright 2012, Sudhi Herle <sudhi -at- herle.net>
// This code is licensed under the same terms as the golang core.
package logger
import (
stdlog "log"
)
func fl2std(flag int) int {
fl := stdlog.LUTC
if 0 != (flag & Ldate) {
fl |= stdlog.Ldate
}
if 0 != (flag & Ltime) {
fl |= stdlog.Ltime
}
if 0 != (flag & Lmicroseconds) {
fl |= stdlog.Lmicroseconds
}
if 0 != (flag & Lfileloc) {
if 0 != (flag & Lfullpath) {
fl |= stdlog.Llongfile
} else {
fl |= stdlog.Lshortfile
}
}
return fl
}
// Return an instance of self that satisfies stdlib logger
func (l *xLogger) StdLogger() *stdlog.Logger {
var g *stdlog.Logger
if g = l.stdlogger.Load(); g == nil {
// here first argument 'l' is the io.Writer; we provide its
// interface implementation below.
g = stdlog.New(l, l.prefix, fl2std(l.flag))
if !l.stdlogger.CompareAndSwap(nil, g) {
g = l.stdlogger.Load()
}
}
return g
}
// We only provide an ioWriter implementation for stdlogger
func (l *xLogger) Write(b []byte) (int, error) {
l.qwrite(b)
return len(b), nil
}
// provide implementations for the nul logger as well
func (e *emptyLogger) StdLogger() *stdlog.Logger {
return stdlog.New(e, e.prefix, fl2std(0))
}
func (e *emptyLogger) Write(b []byte) (int, error) {
return len(b), nil
}
// vim: ft=go:sw=8:ts=8:noexpandtab:tw=98: