Commit cf55754e authored by Kirill Smelkov's avatar Kirill Smelkov

my: Use runtime.CallersFrames instead of runtime.FuncForPC

Because in general case runtime.CallersFrames is more accurate than
runtime.FuncForPC - e.g. the latter does not correctly work with inlined
functions.
parent 98249b24
...@@ -24,29 +24,29 @@ import ( ...@@ -24,29 +24,29 @@ import (
"strings" "strings"
) )
func _myfuncname(nskip int) string { func _myframe(nskip int) runtime.Frame {
pcv := [1]uintptr{} pcv := [1]uintptr{}
runtime.Callers(nskip, pcv[:]) n := runtime.Callers(nskip, pcv[:])
f := runtime.FuncForPC(pcv[0]) if n != 1 {
if f == nil { panic("error determining caller")
return ""
} }
return f.Name() frameit := runtime.CallersFrames(pcv[:])
f, _ := frameit.Next()
return f
} }
// FuncName returns name of currently running function (caller of FuncName()) // FuncName returns name of currently running function (caller of FuncName())
// name is fully qualified package/name.function(.x) // name is fully qualified package/name.function(.x)
func FuncName() string { func FuncName() string {
return _myfuncname(3) f := _myframe(3)
return f.Function
} }
// PkgName returns name of currently running function's package // PkgName returns name of currently running function's package
// package is fully qualified package/name // package is fully qualified package/name
func PkgName() string { func PkgName() string {
myfunc := _myfuncname(3) f := _myframe(3)
if myfunc == "" { myfunc := f.Function
return ""
}
// NOTE dots in package name are after last slash are escaped by go as %2e // NOTE dots in package name are after last slash are escaped by go as %2e
// this way the first '.' after last '/' is delimiter between package and function // this way the first '.' after last '/' is delimiter between package and function
// //
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment