forked from simonvetter/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
78 lines (56 loc) · 1.29 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
package modbus
import (
"fmt"
"os"
)
type logger struct {
prefix string
}
func newLogger(prefix string) (l *logger) {
l = &logger{
prefix: prefix,
}
return
}
func (l *logger) Info(msg string) {
l.write(false, fmt.Sprintf("%s [info]: %s\n", l.prefix, msg))
return
}
func (l *logger) Infof(format string, msg ...interface{}) {
l.write(false, fmt.Sprintf("%s [info]: %s\n", l.prefix, fmt.Sprintf(format, msg...)))
return
}
func (l *logger) Warning(msg string) {
l.write(false, fmt.Sprintf("%s [warn]: %s\n", l.prefix, msg))
return
}
func (l *logger) Warningf(format string, msg ...interface{}) {
l.write(false, fmt.Sprintf("%s [warn]: %s\n", l.prefix, fmt.Sprintf(format, msg...)))
return
}
func (l *logger) Error(msg string) {
l.write(false, fmt.Sprintf("%s [error]: %s\n", l.prefix, msg))
return
}
func (l *logger) Errorf(format string, msg ...interface{}) {
l.write(false, fmt.Sprintf("%s [error]: %s\n", l.prefix, fmt.Sprintf(format, msg...)))
return
}
func (l *logger) Fatal(msg string) {
l.Error(msg)
os.Exit(1)
return
}
func (l *logger) Fatalf(format string, msg ...interface{}) {
l.Errorf(format, msg...)
os.Exit(1)
return
}
func (l *logger) write(stderr bool, msg string) {
if stderr {
os.Stderr.WriteString(msg)
} else {
os.Stdout.WriteString(msg)
}
return
}