1
This commit is contained in:
parent
a6abd190f9
commit
7f080f238e
23
sysutils.go
23
sysutils.go
@ -233,3 +233,26 @@ func PrintCallStack() {
|
||||
}
|
||||
fmt.Println(string(buf))
|
||||
}
|
||||
|
||||
func GetCallStack() string {
|
||||
// 获取当前函数的调用者信息
|
||||
pc, file, line, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
fmt.Println("runtime.Caller error")
|
||||
return ""
|
||||
}
|
||||
|
||||
funcName := runtime.FuncForPC(pc).Name()
|
||||
fmt.Printf("Function: %s\nFile: %s\nLine: %d\n", funcName, file, line)
|
||||
|
||||
// 使用Stack打印完整的调用栈信息,可以用于debug
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
n := runtime.Stack(buf, false)
|
||||
if n < len(buf) {
|
||||
break
|
||||
}
|
||||
buf = make([]byte, 2*len(buf))
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user