123456789101112131415161718192021222324252627282930313233343536373839 |
- //@File callerfunc.go
- //@Time 2022/05/16
- //@Author #Suyghur,
- package utils
- import (
- "fmt"
- "runtime"
- "strings"
- )
- func CallerFuncName() string {
- pc := make([]uintptr, 1)
- runtime.Callers(3, pc)
- f := runtime.FuncForPC(pc[0])
- return f.Name()
- }
- func CallerFuncLine() string {
- pc := make([]uintptr, 1)
- runtime.Callers(2, pc)
- f := runtime.FuncForPC(pc[0])
- file, line := f.FileLine(pc[0])
- return fmt.Sprintf("%s@%d", file, line)
- }
- func cleanUpFuncName(funcName string) string {
- end := strings.LastIndex(funcName, ".")
- if end == -1 {
- return ""
- }
- return funcName[end+1:]
- }
- func GetSelfFuncName() string {
- pc, _, _, _ := runtime.Caller(1)
- return cleanUpFuncName(runtime.FuncForPC(pc).Name())
- }
|