-
Notifications
You must be signed in to change notification settings - Fork 1
/
stacktrace.go
60 lines (53 loc) · 1.15 KB
/
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
55
56
57
58
59
60
package tracer
import (
"fmt"
"path"
"runtime"
"strings"
)
const DefaultDepth = 32
type Caller struct {
File string
Function string
Line int
}
func (c Caller) String() string {
_, file := path.Split(c.File)
return fmt.Sprintf("at %s(%s:%d)", c.Function, file, c.Line)
}
type StackTrace []Caller
func GetStackTrace(skip int, maxDepth ...int) StackTrace {
maxDepth = append(maxDepth, DefaultDepth)
depth := maxDepth[0] + skip
var stack StackTrace
for i := skip; i < depth; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
funcName := "unknown"
f := runtime.FuncForPC(pc)
if f != nil {
funcName = f.Name()
}
stack = append(stack, Caller{
File: file,
Function: funcName,
Line: line,
})
if funcName == "main.main" {
break
}
}
return stack
}
func (st StackTrace) String() string {
var formatted strings.Builder
for i := range st {
caller := st[len(st)-1-i]
prefix := strings.Repeat(" ", i)
// fmt.Fprintf(&formatted, "%s%s at %s:%d\n", prefix, caller.Function, caller.File, caller.Line)
fmt.Fprintf(&formatted, "%s%s\n", prefix, caller.String())
}
return formatted.String()
}