-
Notifications
You must be signed in to change notification settings - Fork 33
/
extern.go
38 lines (32 loc) · 941 Bytes
/
extern.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
package runtime
import (
"fmt"
"path"
"runtime"
"strings"
)
// GetCaller returns the caller of the function that calls it.
//The argument skip is the number of stack frames to skip before recording in pc, with 0 identifying the frame for Callers itself and 1 identifying the caller of Callers
func GetCallerWithSkip(skip int) string {
var pc [1]uintptr
runtime.Callers(skip+1, pc[:])
f := runtime.FuncForPC(pc[0])
if f == nil {
return fmt.Sprintf("Unable to find caller")
}
return f.Name()
}
func GetCaller() string {
//4 skip, 1 GetCaller, 1 GetCallerWithSkip, 1 runtime.Callers, 1 caller of GetCaller
return GetCallerWithSkip(3)
}
func GetShortCaller() string {
fn := GetCallerWithSkip(3)
return strings.TrimPrefix(path.Ext(fn), ".")
}
func GetCallStackTrace() string {
const size = 64 << 10
stacktrace := make([]byte, size)
stacktrace = stacktrace[:runtime.Stack(stacktrace, false)]
return string(stacktrace)
}