forked from pkg/errors
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
Latest commit
178 lines (164 loc) · 4.31 KB
/
Copy pathstack.go
File metadata and controls
178 lines (164 loc) · 4.31 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
package errors
import (
"fmt"
"io"
"path"
"runtime"
"strings"
)
// Frame represents a program counter inside a stack frame.
typeFrameuintptr
// pc returns the program counter for this frame;
// multiple frames may have the same PC value.
func (fFrame) pc() uintptr { returnuintptr(f) -1 }
// file returns the full path to the file that contains the
// function for this Frame's pc.
func (fFrame) file() string {
fn:=runtime.FuncForPC(f.pc())
iffn==nil {
return"unknown"
}
file, _:=fn.FileLine(f.pc())
returnfile
}
// line returns the line number of source code of the
// function for this Frame's pc.
func (fFrame) line() int {
fn:=runtime.FuncForPC(f.pc())
iffn==nil {
return0
}
_, line:=fn.FileLine(f.pc())
returnline
}
// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s path of source file relative to the compile time GOPATH
// %+v equivalent to %+s:%d
func (fFrame) Format(s fmt.State, verbrune) {
switchverb {
case's':
switch {
cases.Flag('+'):
pc:=f.pc()
fn:=runtime.FuncForPC(pc)
iffn==nil {
io.WriteString(s, "unknown")
} else {
file, _:=fn.FileLine(pc)
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
}
default:
io.WriteString(s, path.Base(f.file()))
}
case'd':
fmt.Fprintf(s, "%d", f.line())
case'n':
name:=runtime.FuncForPC(f.pc()).Name()
io.WriteString(s, funcname(name))
case'v':
f.Format(s, 's')
io.WriteString(s, ":")
f.Format(s, 'd')
}
}
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
typeStackTrace []Frame
func (stStackTrace) Format(s fmt.State, verbrune) {
switchverb {
case'v':
switch {
cases.Flag('+'):
for_, f:=rangest {
fmt.Fprintf(s, "\n%+v", f)
}
cases.Flag('#'):
fmt.Fprintf(s, "%#v", []Frame(st))
default:
fmt.Fprintf(s, "%v", []Frame(st))
}
case's':
fmt.Fprintf(s, "%s", []Frame(st))
}
}
// stack represents a stack of program counters.
typestack []uintptr
func (s*stack) Format(st fmt.State, verbrune) {
switchverb {
case'v':
switch {
casest.Flag('+'):
for_, pc:=range*s {
f:=Frame(pc)
fmt.Fprintf(st, "\n%+v", f)
}
}
}
}
func (s*stack) StackTrace() StackTrace {
f:=make([]Frame, len(*s))
fori:=0; i<len(f); i++ {
f[i] =Frame((*s)[i])
}
returnf
}
funccallers() *stack {
constdepth=32
varpcs [depth]uintptr
n:=runtime.Callers(3, pcs[:])
varststack=pcs[0:n]
return&st
}
// funcname removes the path prefix component of a function's name reported by func.Name().
funcfuncname(namestring) string {
i:=strings.LastIndex(name, "/")
name=name[i+1:]
i=strings.Index(name, ".")
returnname[i+1:]
}
functrimGOPATH(name, filestring) string {
// Here we want to get the source file path relative to the compile time
// GOPATH. As of Go 1.6.x there is no direct way to know the compiled
// GOPATH at runtime, but we can infer the number of path segments in the
// GOPATH. We note that fn.Name() returns the function name qualified by
// the import path, which does not include the GOPATH. Thus we can trim
// segments from the beginning of the file path until the number of path
// separators remaining is one more than the number of path separators in
// the function name. For example, given:
//
// GOPATH /home/user
// file /home/user/src/pkg/sub/file.go
// fn.Name() pkg/sub.Type.Method
//
// We want to produce:
//
// pkg/sub/file.go
//
// From this we can easily see that fn.Name() has one less path separator
// than our desired output. We count separators from the end of the file
// path until it finds two more than in the function name and then move
// one character forward to preserve the initial path segment without a
// leading separator.
constsep="/"
goal:=strings.Count(name, sep) +2
i:=len(file)
forn:=0; n<goal; n++ {
i=strings.LastIndex(file[:i], sep)
ifi==-1 {
// not enough separators found, set i so that the slice expression
// below leaves file unmodified
i=-len(sep)
break
}
}
// get back to 0 or trim the leading separator
file=file[i+len(sep):]
returnfile
}