forked from nqmt/goerror
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstacktrace.go
54 lines (42 loc) · 900 Bytes
/
stacktrace.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
package goerror
import (
"fmt"
"runtime"
)
var (
DefaultStackTraceCap = 15
DefaultStackTraceSkipLine = 2
)
type frame struct {
funcName string
line int
path string
}
func (e *GoError) StackTrace() string {
stackTrance := ""
for _, frame := range e.frames {
stackTrance += fmt.Sprintf("%s: %s %d \n", frame.funcName, frame.path, frame.line)
}
return stackTrance
}
func trace(skip int) []*frame {
frames := make([]*frame, 0)
i := 0
for {
if i >= DefaultStackTraceCap {
break
}
pc, path, line, ok := runtime.Caller(skip)
if !ok {
break
}
frames = append(frames, &frame{
funcName: runtime.FuncForPC(pc).Name(),
line: line,
path: path,
})
skip++
i++
}
return frames
}