-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.go
65 lines (58 loc) · 1.04 KB
/
trace.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
package debug
import (
"runtime"
"strings"
)
const (
traceStepBack int = 2
packageSeparator string = `/`
)
// Trace
type trace struct {
Package string
Function string
File string
Line int
Stack string
}
func newTrace() *trace {
return new(trace)
}
func (t *trace) Trace(level int) (trc *trace) {
var (
ok bool
pc uintptr
fn *runtime.Func
buf []byte
tmp []string
n int
)
if level == 0 {
level = traceStepBack
}
buf = make([]byte, 1<<16)
pc, t.File, t.Line, ok = runtime.Caller(level)
if ok == true {
fn = runtime.FuncForPC(pc)
if fn != nil {
t.Function = fn.Name()
}
n = runtime.Stack(buf, true)
t.Stack = string(buf[:n])
tmp = strings.Split(t.Function, packageSeparator)
if len(tmp) > 1 {
t.Package += strings.Join(tmp[:len(tmp)-1], packageSeparator)
t.Function = tmp[len(tmp)-1]
}
tmp = strings.SplitN(t.Function, `.`, 2)
if len(tmp) == 2 {
if t.Package != "" {
t.Package += packageSeparator
}
t.Package += tmp[0]
t.Function = tmp[1]
}
}
trc = t
return
}