-
Notifications
You must be signed in to change notification settings - Fork 19
/
api.go
171 lines (146 loc) · 5.69 KB
/
api.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
package serverlogger
import (
"flag"
"fmt"
"io"
"log"
"regexp"
"sync"
"github.com/Symantec/Dominator/lib/logbuf"
"github.com/Symantec/Dominator/lib/srpc"
)
var (
initialLogDebugLevel = flag.Int("initialLogDebugLevel", -1,
"initial debug log level")
logSubseconds = flag.Bool("logSubseconds", false,
"if true, datestamps will have subsecond resolution")
)
type Logger struct {
accessChecker func(method string, authInfo *srpc.AuthInformation) bool
circularBuffer *logbuf.LogBuffer
flags int
level int16
maxLevel int16
mutex sync.Mutex // Lock everything below.
streamers map[*streamerType]struct{}
}
type streamerType struct {
debugLevel int16
excludeRegex *regexp.Regexp // nil: nothing excluded. Processed after incl.
includeRegex *regexp.Regexp // nil: everything included.
output chan<- []byte
}
// New will create a Logger which has an internal log buffer (see the
// lib/logbuf package). It implements the log.DebugLogger interface.
// By default, the max debug level is -1, meaning all debug logs are dropped
// (ignored).
// The name of the new logger is given by name. This name is used to remotely
// identify the logger for SRPC methods such as Logger.SetDebugLevel. The first
// or primary logger should be created with name "" (the empty string).
func New(name string) *Logger {
flags := log.LstdFlags
if *logSubseconds {
flags |= log.Lmicroseconds
}
return newLogger(name, logbuf.GetStandardOptions(), flags)
}
// NewWithFlags will create a Logger which has an internal log buffer (see the
// lib/logbuf package). It implements the log.DebugLogger interface.
// By default, the max debug level is -1, meaning all debug logs are dropped
// (ignored).
// The name of the new logger is given by name. This name is used to remotely
// identify the logger for RPC methods such as Logger.SetDebugLevel. The first
// or primary logger should be created with name "" (the empty string).
func NewWithFlags(name string, flags int) *Logger {
return newLogger(name, logbuf.GetStandardOptions(), flags)
}
// NewWithOptions will create a Logger which has an internal log buffer (see the
// lib/logbuf package). It implements the log.DebugLogger interface.
// By default, the max debug level is -1, meaning all debug logs are dropped
// (ignored).
// The name of the new logger is given by name. This name is used to remotely
// identify the logger for RPC methods such as Logger.SetDebugLevel. The first
// or primary logger should be created with name "" (the empty string).
func NewWithOptions(name string, options logbuf.Options, flags int) *Logger {
return newLogger(name, options, flags)
}
// Debug will call the Print method if level is less than or equal to the max
// debug level for the Logger.
func (l *Logger) Debug(level uint8, v ...interface{}) {
l.debug(int16(level), v...)
}
// Debugf will call the Printf method if level is less than or equal to the max
// debug level for the Logger.
func (l *Logger) Debugf(level uint8, format string, v ...interface{}) {
l.debugf(int16(level), format, v...)
}
// Debugln will call the Println method if level is less than or equal to the
// max debug level for the Logger.
func (l *Logger) Debugln(level uint8, v ...interface{}) {
l.debugln(int16(level), v...)
}
// GetLevel gets the current maximum debug level.
func (l *Logger) GetLevel() int16 {
return l.level
}
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
func (l *Logger) Fatal(v ...interface{}) {
l.fatals(fmt.Sprint(v...))
}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.fatals(fmt.Sprintf(format, v...))
}
// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
func (l *Logger) Fatalln(v ...interface{}) {
l.fatals(fmt.Sprintln(v...))
}
// Flush flushes the open log file (if one is open). This should only be called
// just prior to process termination. The log file is automatically flushed
// after short periods of inactivity.
func (l *Logger) Flush() error {
return l.circularBuffer.Flush()
}
// Panic is equivalent to Print() followed by a call to panic().
func (l *Logger) Panic(v ...interface{}) {
l.panics(fmt.Sprint(v...))
}
// Panicf is equivalent to Printf() followed by a call to panic().
func (l *Logger) Panicf(format string, v ...interface{}) {
l.panics(fmt.Sprintf(format, v...))
}
// Panicln is equivalent to Println() followed by a call to panic().
func (l *Logger) Panicln(v ...interface{}) {
l.panics(fmt.Sprintln(v...))
}
// Print prints to the logger. Arguments are handled in the manner of fmt.Print.
func (l *Logger) Print(v ...interface{}) {
l.prints(fmt.Sprint(v...))
}
// Printf prints to the logger. Arguments are handled in the manner of
// fmt.Printf.
func (l *Logger) Printf(format string, v ...interface{}) {
l.prints(fmt.Sprintf(format, v...))
}
// Println prints to the logger. Arguments are handled in the manner of
// fmt.Println.
func (l *Logger) Println(v ...interface{}) {
l.prints(fmt.Sprintln(v...))
}
// SetAccessChecker sets the function that is called when SRPC methods are
// called for the Logger. This allows the application to control which users or
// groups are permitted to remotely control the Logger.
func (l *Logger) SetAccessChecker(
accessChecker func(method string, authInfo *srpc.AuthInformation) bool) {
l.accessChecker = accessChecker
}
// SetLevel sets the maximum debug level. A negative level will cause all debug
// messages to be dropped.
func (l *Logger) SetLevel(maxLevel int16) {
l.setLevel(maxLevel)
}
// WriteHtml will write the contents of the internal log buffer to writer, with
// appropriate HTML markups.
func (l *Logger) WriteHtml(writer io.Writer) {
l.circularBuffer.WriteHtml(writer)
}