Created
November 23, 2019 14:13
-
-
Save songfei1983/e1661016fb8948b2d08d1f633a038544 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ( | |
re = regexp.MustCompile(`^(\S.+)\.(\S.+)$`) | |
) | |
type CallerInfo struct { | |
PackageName string | |
FunctionName string | |
FileName string | |
FileLine int | |
} | |
func Dump() (callerInfo []*CallerInfo) { | |
for i := 1; ; i++ { | |
pc, _, _, ok := runtime.Caller(i) // https://golang.org/pkg/runtime/#Caller | |
if !ok { | |
break | |
} | |
fn := runtime.FuncForPC(pc) | |
fileName, fileLine := fn.FileLine(pc) | |
_fn := re.FindStringSubmatch(fn.Name()) | |
callerInfo = append(callerInfo, &CallerInfo{ | |
PackageName: _fn[1], | |
FunctionName: _fn[2], | |
FileName: fileName, | |
FileLine: fileLine, | |
}) | |
} | |
for i := len(callerInfo) - 1; i > -1; i-- { | |
v := callerInfo[i] | |
fmt.Printf("%02d: %s.%s@%s:%d\n", i, v.PackageName, v.FunctionName, v.FileName, v.FileLine) | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment