-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.go
91 lines (80 loc) · 1.98 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
package go_i2cp
import "fmt"
const (
PROTOCOL = 1 << 0
LOGIC = 1 << 1
DEBUG = 1 << 4
INFO = 1 << 5
WARNING = 1 << 6
ERROR = 1 << 7
FATAL = 1 << 8
STRINGMAP = 1 << 9
INTMAP = 1 << 10
QUEUE = 1 << 11
STREAM = 1 << 12
CRYPTO = 1 << 13
TCP = 1 << 14
CLIENT = 1 << 15
CERTIFICATE = 1 << 16
LEASE = 1 << 17
DESTINATION = 1 << 18
SESSION = 1 << 19
SESSION_CONFIG = 1 << 20
TEST = 1 << 21
DATAGRAM = 1 << 22
CONFIG_FILE = 1 << 23
VERSION = 1 << 24
TAG_MASK = 0x0000000f
LEVEL_MASK = 0x000001f0
COMPONENT_MASK = 0xfffffe00
ALL = 0xffffffff
)
type LoggerTags = uint32
type LoggerCallbacks struct {
opaque *interface{}
onLog func(*Logger, LoggerTags, string)
}
type Logger struct {
callbacks *LoggerCallbacks
logLevel int
}
var logInstance = &Logger{}
// TODO filter
func LogInit(callbacks *LoggerCallbacks, level int) {
logInstance = &Logger{callbacks: callbacks}
logInstance.setLogLevel(level)
}
func Debug(tags LoggerTags, message string, args ...interface{}) {
logInstance.log(tags|DEBUG, message, args...)
}
func Info(tags LoggerTags, message string, args ...interface{}) {
logInstance.log(tags|INFO, message, args...)
}
func Warning(tags LoggerTags, message string, args ...interface{}) {
logInstance.log(tags|WARNING, message, args...)
}
func Error(tags LoggerTags, message string, args ...interface{}) {
logInstance.log(tags|ERROR, message, args...)
}
func Fatal(tags LoggerTags, message string, args ...interface{}) {
logInstance.log(tags|FATAL, message, args...)
}
func (l *Logger) log(tags LoggerTags, format string, args ...interface{}) {
if l.callbacks == nil {
fmt.Printf(format+"\n", args)
} else {
l.callbacks.onLog(l, tags, fmt.Sprintf(format, args))
}
}
func (l *Logger) setLogLevel(level int) {
switch level {
case DEBUG:
case INFO:
case WARNING:
case ERROR:
case FATAL:
l.logLevel = level
default:
l.logLevel = ERROR
}
}