-
Notifications
You must be signed in to change notification settings - Fork 0
/
logchan.go
237 lines (200 loc) · 4.72 KB
/
logchan.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
package logchan
import (
"log"
"fmt"
"strings"
)
type Level uint64
type Channel struct {
Level Level
Key byte
Desc string
}
type Channels []Channel
const (
LOG_NONE Level = 0x0
LOG_DEBUG Level = 0x800000000000000
LOG_INFO Level = 0x1000000000000000
LOG_WARN Level = 0x2000000000000000
LOG_ERROR Level = 0x4000000000000000
LOG_FATAL Level = 0x8000000000000000
LOG_ALL Level = 0xFFFFFFFFFFFFFFFF
LOG_LEVEL_5 Level = LOG_FATAL
LOG_LEVEL_4 Level = LOG_ERROR | LOG_LEVEL_5
LOG_LEVEL_3 Level = LOG_WARN | LOG_LEVEL_4
LOG_LEVEL_2 Level = LOG_INFO | LOG_LEVEL_3
LOG_LEVEL_1 Level = LOG_DEBUG | LOG_LEVEL_2
)
const (
CHANNEL_NONE byte = '0'
CHANNEL_DEBUG byte = 'D'
CHANNEL_INFO byte = 'I'
CHANNEL_WARN byte = 'W'
CHANNEL_ERROR byte = 'E'
CHANNEL_FATAL byte = 'F'
CHANNEL_LEVEL_1 byte = '1'
CHANNEL_LEVEL_2 byte = '2'
CHANNEL_LEVEL_3 byte = '3'
CHANNEL_LEVEL_4 byte = '4'
CHANNEL_LEVEL_5 byte = '5'
CHANNEL_ALL byte = 'A'
)
var defaultChannels = []Channel {
Channel{LOG_NONE, CHANNEL_NONE, "none"},
Channel{LOG_DEBUG, CHANNEL_DEBUG, "debug"},
Channel{LOG_INFO, CHANNEL_INFO, "info"},
Channel{LOG_WARN, CHANNEL_WARN, "warn"},
Channel{LOG_ERROR, CHANNEL_ERROR, "error"},
Channel{LOG_FATAL, CHANNEL_FATAL, "fatal"},
Channel{LOG_ALL, CHANNEL_ALL, "all"}}
type Logger struct {
level Level
channels Channels
chanmap map[byte]Channel
bitmap map[Level]Channel
catchAll Channel
}
func NewLogger(ch Channels, def Level) *Logger {
ret := new(Logger)
ret.level = def
ret.channels = make(Channels, 1)
ret.chanmap = make(map[byte]Channel)
ret.bitmap = make(map[Level]Channel)
for _, c := range ch {
if c.Level == LOG_ALL {
ret.catchAll = c
} else {
ret.chanmap[c.Key] = c
ret.bitmap[c.Level] = c
ret.channels = append(ret.channels, c)
}
}
// add in the defaultChannels
for _, c := range defaultChannels {
if c.Level == LOG_ALL {
ret.catchAll = c
} else {
ret.chanmap[c.Key] = c
ret.bitmap[c.Level] = c
ret.channels = append(ret.channels, c)
}
}
return ret
}
func (logger *Logger) AddChannels(ch Channels) {
for _, c := range ch {
if c.Level == LOG_ALL {
logger.catchAll = c
} else {
logger.chanmap[c.Key] = c
logger.bitmap[c.Level] = c
logger.channels = append(logger.channels, c)
}
}
}
func (logger *Logger) AtLevel (l Level) bool {
return (l & logger.level) != 0
}
func (logger *Logger) LevelToPrefix (l Level) string {
s := logger.LevelToString(l)
if len(s) > 0 {
s = fmt.Sprintf("[%s] ", s)
}
return s
}
func (logger *Logger) LevelToString(l Level) string {
descs := make([]string,0)
for _, c := range logger.channels {
if (c.Level & l) != 0 {
descs = append (descs, c.Desc)
l = l&(^c.Level)
}
}
if len(descs) == 0 {
descs = append(descs, logger.catchAll.Desc)
}
return strings.Join(descs, ",")
}
func (logger *Logger) SetChannelsEasy (which string, s string, setIfEmpty bool) bool {
ret := true
if len(s) > 0 || setIfEmpty {
if s, e := logger.SetChannels(s); e == nil {
log.Printf("Setting %s logging to '%s'\n", which, s);
} else {
log.Printf("Failed to set %s logging: %s\n", which, e);
ret = false
}
}
return ret
}
func (logger *Logger) SetChannels (s string) (newdesc string, e error) {
var newlev Level = 0
for _, c := range []byte(s) {
if ch, found := logger.chanmap[c]; found {
newlev |= ch.Level
} else {
e = fmt.Errorf("bad logger channel found: '%c'", c)
break
}
}
if e == nil {
newdesc = logger.LevelToString(newlev)
logger.level = newlev
}
return
}
func (logger *Logger) Printf(l Level, fmt string, v ...interface{}) {
if logger.AtLevel (l) {
s := logger.LevelToPrefix (l)
if len(s) > 0 {
fmt = s + fmt
}
log.Printf (fmt, v...)
}
}
func (logger *Logger) Print(l Level, v ...interface{}) {
if logger.AtLevel (l) {
s := logger.LevelToPrefix (l)
if len(s) > 0 {
log.Print (s)
}
log.Print(v)
}
}
func (logger *Logger) Println(l Level, v ...interface{}) {
if logger.AtLevel (l) {
s := logger.LevelToPrefix (l)
if len(s) > 0 {
log.Print (s)
}
log.Println (v...)
}
}
var std = NewLogger(defaultChannels, LOG_NONE)
func AddChannels(chs Channels) {
std.AddChannels(chs)
}
func AtLevel (l Level) bool {
return (l & std.level) != 0
}
func LevelToPrefix (l Level) string {
return std.LevelToPrefix(l)
}
func LevelToString(l Level) string {
return std.LevelToString(l)
}
func SetChannelsEasy (which string, s string, setIfEmpty bool) bool {
return std.SetChannelsEasy(which, s, setIfEmpty)
}
func SetChannels (s string) (newdesc string, e error) {
return std.SetChannels(s)
}
func Printf(l Level, fmt string, v ...interface{}) {
std.Printf(l, fmt, v...)
}
func Print(l Level, v ...interface{}) {
std.Print(l, v...)
}
func Println(l Level, v ...interface{}) {
std.Println(l, v...)
}