caller.go 548 B

1234567891011121314151617181920212223242526272829
  1. package caller
  2. import (
  3. "runtime"
  4. "strings"
  5. )
  6. func callerInfo(i int) string {
  7. ptr, _, _, ok := runtime.Caller(i)
  8. fName := "unknown"
  9. if ok {
  10. f := runtime.FuncForPC(ptr)
  11. if f != nil {
  12. // f.Name() is like: github.com/docker/libnetwork/caller.MethodName
  13. tmp := strings.Split(f.Name(), ".")
  14. if len(tmp) > 0 {
  15. fName = tmp[len(tmp)-1]
  16. }
  17. }
  18. }
  19. return fName
  20. }
  21. // Name returns the name of the function at the specified level
  22. // level == 0 means current method name
  23. func Name(level int) string {
  24. return callerInfo(2 + level)
  25. }