diff --git a/sysutils.go b/sysutils.go index 3dccf9c..3ce82e2 100644 --- a/sysutils.go +++ b/sysutils.go @@ -9,6 +9,7 @@ import ( "os" "reflect" "time" + "runtime" ) func GetDaySeconds(seconds int64, timeZone int64) int64 { @@ -209,3 +210,26 @@ func AppendSlice[T any](s *[]T, val T) { func GetTypeName(v interface{}) string { return reflect.TypeOf(v).String() } + +func PrintCallStack() { +// 获取当前函数的调用者信息 + 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)) + } + fmt.Println(string(buf)) +}