-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.go
More file actions
160 lines (134 loc) · 3.76 KB
/
stack.go
File metadata and controls
160 lines (134 loc) · 3.76 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
// MIT License
//
// Copyright (c) 2021 Xiantu Li
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package errors
import (
"runtime"
"strings"
_ "unsafe" //nolint:bgolint
"github.com/rs/zerolog"
)
func getPCSlow() (pcs [1]uintptr) {
runtime.Callers(3, pcs[:])
return
}
func buildStackSlow(s []uintptr) int {
return runtime.Callers(3, s[:])
}
func CallersSkip(skip int) (cs []caller) {
pcs := pool.Get().(*[DefaultDepth]uintptr)
for i := range pcs {
pcs[i] = 0
}
n := buildStack(pcs[:]) //仅当特征码使用,有点大材小用
// for i := n; i < DefaultDepth; i++ {
// pcs[i] = 0
// }
//
cs = cacheCallers.Get(pcs, n)
if cs == nil {
pcs1 := make([]uintptr, DefaultDepth)
npc1 := runtime.Callers(baseSkip, pcs1[:DefaultDepth])
cs = parseSlow(pcs1[:npc1])
cacheCallers.Set(pcs, n, cs)
}
pool.Put(pcs)
cs = cs[skip:]
return
}
// CallerFrame 使用 Read-copy update(RCU) 缓存提高性能
func CallerFrame(l uintptr) (c *caller) {
c = cacheCaller.Get(l)
if c != nil {
return
}
cs := parseSlow([]uintptr{l})
if len(cs) > 0 {
c = &cs[0]
cacheCaller.Set(l, c)
}
return
}
type zeroStack struct {
stack []caller
}
func (e zeroStack) MarshalZerologArray(a *zerolog.Array) {
for _, c := range e.stack {
a.Str(c.String())
}
}
func ZerologStack(skip int) zeroStack {
cs := CallersSkip(skip + 1)
return zeroStack{stack: cs}
}
type zeroStackSkip struct {
stack []caller
skips []SkipFrame
}
func (e zeroStackSkip) MarshalZerologArray(a *zerolog.Array) {
for _, c := range e.stack {
for _, opt := range e.skips {
if opt(c.Func, c.File, c.Line) {
return
}
}
a.Str(c.String())
}
}
func ZerologStackWithSkips(skip int, skips ...SkipFrame) zeroStackSkip {
cs := CallersSkip(skip + 1)
return zeroStackSkip{stack: cs, skips: skips}
}
type SkipFrame func(Func string, File string, Line int) bool
func SkipFuncPrefix(pre string) SkipFrame {
return func(Func string, File string, Line int) bool {
return strings.HasPrefix(Func, pre)
}
}
func SkipFilePrefix(pre string) SkipFrame {
return func(Func string, File string, Line int) bool {
return strings.HasPrefix(File, pre)
}
}
func SkipFuncSuffix(pre string) SkipFrame {
return func(Func string, File string, Line int) bool {
return strings.HasSuffix(Func, pre)
}
}
func SkipFileSuffix(pre string) SkipFrame {
return func(Func string, File string, Line int) bool {
return strings.HasSuffix(File, pre)
}
}
func SkipFuncContains(pre string) SkipFrame {
return func(Func string, File string, Line int) bool {
return strings.Contains(Func, pre)
}
}
func SkipFileContains(pre string) SkipFrame {
return func(Func string, File string, Line int) bool {
return strings.Contains(File, pre)
}
}
type PC uintptr
func (p PC) CallerFrame() (c *caller) {
return CallerFrame(uintptr(p))
}