This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
golog.go
346 lines (306 loc) · 8.44 KB
/
golog.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
// The golog package is a logging framework for the Go language based on the
// go routine and channel features of the language. In essence, log
// messages sent to a logger are sent through a global channel where a
// go routine listens to and services each log write serially.
//
// As of right now, all messages are sent through a single logger channel,
// guranteeing serialization of log messages. In the future, golog will
// switch to the model of having a channel and go routine per resource
// (such as file, network, console, etc...) so that writes to any single
// resource are serialized, but writes to different resources can be
// parallelized.
//
package golog
import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
// ****************************************************************************
// Log Priority used to filter messages sent to the loggers.
//
type Priority int
const (
log_DISABLE Priority = iota - 1
// Using syslog standard priorities.
// From /usr/include/sys/syslog.h.
// These are the same on Linux, BSD, and OS X.
LOG_EMERG
LOG_ALERT
LOG_CRIT
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
)
func Priorities() []Priority {
return []Priority{
LOG_EMERG,
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG}
}
// If a priority is out of bounds given any input, we'll simply
// truncate it to the closest valid priority.
func BoundPriority(priority Priority) Priority {
if priority < log_DISABLE {
priority = log_DISABLE
} else if priority > LOG_DEBUG {
priority = LOG_DEBUG
}
return priority
}
func (p Priority) String() string {
switch p {
case LOG_EMERG:
return "EMERGENCY"
case LOG_ALERT:
return "ALERT"
case LOG_CRIT:
return "CRITICAL"
case LOG_ERR:
return "ERROR"
case LOG_WARNING:
return "WARNING"
case LOG_NOTICE:
return "NOTICE"
case LOG_INFO:
return "INFO"
case LOG_DEBUG:
return "DEBUG"
case log_DISABLE:
return "DISABLED"
}
return "UNKNOWN(" + strconv.Itoa(int(p)) + ")"
}
func (p Priority) ShortString() string {
switch p {
case LOG_EMERG:
return "EMERG"
case LOG_ALERT:
return "ALERT"
case LOG_CRIT:
return "CRTCL"
case LOG_ERR:
return "ERROR"
case LOG_WARNING:
return "WARN "
case LOG_NOTICE:
return "NTICE"
case LOG_INFO:
return "INFO "
case LOG_DEBUG:
return "DEBUG"
case log_DISABLE:
return "DSBLD"
}
return "UNKNOWN(" + strconv.Itoa(int(p)) + ")"
}
func ParsePriority(p string) Priority {
p = strings.ToUpper(p)
switch {
case LOG_EMERG.String() == p:
return LOG_EMERG
case LOG_ALERT.String() == p:
return LOG_ALERT
case LOG_CRIT.String() == p:
return LOG_CRIT
case LOG_ERR.String() == p:
return LOG_ERR
case LOG_WARNING.String() == p:
return LOG_WARNING
case LOG_NOTICE.String() == p:
return LOG_NOTICE
case LOG_INFO.String() == p:
return LOG_INFO
case LOG_DEBUG.String() == p:
return LOG_DEBUG
}
return log_DISABLE
}
// ****************************************************************************
// The actual Logger structure, methods, and components.
// The current Logger is implemented by holding a map of LogProcessors.
// A new Logger can be created by calling the NewLogger methods further below.
// A Logger will be initialized with zero processors to begin with, and thus,
// will need to have LogProcessors added to it with AddProcessor before it
// can begin logging.
//
type Logger struct {
// prefix used to prepend to logs if no other prefix is supplied.
prefix string
processors map[string]LogProcessor
mu sync.RWMutex // Read/Write Lock used to protect the prefix.
}
// Storage object used to pass the log data over to the Processor.
type LogEntry struct {
Prefix string // Prefix to prepend to the log message.
Priority Priority // Priority of the log message.
Msg string // The actual message payload
Created time.Time // Time this message was created.
}
func (dl *Logger) SetPrefix(newPrefix string) {
dl.mu.Lock()
dl.prefix = newPrefix
dl.mu.Unlock()
}
// Set/Get the priority of the Processor with the given name.
// If no processor with the given name exists, we return an error.
func (dl *Logger) SetPriority(procName string, newPriority Priority) error {
newPriority = BoundPriority(newPriority)
proc := dl.processors[procName]
if proc != nil {
proc.SetPriority(newPriority)
return nil
}
return errors.New("Couldn't find log processor with name '" + procName + "'")
}
func (dl *Logger) GetPriority(procName string) (Priority, error) {
proc := dl.processors[procName]
if proc != nil {
return proc.GetPriority(), nil
}
return LOG_EMERG, errors.New("Coudln't find log processor with name '" + procName + "'")
}
func (dl *Logger) GetPriorities() map[string]Priority {
pmap := map[string]Priority{}
for name, proc := range dl.processors {
pmap[name] = proc.GetPriority()
}
return pmap
}
func (dl *Logger) GetMaxPriority() Priority {
max := log_DISABLE
for _, proc := range dl.processors {
if p := proc.GetPriority(); p > max {
max = p
}
}
return max
}
// Add processors to this logger with the given name. Names need to be
// unique against all other processors. If a name conflict arises, we
// simply override the old processor with the same name with the new one.
func (dl *Logger) AddProcessor(name string, processor LogProcessor) {
if p := dl.processors[name]; p != nil {
p.Close()
}
if processor == nil {
// If we're setting it to nil, let's take that as deleting the key.
delete(dl.processors, name)
} else {
dl.processors[name] = processor
}
}
func (dl *Logger) DisableProcessor(name string) {
dl.processors[name].SetPriority(log_DISABLE)
}
func (dl *Logger) Close() {
for name, proc := range dl.processors {
delete(dl.processors, name)
if proc != nil {
proc.Close()
}
}
}
// Begin Logging interface. The following methods are used for logging
// messages to whatever processors this logger is associated with.
//
func (dl *Logger) Plogf(priority Priority, prefix string, format string, args ...interface{}) {
message := format
if len(args) > 0 {
message = fmt.Sprintf(format, args...)
}
if len(message) == 0 || message[len(message)-1] != '\n' {
message = message + "\n"
}
entry := &LogEntry{
Prefix: prefix,
Priority: BoundPriority(priority),
Msg: message,
Created: time.Now(),
}
for _, p := range dl.processors {
p.Process(entry)
}
}
func (dl *Logger) Logf(p Priority, format string, args ...interface{}) {
dl.mu.RLock()
prefix := dl.prefix
dl.mu.RUnlock()
dl.Plogf(p, prefix, format, args...)
}
func (dl *Logger) Debugf(format string, args ...interface{}) {
dl.Logf(LOG_DEBUG, format, args...)
}
func (dl *Logger) Infof(format string, args ...interface{}) {
dl.Logf(LOG_INFO, format, args...)
}
func (dl *Logger) Noticef(format string, args ...interface{}) {
dl.Logf(LOG_NOTICE, format, args...)
}
func (dl *Logger) Warningf(format string, args ...interface{}) {
dl.Logf(LOG_WARNING, format, args...)
}
func (dl *Logger) Errorf(format string, args ...interface{}) {
dl.Logf(LOG_ERR, format, args...)
}
func (dl *Logger) Criticalf(format string, args ...interface{}) {
dl.Logf(LOG_CRIT, format, args...)
}
func (dl *Logger) Alertf(format string, args ...interface{}) {
dl.Logf(LOG_ALERT, format, args...)
}
func (dl *Logger) Emergencyf(format string, args ...interface{}) {
dl.Logf(LOG_EMERG, format, args...)
}
// Create a new empty Logger with the given prefix.
// The prefix will be prepended to every log message unless
// LogP(...) is used, in which case, the prefix supplied by the 'prefix'
// parameter will be used instead.
//
func NewLogger(prefix string) *Logger {
return &Logger{prefix: prefix, processors: map[string]LogProcessor{}}
}
// ****************************************************************************
// The following section covers the part of the Logger which listens to the
// channel for new messages to write. All messages that are logged are
// eventually sent to this channel, and the following go routine services them
// to their appropriate Writer objects.
//
var logchan chan *LogMsg
const logQueueSize = 512
var die *int32 = new(int32)
func init() {
logchan = make(chan *LogMsg, logQueueSize)
go func() {
for entry := range logchan {
io.WriteString(entry.w, entry.msg)
shouldDie := atomic.LoadInt32(die)
if shouldDie > 0 {
break
}
}
}()
}
func FlushLogsAndDie() {
atomic.AddInt32(die, 1)
for i := 0; i < logQueueSize; i++ {
select {
case entry := <-logchan:
io.WriteString(entry.w, entry.msg)
default:
break
}
}
}