-
Notifications
You must be signed in to change notification settings - Fork 191
/
stack.go
187 lines (154 loc) · 3.72 KB
/
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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package errorx
import (
"bytes"
"fmt"
"io"
"path/filepath"
"runtime"
"strconv"
)
// stack represents a stack of program counters.
type stack []uintptr
// Format stack trace
func (s *stack) Format(fs fmt.State, verb rune) {
switch verb {
// case 'v', 's':
case 'v':
_, _ = s.WriteTo(fs)
}
}
// StackLen for error
func (s *stack) StackLen() int {
return len(*s)
}
// WriteTo for error
func (s *stack) WriteTo(w io.Writer) (int64, error) {
if len(*s) == 0 {
return 0, nil
}
nn, _ := w.Write([]byte("\nSTACK:\n"))
for _, pc := range *s {
// For historical reasons if pc is interpreted as a uintptr
// its value represents the program counter + 1.
fc := runtime.FuncForPC(pc - 1)
if fc == nil {
continue
}
// file eg: workspace/godev/gookit/goutil/errorx/errorx_test.go
file, line := fc.FileLine(pc - 1)
// f.Name() eg: github.com/gookit/goutil/errorx_test.TestWithPrev()
location := fc.Name() + "()\n " + file + ":" + strconv.Itoa(line) + "\n"
n, _ := w.Write([]byte(location))
nn += n
}
return int64(nn), nil
}
// String format to string
func (s *stack) String() string {
var buf bytes.Buffer
_, _ = s.WriteTo(&buf)
return buf.String()
}
// StackFrames stack frame list
func (s *stack) StackFrames() *runtime.Frames {
return runtime.CallersFrames(*s)
}
// CallerPC the caller PC value in the stack. it is first frame.
func (s *stack) CallerPC() uintptr {
if len(*s) == 0 {
return 0
}
// For historical reasons if pc is interpreted as a uintptr
// its value represents the program counter + 1.
return (*s)[0] - 1
}
/*************************************************************
* For error caller func
*************************************************************/
// Func struct
type Func struct {
*runtime.Func
pc uintptr
}
// FuncForPC create.
func FuncForPC(pc uintptr) *Func {
fc := runtime.FuncForPC(pc)
if fc == nil {
return nil
}
return &Func{
pc: pc,
Func: fc,
}
}
// FileLine returns the file name and line number of the source code
func (f *Func) FileLine() (file string, line int) {
return f.Func.FileLine(f.pc)
}
// Location simple location info for the func
//
// Returns eg:
//
// "github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34"
func (f *Func) Location() string {
file, line := f.FileLine()
return f.Name() + "(), " + filepath.Base(file) + ":" + strconv.Itoa(line)
}
// String of the func
//
// Returns eg:
//
// github.com/gookit/goutil/errorx_test.TestWithPrev()
// At /path/to/github.com/gookit/goutil/errorx_test.go:34
func (f *Func) String() string {
file, line := f.FileLine()
return f.Name() + "()\n At " + file + ":" + strconv.Itoa(line)
}
// MarshalText handle
func (f *Func) MarshalText() ([]byte, error) {
return []byte(f.String()), nil
}
/*************************************************************
* helper func for callers stacks
*************************************************************/
// ErrStackOpt struct
type ErrStackOpt struct {
SkipDepth int
TraceDepth int
}
// default option
var stdOpt = newErrOpt()
// ResetStdOpt config
func ResetStdOpt() {
stdOpt = newErrOpt()
}
func newErrOpt() *ErrStackOpt {
return &ErrStackOpt{
SkipDepth: 3,
TraceDepth: 8,
}
}
// Config the stdOpt setting
func Config(fns ...func(opt *ErrStackOpt)) {
for _, fn := range fns {
fn(stdOpt)
}
}
// SkipDepth setting
func SkipDepth(skipDepth int) func(opt *ErrStackOpt) {
return func(opt *ErrStackOpt) {
opt.SkipDepth = skipDepth
}
}
// TraceDepth setting
func TraceDepth(traceDepth int) func(opt *ErrStackOpt) {
return func(opt *ErrStackOpt) {
opt.TraceDepth = traceDepth
}
}
func callersStack(skip, depth int) *stack {
pcs := make([]uintptr, depth)
num := runtime.Callers(skip, pcs[:])
var st stack = pcs[0:num]
return &st
}