This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
log.go
72 lines (61 loc) · 1.44 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
package modbus
import (
"log"
"os"
"sync/atomic"
)
// 内部调试实现.
type logger struct {
provider LogProvider
// has log output enabled,
// 1: enable
// 0: disable
has uint32
}
// newLogger new logger with prefix.
func newLogger(prefix string) logger {
return logger{
provider: defaultLogger{log.New(os.Stdout, prefix, log.LstdFlags)},
has: 0,
}
}
// LogMode set enable or disable log output when you has set logger.
func (sf *logger) LogMode(enable bool) {
if enable {
atomic.StoreUint32(&sf.has, 1)
} else {
atomic.StoreUint32(&sf.has, 0)
}
}
// setLogProvider overwrite log provider.
func (sf *logger) setLogProvider(p LogProvider) {
if p != nil {
sf.provider = p
}
}
// Error Log ERROR level message.
func (sf logger) Errorf(format string, v ...interface{}) {
if atomic.LoadUint32(&sf.has) == 1 {
sf.provider.Errorf(format, v...)
}
}
// Debug Log DEBUG level message.
func (sf logger) Debugf(format string, v ...interface{}) {
if atomic.LoadUint32(&sf.has) == 1 {
sf.provider.Debugf(format, v...)
}
}
// default log.
type defaultLogger struct {
*log.Logger
}
// check implement LogProvider interface.
var _ LogProvider = (*defaultLogger)(nil)
// Error Log ERROR level message.
func (sf defaultLogger) Errorf(format string, v ...interface{}) {
sf.Printf("[E]: "+format, v...)
}
// Debug Log DEBUG level message.
func (sf defaultLogger) Debugf(format string, v ...interface{}) {
sf.Printf("[D]: "+format, v...)
}