-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallstack.go
More file actions
101 lines (92 loc) · 2.9 KB
/
callstack.go
File metadata and controls
101 lines (92 loc) · 2.9 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
package golog
import (
"fmt"
"runtime"
"strings"
)
// TrimCallStackPathPrefix will be trimmed from the
// beginning of every call-stack file-path.
// Defaults to $GOPATH/src/ of the build environment
// or will be empty if go build gets called with -trimpath.
var TrimCallStackPathPrefix = filePathPrefix()
func filePathPrefix() string {
// This Go package is hosted on GitHub
// so there should always be "github.com"
// in the path of this source file
// if it was cloned using standard go get
_, file, _, _ := runtime.Caller(1)
end := strings.LastIndex(file, "github.com")
if end == -1 {
// panic("expected github.com in call-stack file-path, but got: " + file)
return "" // GitHub action might have it under /home/runner/work/...
}
return file[:end]
}
func callstack(skip int) string {
skip = max(2+skip, 0) // Prefer robustness in logging over negative index panics
stack := make([]uintptr, 32)
n := runtime.Callers(skip, stack)
stack = stack[:n]
var b strings.Builder
frames := runtime.CallersFrames(stack)
for {
frame, _ := frames.Next()
if frame.Function == "" || strings.HasPrefix(frame.Function, "runtime.") {
break
}
_, _ = fmt.Fprintf(
&b,
"%s\n %s:%d\n",
frame.Function,
strings.TrimPrefix(frame.File, TrimCallStackPathPrefix),
frame.Line,
)
}
return b.String()
}
// CallingFunction returns the fully qualified name
// including the package import path of the calling function.
//
// The sum of the optional skipFrames
// callstack frames will be skipped.
func CallingFunction(skipFrames ...int) string {
skip := 2 // This function plus runtime.Callers
for _, n := range skipFrames {
skip += n
}
var stack [1]uintptr
runtime.Callers(skip, stack[:])
name := runtime.FuncForPC(stack[0]).Name()
// Trim generic type parameters from the function name
if generic := strings.IndexByte(name, '['); generic != -1 {
name = name[:generic]
}
return name
}
// CallingFunctionName returns the name of the calling function
// without the package prefix.
//
// The sum of the optional skipFrames
// callstack frames will be skipped.
func CallingFunctionName(skipFrames ...int) string {
name := CallingFunction(append(skipFrames, 1)...)
return name[strings.LastIndex(name, ".")+1:]
}
// CallingFunctionPackageName returns the name of the calling function
// without the package prefix.
//
// The sum of the optional skipFrames
// callstack frames will be skipped.
func CallingFunctionPackageName(skipFrames ...int) string {
name := CallingFunction(append(skipFrames, 1)...)
name = name[strings.LastIndexByte(name, '/')+1:]
return name[:strings.IndexByte(name, '.')]
}
// CallingFunctionPackagePath returns the import path of the calling function.
//
// The sum of the optional skipFrames
// callstack frames will be skipped.
func CallingFunctionPackagePath(skipFrames ...int) string {
name := CallingFunction(append(skipFrames, 1)...)
return name[:strings.LastIndexByte(name, '.')]
}