forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_windows.go
99 lines (80 loc) · 2.07 KB
/
log_windows.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
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"log"
"os"
"runtime/debug"
)
type Logger struct {
toSyslog bool
level Priority
selectors map[string]bool
logger *log.Logger
}
type Priority int
const (
// Severity.
// From /usr/include/sys/syslog.h.
// These are the same on Linux, BSD, and OS X.
LOG_EMERG Priority = iota
LOG_ALERT
LOG_CRIT
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
)
var _log Logger
func DEBUG(selector string, format string, v ...interface{}) {
if _log.level >= LOG_DEBUG {
selected := _log.selectors[selector]
if !selected {
return
}
_log.logger.Output(2, fmt.Sprintf("DBG "+format, v...))
}
}
func IS_DEBUG(selector string) bool {
return _log.selectors[selector]
}
func INFO(format string, v ...interface{}) {
if _log.level >= LOG_INFO {
_log.logger.Output(2, fmt.Sprintf("INFO "+format, v...))
}
}
func WARN(format string, v ...interface{}) {
if _log.level >= LOG_WARNING {
_log.logger.Output(2, fmt.Sprintf("WARN "+format, v...))
}
}
func ERR(format string, v ...interface{}) {
if _log.level >= LOG_ERR {
_log.logger.Output(2, fmt.Sprintf("ERR "+format, v...))
}
}
func CRIT(format string, v ...interface{}) {
if _log.level >= LOG_CRIT {
_log.logger.Output(2, fmt.Sprintf("CRIT "+format, v...))
}
}
func WTF(format string, v ...interface{}) {
if _log.level >= LOG_CRIT {
_log.logger.Output(2, fmt.Sprintf("CRIT "+format, v...))
}
// TODO: assert here when not in production mode
}
func RECOVER(msg string) {
if r := recover(); r != nil {
ERR("%s. Recovering, but please report this: %s.", msg, r)
ERR("Stacktrace: %s", debug.Stack())
}
}
func LogInit(level Priority, prefix string, toSyslog bool, debugSelectors []string) {
_log.level = level
_log.selectors = make(map[string]bool)
for _, selector := range debugSelectors {
_log.selectors[selector] = true
}
_log.logger = log.New(os.Stdout, prefix, log.Lshortfile)
}