-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwithCaller_enabled.go
70 lines (60 loc) · 1.33 KB
/
withCaller_enabled.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
// +build debug
package dlog
import (
"log"
"runtime"
"strings"
"github.com/kirillDanshin/myutils"
)
// D dumps a value
func (*WithCaller) D(v ...interface{}) {
c, _ := GetCaller()
log.Print(c, ": [")
for _, v := range v {
spewInstance.Dump(v)
}
log.Println("]")
}
// F is a build-time enabled printf
func (*WithCaller) F(f string, v ...interface{}) {
c, _ := GetCaller()
// log.Printf(myutils.Concat(c.String(), "[\n\t", f, "\n]"), v...)
spewInstance.Printf(myutils.Concat(c.String(), "[\n\t", f, "\n]"), v...)
}
// P is a build-time enabled print
func (*WithCaller) P(v ...interface{}) {
c, _ := GetCaller()
log.Print(c, ": [\t")
log.Print(v...)
log.Println("]")
}
// Ln is a build-time enabled println
func (*WithCaller) Ln(v ...interface{}) {
c, _ := GetCaller()
log.Print(c, ": [\t")
log.Println(v...)
log.Println("]")
}
// GetCaller returns caller's file, line and func name
func GetCaller(stackBack ...int) (*Caller, bool) {
sb := 2
if len(stackBack) > 0 {
sb = stackBack[0] + 1
}
pc, file, line, ok := runtime.Caller(sb)
if !ok {
return &Caller{
File: CallerUnknown,
Line: 0,
FuncName: CallerUnknown,
}, false
}
if li := strings.LastIndex(file, "/"); li > 0 {
file = file[li+1:]
}
return &Caller{
File: file,
Line: line,
FuncName: runtime.FuncForPC(pc).Name(),
}, true
}