-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_util.go
104 lines (83 loc) · 2.16 KB
/
log_util.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
package log
import (
"bytes"
"fmt"
"time"
"github.com/davecgh/go-spew/spew"
)
func Sprint(args ...any) string {
buf := new(bytes.Buffer)
for i, arg := range args {
if i > 0 {
fmt.Fprint(buf, " ")
}
fmt.Fprint(buf, arg)
}
return buf.String()
}
func Sprintf(format string, args ...any) string {
return fmt.Sprintf(format, args...)
}
func (l *Logger) Println(args ...any) {
str := Sprint(args...)
l.LogLine(&Line{time.Now(), "debug", str, false})
}
func (l *Logger) Printf(format string, args ...any) {
l.LogLine(&Line{time.Now(), "debug", Sprintf(format, args...), false})
}
func (l *Logger) CatPrintf(cat Category, format string, args ...any) {
l.LogLine(&Line{time.Now(), cat, Sprintf(format, args...), false})
}
// shorthand
// Prints 1+ items to default logger
func Println(args ...any) {
DefaultLogger.Println(args...)
}
// Prints a formatted string to default logger
func Printf(format string, args ...any) {
DefaultLogger.Printf(format, args...)
}
// Prints a warning to the default logger
func Warn(args ...any) {
str := Sprint(args...)
DefaultLogger.LogLine(&Line{time.Now(), "warn", str, false})
}
// Prints 1+ error messages to the default logger
func Err(args ...any) {
str := Sprint(args...)
DefaultLogger.LogLine(&Line{time.Now(), "error", str, false})
}
func death() {
// Bide your time and wait for the end
wait := make(chan bool)
<-wait
}
// Crash the program, providing 1+ error messages
func Fatal(args ...any) {
str := Sprint(append([]any{"Fatal: "}, args...)...)
DefaultLogger.LogLine(&Line{time.Now(), "error", str, true})
death()
}
func dump(object any) string {
return spew.Sdump(object)
}
func NewDumpLine(name string, cat Category, object any) *Line {
return &Line{
time.Now(),
cat,
Sprintf("%s = %s", name, dump(object)),
false,
}
}
// Show a formatted representation of an object to the default logger
func Dump(name string, object any) {
ln := NewDumpLine(name, "debug", object)
DefaultLogger.LogLine(ln)
}
// Show a formatted object representation before crashing the program
func FatalDump(name string, object any) {
ln := NewDumpLine(name, "error", object)
ln.Fatal = true
DefaultLogger.LogLine(ln)
death()
}