github.com/moby/docker@v26.1.3+incompatible/libnetwork/internal/caller/caller.go (about)

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