forked from andeya/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
537 lines (458 loc) · 15.3 KB
/
log.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright 2015-2019 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package erpc
import (
"fmt"
"log"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/henrylee2cn/goutil/graceful"
"github.com/henrylee2cn/erpc/v6/utils"
"github.com/henrylee2cn/erpc/v6/utils/color"
"github.com/henrylee2cn/goutil"
)
type (
// LoggerOutputter writes log.
LoggerOutputter interface {
// Output writes log, can append time, line and so on information.
Output(calldepth int, msgBytes []byte, loggerLevel LoggerLevel)
// Flush writes any buffered log to the underlying io.Writer.
Flush() error
}
// LoggerLevel defines all available log levels for log messages.
LoggerLevel int
// Logger logger interface
Logger interface {
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
Printf(format string, a ...interface{})
// Fatalf is equivalent to Criticalf followed by a call to os.Exit(1).
Fatalf(format string, a ...interface{})
// Panicf is equivalent to Criticalf followed by a call to panic().
Panicf(format string, a ...interface{})
// Criticalf logs a message using CRITICAL as log level.
Criticalf(format string, a ...interface{})
// Errorf logs a message using ERROR as log level.
Errorf(format string, a ...interface{})
// Warnf logs a message using WARNING as log level.
Warnf(format string, a ...interface{})
// Noticef logs a message using NOTICE as log level.
Noticef(format string, a ...interface{})
// Infof logs a message using INFO as log level.
Infof(format string, a ...interface{})
// Debugf logs a message using DEBUG as log level.
Debugf(format string, a ...interface{})
// Tracef logs a message using TRACE as log level.
Tracef(format string, a ...interface{})
}
)
// Logger levels.
const (
OFF LoggerLevel = iota
PRINT
CRITICAL
ERROR
WARNING
NOTICE
INFO
DEBUG
TRACE
)
var loggerLevelMap = map[LoggerLevel]string{
OFF: "OFF",
PRINT: "PRINT",
CRITICAL: "CRITICAL",
ERROR: "ERROR",
WARNING: "WARNING",
NOTICE: "NOTICE",
INFO: "INFO",
DEBUG: "DEBUG",
TRACE: "TRACE",
}
var loggerLevel = DEBUG
func (l LoggerLevel) String() string {
s, ok := loggerLevelMap[l]
if !ok {
return "unknown"
}
return s
}
type easyLoggerOutputter struct {
output func(calldepth int, msgBytes []byte, loggerLevel LoggerLevel)
flush func() error
}
// Output writes log.
func (e *easyLoggerOutputter) Output(calldepth int, msgBytes []byte, loggerLevel LoggerLevel) {
e.output(calldepth, msgBytes, loggerLevel)
}
// Flush writes any buffered log to the underlying io.Writer.
func (e *easyLoggerOutputter) Flush() error {
return e.flush()
}
var loggerOutputter = func() LoggerOutputter {
type msg struct {
logBytes []byte
loggerLevel LoggerLevel
}
var p = sync.Pool{
New: func() interface{} {
return new(msg)
},
}
var loggerLevelTagMap = map[LoggerLevel]string{
OFF: color.Bold("OFF"),
PRINT: color.Bold("PRIN"),
CRITICAL: color.Magenta(color.Bold("CRIT")),
ERROR: color.Red(color.Bold("ERRO")),
WARNING: color.Yellow(color.Bold("WARN")),
NOTICE: color.Green(color.Bold("NOTI")),
INFO: color.Green(color.Bold("INFO")),
DEBUG: color.Cyan(color.Bold("DEBU")),
TRACE: color.Cyan(color.Bold("TRAC")),
}
var c = make(chan *msg, 1024)
go func() {
for m := range c {
if m == nil {
continue
}
if m.loggerLevel > ERROR || m.loggerLevel == PRINT {
color.Stderr.Write(m.logBytes)
} else {
color.Stdout.Write(m.logBytes)
}
p.Put(m)
}
}()
return &easyLoggerOutputter{
output: func(calldepth int, msgBytes []byte, loggerLevel LoggerLevel) {
m := p.Get().(*msg)
buf := utils.AcquireByteBuffer()
buf.WriteString("[" + time.Now().Format("2006/01/02 15:04:05.000") + "]")
buf.WriteString(" [" + loggerLevelTagMap[loggerLevel] + "] ")
buf.Write(msgBytes)
line := goutil.GetCallLine(calldepth + 1)
if !strings.Contains(line, "github.com/henrylee2cn/erpc") &&
!strings.Contains(line, "github.com/henrylee2cn/goutil/graceful") {
buf.WriteString(" <" + line + ">\n")
} else {
buf.WriteByte('\n')
}
m.logBytes = goutil.StringToBytes(buf.String())
m.loggerLevel = loggerLevel
c <- m
},
flush: func() error {
c <- nil
for len(c) > 0 {
runtime.Gosched()
}
return nil
},
}
}()
// FlushLogger writes any buffered log to the underlying io.Writer.
func FlushLogger() error {
return loggerOutputter.Flush()
}
// SetLoggerOutputter sets logger outputter.
// NOTE: Concurrent is not safe!
func SetLoggerOutputter(outputter LoggerOutputter) (flusher func() error) {
loggerOutputter = outputter
return FlushLogger
}
// SetLoggerLevel sets the logger's level by string.
func SetLoggerLevel(level string) (flusher func() error) {
for k, v := range loggerLevelMap {
if v == level {
loggerLevel = k
return FlushLogger
}
}
log.Printf("Unknown level string: %s", level)
return FlushLogger
}
// SetLoggerLevel2 sets the logger's level by number.
func SetLoggerLevel2(level LoggerLevel) (flusher func() error) {
_, ok := loggerLevelMap[level]
if !ok {
log.Printf("Unknown level number: %d", level)
return FlushLogger
}
loggerLevel = level
return FlushLogger
}
// GetLoggerLevel gets the logger's level.
func GetLoggerLevel() LoggerLevel {
return loggerLevel
}
// EnableLoggerLevel returns if can print the level of log.
func EnableLoggerLevel(level LoggerLevel) bool {
if level <= loggerLevel {
return level != OFF
}
return false
}
// GetLogger returns the global logger object.
func GetLogger() Logger {
return logger
}
func loggerOutput(loggerLevel LoggerLevel, format string, a ...interface{}) {
if !EnableLoggerLevel(loggerLevel) {
return
}
loggerOutputter.Output(3, goutil.StringToBytes(fmt.Sprintf(format, a...)), loggerLevel)
}
// ************ global logger functions ************
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func Printf(format string, a ...interface{}) {
loggerOutput(PRINT, format, a...)
}
// Fatalf is equivalent to l.Criticalf followed by a call to os.Exit(1).
func Fatalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
os.Exit(1)
}
// Panicf is equivalent to l.Criticalf followed by a call to panic().
func Panicf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
panic(fmt.Sprintf(format, a...))
}
// Criticalf logs a message using CRITICAL as log level.
func Criticalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
}
// Errorf logs a message using ERROR as log level.
func Errorf(format string, a ...interface{}) {
loggerOutput(ERROR, format, a...)
}
// Warnf logs a message using WARNING as log level.
func Warnf(format string, a ...interface{}) {
loggerOutput(WARNING, format, a...)
}
// Noticef logs a message using NOTICE as log level.
func Noticef(format string, a ...interface{}) {
loggerOutput(NOTICE, format, a...)
}
// Infof logs a message using INFO as log level.
func Infof(format string, a ...interface{}) {
loggerOutput(INFO, format, a...)
}
// Debugf logs a message using DEBUG as log level.
func Debugf(format string, a ...interface{}) {
loggerOutput(DEBUG, format, a...)
}
// Tracef logs a message using TRACE as log level.
func Tracef(format string, a ...interface{}) {
loggerOutput(TRACE, format, a...)
}
// ************ globalLogger logger methods ************
type globalLogger struct{}
var (
logger = new(globalLogger)
_ Logger = logger
_ graceful.LoggerWithFlusher = logger
)
func (globalLogger) Flush() error {
return FlushLogger()
}
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func (globalLogger) Printf(format string, a ...interface{}) {
loggerOutput(PRINT, format, a...)
}
// Fatalf is equivalent to l.Criticalf followed by a call to os.Exit(1).
func (globalLogger) Fatalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
os.Exit(1)
}
// Panicf is equivalent to l.Criticalf followed by a call to panic().
func (globalLogger) Panicf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
panic(fmt.Sprintf(format, a...))
}
// Criticalf logs a message using CRITICAL as log level.
func (globalLogger) Criticalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
}
// Errorf logs a message using ERROR as log level.
func (globalLogger) Errorf(format string, a ...interface{}) {
loggerOutput(ERROR, format, a...)
}
// Warnf logs a message using WARNING as log level.
func (globalLogger) Warnf(format string, a ...interface{}) {
loggerOutput(WARNING, format, a...)
}
// Noticef logs a message using NOTICE as log level.
func (globalLogger) Noticef(format string, a ...interface{}) {
loggerOutput(NOTICE, format, a...)
}
// Infof logs a message using INFO as log level.
func (globalLogger) Infof(format string, a ...interface{}) {
loggerOutput(INFO, format, a...)
}
// Debugf logs a message using DEBUG as log level.
func (globalLogger) Debugf(format string, a ...interface{}) {
loggerOutput(DEBUG, format, a...)
}
// Tracef logs a message using TRACE as log level.
func (globalLogger) Tracef(format string, a ...interface{}) {
loggerOutput(TRACE, format, a...)
}
// ************ *session logger methods ************
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func (s *session) Printf(format string, a ...interface{}) {
loggerOutput(PRINT, format, a...)
}
// Fatalf is equivalent to l.Criticalf followed by a call to os.Exit(1).
func (s *session) Fatalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
os.Exit(1)
}
// Panicf is equivalent to l.Criticalf followed by a call to panic().
func (s *session) Panicf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
panic(fmt.Sprintf(format, a...))
}
// Criticalf logs a message using CRITICAL as log level.
func (s *session) Criticalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
}
// Errorf logs a message using ERROR as log level.
func (s *session) Errorf(format string, a ...interface{}) {
loggerOutput(ERROR, format, a...)
}
// Warnf logs a message using WARNING as log level.
func (s *session) Warnf(format string, a ...interface{}) {
loggerOutput(WARNING, format, a...)
}
// Noticef logs a message using NOTICE as log level.
func (s *session) Noticef(format string, a ...interface{}) {
loggerOutput(NOTICE, format, a...)
}
// Infof logs a message using INFO as log level.
func (s *session) Infof(format string, a ...interface{}) {
loggerOutput(INFO, format, a...)
}
// Debugf logs a message using DEBUG as log level.
func (s *session) Debugf(format string, a ...interface{}) {
loggerOutput(DEBUG, format, a...)
}
// Tracef logs a message using TRACE as log level.
func (s *session) Tracef(format string, a ...interface{}) {
loggerOutput(TRACE, format, a...)
}
// ************ *handlerCtx Pure Logger Methods ************
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func (c *handlerCtx) Printf(format string, a ...interface{}) {
loggerOutput(PRINT, format, a...)
}
// Fatalf is equivalent to l.Criticalf followed by a call to os.Exit(1).
func (c *handlerCtx) Fatalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
os.Exit(1)
}
// Panicf is equivalent to l.Criticalf followed by a call to panic().
func (c *handlerCtx) Panicf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
panic(fmt.Sprintf(format, a...))
}
// Criticalf logs a message using CRITICAL as log level.
func (c *handlerCtx) Criticalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
}
// Errorf logs a message using ERROR as log level.
func (c *handlerCtx) Errorf(format string, a ...interface{}) {
loggerOutput(ERROR, format, a...)
}
// Warnf logs a message using WARNING as log level.
func (c *handlerCtx) Warnf(format string, a ...interface{}) {
loggerOutput(WARNING, format, a...)
}
// Noticef logs a message using NOTICE as log level.
func (c *handlerCtx) Noticef(format string, a ...interface{}) {
loggerOutput(NOTICE, format, a...)
}
// Infof logs a message using INFO as log level.
func (c *handlerCtx) Infof(format string, a ...interface{}) {
loggerOutput(INFO, format, a...)
}
// Debugf logs a message using DEBUG as log level.
func (c *handlerCtx) Debugf(format string, a ...interface{}) {
loggerOutput(DEBUG, format, a...)
}
// Tracef logs a message using TRACE as log level.
func (c *handlerCtx) Tracef(format string, a ...interface{}) {
loggerOutput(TRACE, format, a...)
}
// ************ *callCmd Pure Logger Methods ************
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func (c *callCmd) Printf(format string, a ...interface{}) {
loggerOutput(PRINT, format, a...)
}
// Fatalf is equivalent to l.Criticalf followed by a call to os.Exit(1).
func (c *callCmd) Fatalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
os.Exit(1)
}
// Panicf is equivalent to l.Criticalf followed by a call to panic().
func (c *callCmd) Panicf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
loggerOutputter.Flush()
panic(fmt.Sprintf(format, a...))
}
// Criticalf logs a message using CRITICAL as log level.
func (c *callCmd) Criticalf(format string, a ...interface{}) {
loggerOutput(CRITICAL, format, a...)
}
// Errorf logs a message using ERROR as log level.
func (c *callCmd) Errorf(format string, a ...interface{}) {
loggerOutput(ERROR, format, a...)
}
// Warnf logs a message using WARNING as log level.
func (c *callCmd) Warnf(format string, a ...interface{}) {
loggerOutput(WARNING, format, a...)
}
// Noticef logs a message using NOTICE as log level.
func (c *callCmd) Noticef(format string, a ...interface{}) {
loggerOutput(NOTICE, format, a...)
}
// Infof logs a message using INFO as log level.
func (c *callCmd) Infof(format string, a ...interface{}) {
loggerOutput(INFO, format, a...)
}
// Debugf logs a message using DEBUG as log level.
func (c *callCmd) Debugf(format string, a ...interface{}) {
loggerOutput(DEBUG, format, a...)
}
// Tracef logs a message using TRACE as log level.
func (c *callCmd) Tracef(format string, a ...interface{}) {
loggerOutput(TRACE, format, a...)
}