-
Notifications
You must be signed in to change notification settings - Fork 82
/
trace.go
44 lines (40 loc) · 910 Bytes
/
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
package goutil
import (
"bytes"
"runtime"
"strconv"
"strings"
)
// PanicTrace trace panic stack info.
func PanicTrace(kb int) []byte {
s := []byte("/src/runtime/panic.go")
e := []byte("\ngoroutine ")
line := []byte("\n")
stack := make([]byte, kb<<10) //KB
length := runtime.Stack(stack, true)
start := bytes.Index(stack, s)
if start == -1 {
start = 0
}
stack = stack[start:length]
start = bytes.Index(stack, line) + 1
stack = stack[start:]
end := bytes.LastIndex(stack, line)
if end != -1 {
stack = stack[:end]
}
end = bytes.Index(stack, e)
if end != -1 {
stack = stack[:end]
}
stack = bytes.TrimRight(stack, "\n")
return stack
}
// GetCallLine gets caller line information.
func GetCallLine(calldepth int) string {
_, file, line, ok := runtime.Caller(calldepth + 1)
if !ok {
return "???:0"
}
return file[strings.LastIndex(file, "/src/")+5:] + ":" + strconv.Itoa(line)
}