-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.go
307 lines (257 loc) · 5.41 KB
/
Logger.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
package logger
import (
// "fmt"
"fmt"
"io"
"reflect"
"strings"
"sync"
"time"
// "github.com/AnimusPEXUS/gosignal"
)
type (
EntryType uint
)
const (
TextEntryType EntryType = iota
InfoEntryType
WarningEntryType
ErrorEntryType
)
type OutputOptions struct {
TextIcon string
InfoIcon string
WarningIcon string
ErrorIcon string
InsertTime bool
TimeLayout string
ClosedByLogger bool
}
var std_output_opt = &OutputOptions{
TextIcon: "",
InfoIcon: "[i]",
WarningIcon: "[w]",
ErrorIcon: "[e]",
InsertTime: true,
TimeLayout: time.RFC3339,
ClosedByLogger: false,
}
type WriterWrapper struct {
out interface{}
opt *OutputOptions
}
type LogEntry struct {
Type EntryType
Time time.Time
Text string
}
func (self *LogEntry) TypeString() string {
switch self.Type {
case TextEntryType:
{
return "text"
}
case InfoEntryType:
{
return "info"
}
case WarningEntryType:
{
return "warning"
}
case ErrorEntryType:
{
return "error"
}
}
return "programming error"
}
func (self *LogEntry) TypeStringT() string {
return strings.Title(self.TypeString())
}
type LoggerCallback func(*LogEntry, *Logger)
type LoggerI interface {
Text(string)
Info(string)
Warning(string)
Error(interface{})
PutEntry(type_ EntryType, value interface{})
PutEntryComplete(entry *LogEntry)
}
type Logger struct {
callbacks []LoggerCallback
outputs map[uint64]*WriterWrapper
output_counter uint64
mutex *sync.Mutex
stdout_lbl *LineByLineWriter
stderr_lbl *LineByLineWriter
}
func New() *Logger {
ret := new(Logger)
ret.mutex = new(sync.Mutex)
ret.ResetOutput()
return ret
}
func (self *Logger) Close() {
self.mutex.Lock()
defer self.mutex.Unlock()
for _, val := range self.outputs {
switch val.out.(type) {
case io.WriteCloser:
{
if val.opt.ClosedByLogger {
val.out.(io.WriteCloser).Close()
}
}
}
}
self.resetOutput()
}
func (self *Logger) ConnectCallback(callback LoggerCallback) {
self.callbacks = append(self.callbacks, callback)
}
func (self *Logger) addOutputOpt(out interface{}, opts *OutputOptions) uint64 {
switch out.(type) {
case io.Writer:
case io.WriteCloser:
case LoggerI:
default:
panic("only io.Writer, io.WriteCloser or LoggerI may be passed")
}
ret := self.output_counter
self.outputs[ret] = &WriterWrapper{out, opts}
self.output_counter++
return ret
}
// out type must conform to io.Writer or io.WriteCloser
func (self *Logger) AddOutputOpt(out interface{}, opts *OutputOptions) uint64 {
self.mutex.Lock()
defer self.mutex.Unlock()
return self.addOutputOpt(out, opts)
}
// out type must conform to io.Writer, io.WriteCloser or LoggerI
func (self *Logger) AddOutput(out interface{}) uint64 {
self.mutex.Lock()
defer self.mutex.Unlock()
return self.addOutputOpt(out, std_output_opt)
}
func (self *Logger) DelOutput(id uint64) {
self.mutex.Lock()
defer self.mutex.Unlock()
delete(self.outputs, id)
}
func (self *Logger) resetOutput() {
self.outputs = make(map[uint64]*WriterWrapper)
}
func (self *Logger) ResetOutput() {
self.mutex.Lock()
defer self.mutex.Unlock()
self.resetOutput()
}
func (self *Logger) PutEntry(type_ EntryType, value interface{}) {
go func() {
self.mutex.Lock()
defer self.mutex.Unlock()
value_str := "error"
switch value.(type) {
case string:
value_str = value.(string)
case error:
value_str = value.(error).Error()
default:
value_str = reflect.ValueOf(value).String()
}
log_entry := &LogEntry{type_, time.Now().UTC(), value_str}
self.PutEntryComplete(log_entry)
}()
}
func (self *Logger) PutEntryComplete(entry *LogEntry) {
for _, cb := range self.callbacks {
cb(entry, self)
}
wg := &sync.WaitGroup{}
wg.Add(len(self.outputs))
for _, i := range self.outputs {
go func(
i *WriterWrapper,
wg *sync.WaitGroup,
) {
defer wg.Done()
switch i.out.(type) {
case io.Writer:
self._WriteOutput(i, entry)
case io.WriteCloser:
self._WriteOutput(i, entry)
case LoggerI:
i.out.(LoggerI).PutEntryComplete(entry)
}
}(i, wg)
}
wg.Wait()
}
func (self *Logger) _WriteOutput(
ww *WriterWrapper,
entry *LogEntry,
) {
txt := ""
switch entry.Type {
case TextEntryType:
txt += ww.opt.TextIcon
case InfoEntryType:
txt += ww.opt.InfoIcon
case WarningEntryType:
txt += ww.opt.WarningIcon
case ErrorEntryType:
txt += ww.opt.ErrorIcon
}
if ww.opt.InsertTime {
if len(txt) != 0 {
txt += " "
}
txt += fmt.Sprintf(
"%-30s",
entry.Time.Format(ww.opt.TimeLayout),
)
}
if len(entry.Text) != 0 {
if len(txt) != 0 {
txt += " "
}
txt += entry.Text
}
txt += "\n"
b := []byte(txt)
ww.out.(io.Writer).Write(b)
// switch ww.out.(type) {
// case interface {
// Sync() error
// }:
// ww.out.(interface {
// Sync() error
// }).Sync()
// }
}
func (self *Logger) Text(txt string) {
self.PutEntry(TextEntryType, txt)
}
func (self *Logger) Info(txt string) {
self.PutEntry(InfoEntryType, txt)
}
func (self *Logger) Warning(txt string) {
self.PutEntry(WarningEntryType, txt)
}
func (self *Logger) Error(value interface{}) {
self.PutEntry(ErrorEntryType, value)
}
func (self *Logger) StdoutLbl() *LineByLineWriter {
if self.stdout_lbl == nil {
self.stdout_lbl = NewLineByLineWriter(self, false)
}
return self.stdout_lbl
}
func (self *Logger) StderrLbl() *LineByLineWriter {
if self.stderr_lbl == nil {
self.stderr_lbl = NewLineByLineWriter(self, true)
}
return self.stderr_lbl
}