2018-07-16 20:34:20 -04:00
|
|
|
package caller
|
2017-07-27 14:43:13 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func callerInfo(i int) string {
|
|
|
|
ptr, _, _, ok := runtime.Caller(i)
|
|
|
|
fName := "unknown"
|
|
|
|
if ok {
|
|
|
|
f := runtime.FuncForPC(ptr)
|
|
|
|
if f != nil {
|
2018-07-16 20:34:20 -04:00
|
|
|
// f.Name() is like: github.com/docker/libnetwork/caller.MethodName
|
2017-07-27 14:43:13 -04:00
|
|
|
tmp := strings.Split(f.Name(), ".")
|
|
|
|
if len(tmp) > 0 {
|
|
|
|
fName = tmp[len(tmp)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fName
|
|
|
|
}
|
|
|
|
|
2018-07-16 20:34:20 -04:00
|
|
|
// Name returns the name of the function at the specified level
|
2017-07-27 14:43:13 -04:00
|
|
|
// level == 0 means current method name
|
2018-07-16 20:34:20 -04:00
|
|
|
func Name(level int) string {
|
2017-07-27 14:43:13 -04:00
|
|
|
return callerInfo(2 + level)
|
|
|
|
}
|