github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/caller/caller.go (about)

     1  package caller
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  )
     7  
     8  type Caller struct {
     9  	Name string
    10  	File string
    11  	Line int
    12  }
    13  
    14  func (c Caller) String() string {
    15  	return fmt.Sprintf("called from %s on %s#%d", c.Name, c.File, c.Line)
    16  }
    17  
    18  func NewCaller() Caller {
    19  	var caller Caller
    20  	pc, file, line, ok := runtime.Caller(1)
    21  	details := runtime.FuncForPC(pc)
    22  	if ok && details != nil {
    23  		caller.File = file
    24  		caller.Line = line
    25  		caller.Name = details.Name()
    26  	}
    27  	return caller
    28  }