-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
207 lines (190 loc) · 5.81 KB
/
Copy pathstack.go
File metadata and controls
207 lines (190 loc) · 5.81 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package logng
import (
"bytes"
"fmt"
"runtime"
)
// StackCaller stores the information of the stack caller.
// StackCaller can format the given information as string by using String or Format methods.
type StackCaller struct {
runtime.Frame
}
// String is the implementation of fmt.Stringer.
// It is synonym with fmt.Sprintf("%s", c).
func (c StackCaller) String() string {
return fmt.Sprintf("%s", c)
}
// Format is the implementation of fmt.Formatter.
//
// For '%s' (also '%v'):
//
// %s just show function and entry without padding and indent.
// %+s show file path, line and program counter. padding char '\t', default padding 0, default indent 1.
// % s show file path, line and program counter. padding char ' ', default padding 0, default indent 2.
// %+ s exact with '% s'.
// %#s same with '%+s', use file name as file path.
// %+#s exact with '%#s'.
// % #s same with '% s', use file name as file path.
// %+4s same with '%+s', padding 4, indent 1 by default.
// %+.3s same with '%+s', padding 0 by default, indent 3.
// %+4.3s same with '%+s', padding 4, indent 3.
// %+4.s same with '%+s', padding 4, indent 0.
// % 4s same with '% s', padding 4, indent 2 by default.
// % .3s same with '% s', padding 0 by default, indent 3.
// % 4.3s same with '% s', padding 4, indent 3.
// % 4.s same with '% s', padding 4, indent 0.
// %#4.3s same with '%#s', padding 4, indent 3.
// % #4.3s same with '% #s', padding 4, indent 3.
func (c StackCaller) Format(f fmt.State, verb rune) {
buf := bytes.NewBuffer(make([]byte, 0, 4096))
switch verb {
case 's', 'v':
fn := "???"
if c.Function != "" {
fn = c.Function
}
extended := f.Flag('+') || f.Flag(' ') || f.Flag('#')
if !extended {
buf.WriteString(fmt.Sprintf("%s(%#x)", fn, c.Entry))
break
}
pad, wid, prec := getPadWidPrec(f)
padding, indent := bytes.Repeat([]byte{pad}, wid), bytes.Repeat([]byte{pad}, prec)
buf.Write(padding)
buf.WriteString(fmt.Sprintf("%s(%#x)", fn, c.Entry))
buf.WriteRune('\n')
buf.Write(padding)
buf.Write(indent)
file, line := "???", 0
if c.File != "" {
file = c.File
if f.Flag('#') {
file = trimDirs(file)
}
}
if c.Line > 0 {
line = c.Line
}
buf.WriteString(fmt.Sprintf("%s:%d +%#x", file, line, c.PC-c.Entry))
default:
return
}
_, _ = f.Write(buf.Bytes())
}
// StackTrace stores the information of the stack trace.
type StackTrace struct {
programCounters []uintptr
callers []StackCaller
}
// NewStackTrace creates a new StackTrace from program counters.
func NewStackTrace(programCounters []uintptr) *StackTrace {
pc := make([]uintptr, len(programCounters))
copy(pc, programCounters)
return newStackTrace(pc)
}
// CurrentStackTrace creates a new StackTrace at this point.
// size limits maximum program counter size.
// It uses runtime.Callers and uses skip argument as is. So you should increase skip argument by 1.
func CurrentStackTrace(size, skip int) *StackTrace {
pc := make([]uintptr, size)
pc = pc[:runtime.Callers(skip, pc)]
return newStackTrace(pc)
}
// newStackTrace creates a new StackTrace from program counters without copying.
func newStackTrace(programCounters []uintptr) *StackTrace {
t := &StackTrace{
programCounters: programCounters,
callers: make([]StackCaller, 0, len(programCounters)),
}
if len(t.programCounters) > 0 {
frames := runtime.CallersFrames(t.programCounters)
for {
frame, more := frames.Next()
caller := StackCaller{
Frame: frame,
}
t.callers = append(t.callers, caller)
if !more {
break
}
}
}
return t
}
// Clone clones the underlying StackTrace.
func (t *StackTrace) Clone() *StackTrace {
if t == nil {
return nil
}
t2 := &StackTrace{
programCounters: make([]uintptr, len(t.programCounters)),
callers: make([]StackCaller, len(t.callers)),
}
copy(t2.programCounters, t.programCounters)
copy(t2.callers, t.callers)
return t2
}
// String is the implementation of fmt.Stringer.
// It is synonym with fmt.Sprintf("%s", t).
func (t *StackTrace) String() string {
return fmt.Sprintf("%s", t)
}
// Format is the implementation of fmt.Formatter.
// Format lists all StackCaller's in the underlying StackTrace line by line with the given format.
func (t *StackTrace) Format(f fmt.State, verb rune) {
buf := bytes.NewBuffer(make([]byte, 0, 4096))
switch verb {
case 's', 'v':
format := "%"
for _, r := range []rune{'+', ' ', '#'} {
if f.Flag(int(r)) {
format += string(r)
}
}
_, wid, prec := getPadWidPrec(f)
format += fmt.Sprintf("%d.%ds", wid, prec)
for i, c := range t.callers {
if i > 0 {
buf.WriteRune('\n')
}
buf.WriteString(fmt.Sprintf(format, c))
}
default:
return
}
_, _ = f.Write(buf.Bytes())
}
// ProgramCounters returns program counters.
func (t *StackTrace) ProgramCounters() []uintptr {
result := make([]uintptr, len(t.programCounters))
copy(result, t.programCounters)
return result
}
// SizeOfProgramCounters returns the size of program counters.
func (t *StackTrace) SizeOfProgramCounters() int {
return len(t.programCounters)
}
// ProgramCounter returns a program counter on the given index. It panics if index is out of range.
func (t *StackTrace) ProgramCounter(index int) uintptr {
if 0 > index || index >= len(t.programCounters) {
panic("index out of range")
}
return t.programCounters[index]
}
// Callers returns StackCaller's.
func (t *StackTrace) Callers() []StackCaller {
result := make([]StackCaller, len(t.callers))
copy(result, t.callers)
return result
}
// SizeOfCallers returns the size of StackCaller's.
func (t *StackTrace) SizeOfCallers() int {
return len(t.callers)
}
// Caller returns a caller on the given index. It panics if index is out of range.
func (t *StackTrace) Caller(index int) StackCaller {
if 0 > index || index >= len(t.callers) {
panic("index out of range")
}
return t.callers[index]
}