v1.0.0
This commit is contained in:
60
traceback.go
Normal file
60
traceback.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package slog
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func getTraceback() *MethodTraceback {
|
||||
caller, _, _, _ := runtime.Caller(5)
|
||||
details := runtime.FuncForPC(caller)
|
||||
signature := details.Name()
|
||||
path, line := details.FileLine(caller)
|
||||
splitPath := strings.Split(path, "/")
|
||||
|
||||
splitSignature := strings.Split(signature, ".")
|
||||
method := splitSignature[len(splitSignature)-1]
|
||||
|
||||
tb := &MethodTraceback{
|
||||
Filename: splitPath[len(splitPath)-1],
|
||||
FullPath: path,
|
||||
Line: line,
|
||||
Signature: signature,
|
||||
Method: method,
|
||||
}
|
||||
|
||||
return tb
|
||||
}
|
||||
func getFullTraceback(skip int) []*MethodTraceback {
|
||||
pc := make([]uintptr, 15)
|
||||
runtime.Callers(skip, pc)
|
||||
list := make([]*MethodTraceback, 0)
|
||||
frames := runtime.CallersFrames(pc)
|
||||
for {
|
||||
frame, more := frames.Next()
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
details := runtime.FuncForPC(frame.PC)
|
||||
signature := details.Name()
|
||||
path, line := details.FileLine(frame.PC)
|
||||
splitPath := strings.Split(path, "/")
|
||||
|
||||
splitSignature := strings.Split(signature, ".")
|
||||
method := splitSignature[len(splitSignature)-1]
|
||||
|
||||
tb := &MethodTraceback{
|
||||
Filename: splitPath[len(splitPath)-1],
|
||||
FullPath: path,
|
||||
Line: line,
|
||||
Signature: signature,
|
||||
Method: method,
|
||||
}
|
||||
list = append(list, tb)
|
||||
}
|
||||
sort.Slice(list, func(i, j int) bool {
|
||||
return j < i
|
||||
})
|
||||
return list
|
||||
}
|
||||
Reference in New Issue
Block a user