-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
35 lines (30 loc) · 735 Bytes
/
stack.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
package errstack
import (
"fmt"
"runtime"
)
type StackTrace []uintptr
func Callers(skip int) StackTrace {
stack := make([]uintptr, 32)
depth := runtime.Callers(skip + 1, stack)
return StackTrace(stack[0:depth])
}
func (stack StackTrace) FormatStack(msg string, stopFunction *string) []string {
lines := make([]string, 0, 1 + 2*len(stack))
if msg != "" {
lines = append(lines, msg + ":")
}
frames := runtime.CallersFrames(stack)
for {
frame, more := frames.Next()
lines = append(lines, fmt.Sprintf("%s()", frame.Function))
lines = append(lines, fmt.Sprintf("\t%s:%d", frame.File, frame.Line))
if stopFunction != nil && frame.Function == *stopFunction {
break
}
if !more {
break
}
}
return lines
}