-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
83 lines (70 loc) · 1.5 KB
/
debug.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
package mqttx
import (
"log"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
const (
LevelEmergency = iota
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug
)
var debugEnable bool
type TraceFunc func(format string, level int, v ...interface{})
var UserTrace TraceFunc = nil
func init() {
debugEnable = false
log.SetPrefix("[mqttx] TRACE: ")
log.SetFlags(log.LstdFlags | log.Llongfile)
}
// Debug Enable debug
func Debug(enable bool) {
debugEnable = enable
}
// MqttLibDebug Enable mqtt lib debug
func MqttLibDebug(enable bool, level int) {
if enable {
mqtt.ERROR = mqttLibDebugNew(LevelError)
mqtt.CRITICAL = mqttLibDebugNew(LevelCritical)
mqtt.WARN = mqttLibDebugNew(LevelWarning)
mqtt.DEBUG = mqttLibDebugNew(LevelDebug)
}
}
// SetUserDebug 配置其他日志输出
func SetUserDebug(f TraceFunc) {
UserTrace = f
}
// TraceInfo 调试信息日志
func TraceInfo(format string, v ...interface{}) {
if debugEnable {
if UserTrace != nil {
UserTrace(format, LevelInformational, v...)
} else {
log.Printf(format, v...)
}
}
}
// TraceError 错误日志
func TraceError(format string, v ...interface{}) {
if debugEnable {
if UserTrace != nil {
UserTrace(format, LevelError, v...)
} else {
log.Printf(format, v...)
}
}
}
// TraceDebug Debug调试信息日志
func TraceDebug(format string, v ...interface{}) {
if debugEnable {
if UserTrace != nil {
UserTrace(format, LevelDebug, v...)
} else {
log.Printf(format, v...)
}
}
}