-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_frame.go
More file actions
44 lines (37 loc) · 1.11 KB
/
stack_frame.go
File metadata and controls
44 lines (37 loc) · 1.11 KB
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 errors
import (
"fmt"
"runtime"
"strconv"
)
// StackFrame describes content of a single stack frame stored with error.
type StackFrame struct {
Function string `json:"func"`
File string `json:"file"`
Line int `json:"line"`
}
func (s StackFrame) String() string {
return fmt.Sprintf("%s:%d %s", s.File, s.Line, s.Function)
}
var (
// CallerFramesFunc holds default function used by function Catch()
// to collect call frames.
CallerFramesFunc func(offset int) []StackFrame = DefaultCallerFrames
// CallingStackMaxLen holds maximum elements in the call frames.
CallingStackMaxLen int = 16
)
// DefaultCallerFrames returns default implementation of call frames collector.
func DefaultCallerFrames(offset int) []StackFrame {
res := make([]StackFrame, 0, CallingStackMaxLen)
pc := make([]uintptr, CallingStackMaxLen)
n := runtime.Callers(offset, pc)
frames := runtime.CallersFrames(pc[:n])
for {
frame, more := frames.Next()
if !more {
break
}
res = append(res, StackFrame{Function: frame.Function, File: frame.File + ":" + strconv.Itoa(frame.Line), Line: frame.Line})
}
return res
}