You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
975 B
Go
53 lines
975 B
Go
1 year ago
|
package logsdk
|
||
|
|
||
|
import (
|
||
|
"runtime"
|
||
|
)
|
||
|
|
||
|
// Frame 调用相关信息
|
||
|
type Frame struct {
|
||
|
Function string `json:"func"`
|
||
|
File string `json:"file"`
|
||
|
Line int `json:"line"`
|
||
|
}
|
||
|
|
||
|
// IsValid 表示是否有效
|
||
|
func (frame Frame) IsValid() bool {
|
||
|
return frame.Line > 0
|
||
|
}
|
||
|
|
||
|
func getCaller(skip int) Frame {
|
||
|
pc := make([]uintptr, 1)
|
||
|
n := runtime.Callers(skip+2, pc)
|
||
|
frame, _ := runtime.CallersFrames(pc[:n]).Next()
|
||
|
if frame.PC == 0 {
|
||
|
return Frame{}
|
||
|
}
|
||
|
return Frame{
|
||
|
Function: frame.Function,
|
||
|
File: frame.File,
|
||
|
Line: frame.Line,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func getStack(skip, maximumFrames int) []Frame {
|
||
|
pc := make([]uintptr, maximumFrames)
|
||
|
n := runtime.Callers(skip+2, pc)
|
||
|
stack := make([]Frame, 0, n)
|
||
|
frames := runtime.CallersFrames(pc[:n])
|
||
|
for {
|
||
|
frame, more := frames.Next()
|
||
|
if frame.PC != 0 {
|
||
|
stack = append(stack, Frame{
|
||
|
Function: frame.Function,
|
||
|
File: frame.File,
|
||
|
Line: frame.Line,
|
||
|
})
|
||
|
}
|
||
|
if !more {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
return stack
|
||
|
}
|