forked from go-ozzo/ozzo-log
-
Notifications
You must be signed in to change notification settings - Fork 2
/
callstack.go
69 lines (63 loc) · 1.42 KB
/
callstack.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
package log
import (
"bytes"
"fmt"
"runtime"
"strings"
)
var (
DefaultStackDepth = 5
DefaultSkipStack = 3
DefaultStackFilter = `github.com/admpub/log`
)
// CallStack 调用栈信息
type CallStack struct {
Depth int
Skip int
Filters []string
}
// GetCallStack returns the current call stack information as a string.
// The skip parameter specifies how many top frames should be skipped, while
// the frames parameter specifies at most how many frames should be returned.
func GetCallStack(skip int, frames int, filters ...string) string {
buf := new(bytes.Buffer)
hasFilter := len(filters) > 0
for i, count := skip, 0; count < frames; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
if !hasFilter || !matchFilters(file, filters) {
fmt.Fprintf(buf, "\n%s:%d", file, line)
count++
}
}
return buf.String()
}
func matchFilters(source string, filters []string) bool {
for _, filter := range filters {
if len(filter) == 0 {
continue
}
if strings.Contains(source, filter) {
return true
}
}
return false
}
// GetCallSingleStack 获取单个记录
func GetCallSingleStack(skip int, filters ...string) (fileName string, lineNo int, found bool) {
hasFilter := len(filters) > 0
for i := skip; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
return
}
if !hasFilter || !matchFilters(file, filters) {
fileName = file
lineNo = line
found = ok
return
}
}
}