forked from andradeandrey/fwk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgstream.go
75 lines (62 loc) · 1.66 KB
/
msgstream.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
package fwk
import (
"fmt"
"io"
"os"
)
// WriteSyncer is an io.Writer which can be sync'ed/flushed.
type WriteSyncer interface {
io.Writer
Sync() error
}
type msgstream struct {
lvl Level
w WriteSyncer
n string
}
// NewMsgStream creates a new MsgStream value with name name and minimum
// verbosity level lvl.
// This MsgStream will print messages into w.
func NewMsgStream(name string, lvl Level, w WriteSyncer) MsgStream {
return newMsgStream(name, lvl, w)
}
func newMsgStream(name string, lvl Level, w WriteSyncer) msgstream {
if w == nil {
w = os.Stdout
}
return msgstream{
lvl: lvl,
w: w,
n: fmt.Sprintf("%-20s ", name),
}
}
// Debugf displays a (formated) DBG message
func (msg msgstream) Debugf(format string, a ...interface{}) (int, error) {
return msg.Msg(LvlDebug, format, a...)
}
// Infof displays a (formated) INFO message
func (msg msgstream) Infof(format string, a ...interface{}) (int, error) {
return msg.Msg(LvlInfo, format, a...)
}
// Warnf displays a (formated) WARN message
func (msg msgstream) Warnf(format string, a ...interface{}) (int, error) {
defer msg.flush()
return msg.Msg(LvlWarning, format, a...)
}
// Errorf displays a (formated) ERR message
func (msg msgstream) Errorf(format string, a ...interface{}) (int, error) {
defer msg.flush()
return msg.Msg(LvlError, format, a...)
}
// Msg displays a (formated) message with level lvl.
func (msg msgstream) Msg(lvl Level, format string, a ...interface{}) (int, error) {
if lvl < msg.lvl {
return 0, nil
}
format = msg.n + msg.lvl.msgstring() + " " + format
return fmt.Fprintf(msg.w, format, a...)
}
func (msg msgstream) flush() error {
return msg.w.Sync()
}
// EOF