forked from deepkaran/goforestdb
-
Notifications
You must be signed in to change notification settings - Fork 6
/
log.go
116 lines (94 loc) · 2.27 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package forestdb
//#include "log.h"
import "C"
import (
"log"
"unsafe"
)
//export LogCallbackInternal
func LogCallbackInternal(errCode C.int, msg *C.char, ctx *C.char) {
context := (*C.log_context)(unsafe.Pointer(ctx))
cb := logCallbacks[context.offset]
userCtx := logContexts[context.offset]
cb(C.GoString(context.name), int(errCode), C.GoString(msg), userCtx)
}
//export FatalErrorCallbackInternal
func FatalErrorCallbackInternal() {
fatalErrorCallback()
}
// Logger interface
type Logger interface {
// Warnings, logged by default.
Warnf(format string, v ...interface{})
// Errors, logged by default.
Errorf(format string, v ...interface{})
// Fatal errors. Will not terminate execution.
Fatalf(format string, v ...interface{})
// Informational messages.
Infof(format string, v ...interface{})
// Timing utility
Debugf(format string, v ...interface{})
// Program execution tracing. Not logged by default
Tracef(format string, v ...interface{})
}
type Dummy struct {
}
func (*Dummy) Fatalf(_ string, _ ...interface{}) {
}
func (*Dummy) Errorf(_ string, _ ...interface{}) {
}
func (*Dummy) Warnf(_ string, _ ...interface{}) {
}
func (*Dummy) Infof(_ string, _ ...interface{}) {
}
func (*Dummy) Debugf(_ string, _ ...interface{}) {
}
func (*Dummy) Tracef(_ string, _ ...interface{}) {
}
type LogLevel int
const (
LogFatal LogLevel = iota
LogError
LogWarn
LogInfo
LogDebug
LogTrace
)
type LeveledLog struct {
level LogLevel
}
func NewLeveledLog(level LogLevel) *LeveledLog {
return &LeveledLog{level: level}
}
func (l *LeveledLog) Fatalf(format string, a ...interface{}) {
if l.level >= LogFatal {
log.Fatalf(format, a...)
}
}
func (l *LeveledLog) Errorf(format string, a ...interface{}) {
if l.level >= LogError {
log.Printf(format, a...)
}
}
func (l *LeveledLog) Warnf(format string, a ...interface{}) {
if l.level >= LogWarn {
log.Printf(format, a...)
}
}
func (l *LeveledLog) Infof(format string, a ...interface{}) {
if l.level >= LogInfo {
log.Printf(format, a...)
}
}
func (l *LeveledLog) Debugf(format string, a ...interface{}) {
if l.level >= LogDebug {
log.Printf(format, a...)
}
}
func (l *LeveledLog) Tracef(format string, a ...interface{}) {
if l.level >= LogTrace {
log.Printf(format, a...)
}
}
// Logger to use
var Log Logger = &Dummy{}