-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.go
63 lines (55 loc) · 1.19 KB
/
common.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
package common
import (
"log"
"os"
"time"
)
type LogLevels struct {
Debug bool
Error bool
Warn bool
Critical bool
}
var (
Retain bool = false
QoS byte = 0
HADiscoveryDelay time.Duration = 500 * time.Millisecond
MachineID string
LogState = LogLevels{
Debug: false,
Error: false,
Warn: false,
Critical: false,
}
)
var DebugLog = log.New(os.Stdout, "DEBUG ", 0)
var ErrorLog = log.New(os.Stdout, "ERROR ", 0)
var WarnLog = log.New(os.Stdout, "WARN ", 0)
var CriticalLog = log.New(os.Stdout, "CRITICAL", 0)
const logPrefix = "[ha-mqtt] "
func LogError(message ...interface{}) {
if LogState.Error {
if len(message) > 1 {
for _, mes := range message[:len(message)-2] {
ErrorLog.Printf("%v%v\n", logPrefix, mes)
}
}
ErrorLog.Fatalf("%v%v\n", logPrefix, message[len(message)-1])
} else {
os.Exit(1)
}
}
func LogDebug(message ...interface{}) {
if LogState.Debug {
for _, mes := range message {
DebugLog.Printf("%v%v\n", logPrefix, mes)
}
}
}
func LogWarning(message ...interface{}) {
if LogState.Warn {
for _, mes := range message {
WarnLog.Printf("%v%v\n", logPrefix, mes)
}
}
}