-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
54 lines (47 loc) · 1.43 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
//=============================================================
// log.go
//-------------------------------------------------------------
// Various logging functions
//=============================================================
package utilz
import (
"fmt"
"runtime"
"strings"
"time"
)
func chopPath(original string) string {
i := strings.LastIndex(original, "/")
if i == -1 {
return original
} else {
return original[i+1:]
}
}
func Debug(msg string, vars ...interface{}) {
print_(msg, "\x1b[43;34m", "DEBUG", vars)
}
func Error(msg string, vars ...interface{}) {
print_(msg, "\x1b[41;1m", "ERROR", vars)
}
func Warning(msg string, vars ...interface{}) {
print_(msg, "\x1b[43;1m", "WARNING", vars)
}
func print_(msg, color, type_ string, vars ...interface{}) {
function, file, line, _ := runtime.Caller(2)
for _, v := range vars {
txt := fmt.Sprintf("%v", v)
txt = strings.Trim(txt, "[")
txt = strings.Trim(txt, "]")
msg = fmt.Sprintf("%v %v", msg, txt)
}
fmt.Printf("%v[%v][%s:%d][%s][%v]\x1b[0;1m %v\n", color, time.Now().Format("15:04:05.000"), chopPath(file), line, runtime.FuncForPC(function).Name(), type_, msg)
}
func PrintMemoryUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
Debug(fmt.Sprintf("\tAlloc = %v MiB", m.Alloc/1024/1024))
Debug(fmt.Sprintf("\tTotalAlloc = %v MiB", m.TotalAlloc/1024/1024))
Debug(fmt.Sprintf("\tSys = %v MiB", m.Sys/1024/1024))
Debug(fmt.Sprintf("\tNumGC = %v\n", m.NumGC))
}