forked from diamondburned/gtkcord3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
166 lines (140 loc) · 3.38 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package log
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"time"
"github.com/logrusorgru/aurora"
)
var (
Output io.Writer = os.Stderr
Flags = log.Ltime | log.Lmicroseconds
PrefixPanic = "PANIC! "
PrefixError = "Error: "
PrefixInfo = "Info: "
PrefixDebug = "Debug: "
DebugGreyLvl = uint8(11)
Quiet = true
LogPath = filepath.Join(os.TempDir(), "gtkcord3.log")
)
var (
logPanic *log.Logger
logError *log.Logger
logInfo *log.Logger
logDebug *log.Logger
traceCtr uint64
)
func init() {
// Hijack
// flag.BoolVar(&Quiet, "q", Quiet, "")
// flag.BoolVar(&Quiet, "quiet", Quiet, "Disable debug traces")
f, err := os.OpenFile(LogPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0775)
if err != nil {
log.Println("(NON-FATAL) ERROR: Failed to open log file:", err)
} else {
Output = io.MultiWriter(os.Stderr, f)
}
ResetLoggers()
}
func newLogger(prefix aurora.Value) *log.Logger {
return log.New(Output, prefix.Bold().String(), Flags)
}
func ResetLoggers() {
logPanic = newLogger(aurora.BgRed(aurora.White(PrefixPanic)))
logError = newLogger(aurora.Red(PrefixError))
logInfo = newLogger(aurora.Blue(PrefixInfo))
logDebug = newLogger(aurora.Gray(DebugGreyLvl, PrefixDebug))
}
// Trace, n is the argument to skip callers. 0 shows the location of the Trace
// function.
func Trace(n int) string {
if Quiet {
return "<TRACE N/A>"
}
// i := atomic.AddUint64(&traceCtr, 1)
i := 0
_, file1, line1, _ := runtime.Caller(n + 1)
_, file2, line2, _ := runtime.Caller(n + 2)
_, file3, line3, _ := runtime.Caller(n + 3)
_, file4, line4, _ := runtime.Caller(n + 4)
_, file5, line5, _ := runtime.Caller(n + 5)
_, file6, line6, _ := runtime.Caller(n + 6)
file1 = filepath.Base(file1)
file2 = filepath.Base(file2)
file3 = filepath.Base(file3)
file4 = filepath.Base(file4)
file5 = filepath.Base(file5)
file6 = filepath.Base(file6)
return fmt.Sprintf(
"%d ::: %s:%d > %s:%d > %s:%d > %s:%d > %s:%d > %s:%d >",
i, file6, line6, file5, line5, file4, line4, file3, line3, file2, line2, file1, line1,
)
// return fmt.Sprintf(
// "%d ::: %s:%d > %s:%d > %s:%d >",
// i, file3, line3, file2, line2, file1, line1,
// )
}
func Infof(f string, v ...interface{}) {
logInfo.Printf(f, v...)
}
func Infoln(v ...interface{}) {
logInfo.Println(v...)
}
func Printf(f string, v ...interface{}) {
logInfo.Printf(f, v...)
}
func Println(v ...interface{}) {
logInfo.Println(v...)
}
func Debugf(f string, v ...interface{}) {
if Quiet {
return
}
logDebug.Printf(f, v...)
}
func Debugln(v ...interface{}) {
if Quiet {
return
}
logDebug.Println(v...)
}
func Errorf(f string, v ...interface{}) {
logError.Printf(f, v...)
}
func Errorln(v ...interface{}) {
logError.Println(v...)
}
func Panicf(f string, v ...interface{}) {
logPanic.Panicf(f, v...)
}
func Panicln(v ...interface{}) {
logPanic.Panicln(v...)
}
func Fatalf(f string, v ...interface{}) {
logPanic.Fatalf(f, v...)
}
func Fatalln(v ...interface{}) {
logPanic.Fatalln(v...)
}
func Benchmark(thing string) func() {
now := time.Now()
return func() {
Debugln(thing, "took", time.Now().Sub(now))
}
}
func BenchmarkLoop(thing string) (func(), func()) {
now := time.Now()
last := now
duras := []time.Duration{}
return func() {
looped := time.Now()
duras = append(duras, looped.Sub(last))
last = looped
},
func() {
Debugln(thing, "took cumulatively", time.Now().Sub(now), "individually:", duras)
}
}