-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
48 lines (38 loc) · 1000 Bytes
/
runtime.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
package errors
import (
"fmt"
"path"
"runtime"
)
var (
showFuncName = true
showPackageName = true
)
// SetShowFuncName sets whether to show the function name in the stack trace before the file and line
func SetShowFuncName(state bool) {
showFuncName = state
}
// SetShowPackageName sets whether to show the full function name (package name/function name)
func SetShowPackageName(state bool) {
showPackageName = state
}
// getCallerPath returns the file and line which called any of New functions as string.
//
// skipFrames parameter defines how many functions to skip.
func getCallerPath(skipFrames int) string {
pc, file, line, ok := runtime.Caller(2 + skipFrames)
if !ok {
return "<no source>"
}
f := runtime.FuncForPC(pc).Name()
if showFuncName {
if !showPackageName {
f = stripPackageName(f)
}
return fmt.Sprintf("at %s %s:%d", f, file, line)
}
return fmt.Sprintf("%s:%d", file, line)
}
func stripPackageName(name string) string {
return path.Base(name)
}