-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlog.go
67 lines (56 loc) · 1.23 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
package vncproxy
import (
"fmt"
"log"
)
const (
InfoFlag uint32 = 0x1 << 0
DebugFlag uint32 = 0x1 << 1
InfoLevel uint32 = InfoFlag
DebugLevel uint32 = InfoFlag | DebugFlag
)
type Logger interface {
Infof(format string, v ...interface{})
Debugf(format string, v ...interface{})
}
type logger struct {
level uint32
logger Logger
}
func NewLogger(level uint32, instance Logger) *logger {
return &logger{
level: level,
logger: instance,
}
}
func (l *logger) print(level string, msg ...interface{}) {
if l.logger != nil {
if level == "debug" {
l.logger.Debugf("[vncproxy] %v", fmt.Sprint(msg...))
} else {
l.logger.Infof("[vncproxy] %v", fmt.Sprint(msg...))
}
} else {
log.Printf("[vncproxy][%v] %v", level, fmt.Sprint(msg...))
}
}
func (l *logger) Info(msg ...interface{}) {
if l.level&InfoFlag > 0 {
l.print("info", msg...)
}
}
func (l *logger) Infof(format string, msg ...interface{}) {
if l.level&InfoFlag > 0 {
l.print("info", fmt.Sprintf(format, msg...))
}
}
func (l *logger) Debug(msg ...interface{}) {
if l.level&DebugFlag > 0 {
l.print("debug", msg...)
}
}
func (l *logger) Debugf(format string, msg ...interface{}) {
if l.level&DebugFlag > 0 {
l.print("debug", fmt.Sprintf(format, msg...))
}
}