-
Notifications
You must be signed in to change notification settings - Fork 3
/
trace_frame.go
91 lines (88 loc) · 1.9 KB
/
trace_frame.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
package system
import (
"bytes"
"runtime"
"strconv"
"strings"
)
func TraceFrame2Buffer(frameC int, buffer *bytes.Buffer) {
if frameC < 1 {
return
}
for i := 1; i < frameC+1; i++ {
if pc, file, line, ok := runtime.Caller(i); !ok {
break
} else {
if strings.HasPrefix(file,"runtime/") || strings.Index(file,"!dx!common!lib@") > 0{
break
}
if i > 1 {
buffer.Write([]byte{'\r', '\n'})
}
idx := strings.LastIndexByte(file, '/')
if idx == -1 {
buffer.WriteString(file)
} else {
idx = strings.LastIndexByte(file[:idx], '/')
if idx == -1 {
buffer.WriteString(file)
} else {
buffer.WriteString(file[idx+1:])
}
}
buffer.WriteByte(':')
buffer.WriteString(strconv.Itoa(line))
funcName := runtime.FuncForPC(pc).Name()
idx = strings.IndexByte(funcName, '.')
if idx > 0 {
funcName = funcName[idx+1:]
}
buffer.WriteByte('.')
buffer.WriteString(funcName)
if strings.Index(file,"/main.go")>0{
break
}
}
}
}
func TraceFrame(frameC int, bt []byte) []byte {
if frameC < 1 {
return nil
}
for i := 1; i < frameC+1; i++ {
if pc, file, line, ok := runtime.Caller(i); !ok {
break
} else {
if strings.HasPrefix(file,"runtime/") || strings.Index(file,"!dx!common!lib@") > 0{
break
}
if i > 1 {
bt = append(bt, '\r', '\n')
}
idx := strings.LastIndexByte(file, '/')
if idx == -1 {
bt = append(bt, file...)
} else {
idx = strings.LastIndexByte(file[:idx], '/')
if idx == -1 {
bt = append(bt, file...)
} else {
bt = append(bt, file[idx+1:]...)
}
}
bt = append(bt, ':')
bt = append(bt, strconv.Itoa(line)...)
funcName := runtime.FuncForPC(pc).Name()
idx = strings.IndexByte(funcName, '.')
if idx > 0 {
funcName = funcName[idx+1:]
}
bt = append(bt, '.')
bt = append(bt, funcName...)
if strings.Index(file,"/main.go")>0{
break
}
}
}
return bt
}