-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug.go
More file actions
42 lines (35 loc) · 780 Bytes
/
debug.go
File metadata and controls
42 lines (35 loc) · 780 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
package otk
import (
"path/filepath"
"runtime"
"strings"
)
func init() {
initRelative()
}
var prefixPath string
func initRelative() {
_, fileName, _, _ := runtime.Caller(0)
prefixPath = filepath.Dir(fileName)
}
// Caller returns function, file and line of the caller.
func Caller(skip int, trim bool) (function, file string, line int) {
var uframes [3]uintptr
runtime.Callers(skip+1, uframes[:])
frames := runtime.CallersFrames(uframes[:])
if _, ok := frames.Next(); !ok {
return "", "", 0
}
fr, ok := frames.Next()
if !ok {
return
}
function, file, line = fr.Function, fr.File, fr.Line
if trim {
if idx := strings.LastIndexByte(function, '/'); idx != -1 {
function = function[idx+1:]
}
file = strings.TrimPrefix(file, prefixPath)
}
return
}