-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoutput.go
74 lines (64 loc) · 1.42 KB
/
output.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
package main
import (
"fmt"
"log"
)
const (
logLevelNormal = 0
logLevelVerbose = 1
logLevelDebug = 2
logLevelDump = 3
)
type Output struct {
Logger *log.Logger
Level int
}
func NewOutput(config Config) (out Output) {
out.Logger = log.New(config.LogFile, "", log.Ldate|log.Lmicroseconds|log.LUTC)
out.Level = config.LogLevel
return out
}
func (out Output) Dump(slice []byte, format string, args ...interface{}) {
if out.Level >= logLevelDump {
str := fmt.Sprintf(format, args...)
rowCount := len(slice) / 16
if len(slice)%16 > 0 {
rowCount++
}
for i := 0; i < rowCount; i++ {
str += " "
for j := 0; j < 16; j++ {
if len(slice)-i*16 <= j {
str += " "
} else {
str += fmt.Sprintf("%2x ", slice[i*16+j])
}
}
str += " "
for j := 0; j < 16; j++ {
if len(slice)-i*16 <= j {
str += " "
} else if slice[i*16+j] >= 0x20 && slice[i*16+j] <= 0x7e {
str += fmt.Sprintf("%c ", slice[i*16+j])
} else {
str += "* "
}
}
str += "\n"
}
out.Logger.Print(str)
}
}
func (out Output) Debug(format string, args ...interface{}) {
if out.Level >= logLevelDebug {
out.Logger.Printf(format, args...)
}
}
func (out Output) Verbose(format string, args ...interface{}) {
if out.Level >= logLevelVerbose {
out.Logger.Printf(format, args...)
}
}
func (out Output) Log(format string, args ...interface{}) {
out.Logger.Printf(format, args...)
}