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.
crate/log/logsdk/runtime.go

59 lines
1.1 KiB
Go

package logsdk
import (
"runtime"
)
const (
maximumFrames = 32
getCallerSkipOffset = 2
entrySkipOffset = 2
)
// 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+getCallerSkipOffset, 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+getCallerSkipOffset, 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
}