-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
48 lines (41 loc) · 764 Bytes
/
utils.go
File metadata and controls
48 lines (41 loc) · 764 Bytes
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
package log
import (
"fmt"
"runtime"
"strings"
)
func TrimPath(file string) string {
idx := strings.LastIndexByte(file, '/')
if idx == -1 {
return file
}
return file[idx+1:]
}
func GetStackInString(skip int, depth int) (stack []string) {
rawStack := GetStack(skip, depth)
for _, frame := range rawStack {
caller := fmt.Sprintf("%v %v:%v",
TrimPath(frame.Function),
frame.File,
frame.Line,
)
stack = append(stack, caller)
}
return
}
func GetStack(skip int, depth int) (stack []runtime.Frame) {
pc := make([]uintptr, depth)
n := runtime.Callers(skip, pc)
if n == 0 {
return
}
frames := runtime.CallersFrames(pc[:n])
for {
frame, more := frames.Next()
stack = append(stack, frame)
if !more {
break
}
}
return
}