154 lines
2.8 KiB
Go
154 lines
2.8 KiB
Go
package Models
|
|
|
|
import (
|
|
"AnP/Utils"
|
|
"encoding/json"
|
|
"runtime/debug"
|
|
"strconv"
|
|
)
|
|
|
|
type DebugStackLineModel struct {
|
|
Line int
|
|
Method string
|
|
File string
|
|
}
|
|
|
|
func NewDebugStackLineModel(line int, file string, method string) DebugStackLineModel {
|
|
return DebugStackLineModel{
|
|
Line: line,
|
|
File: file,
|
|
Method: method,
|
|
}
|
|
}
|
|
|
|
func NewDebugStackLineFromMatch(match Utils.PatternMatch) DebugStackLineModel {
|
|
|
|
line, _ := strconv.ParseInt(match.Groups[4], 10, 32)
|
|
|
|
return NewDebugStackLineModel(int(line), match.Groups[3], match.Groups[1])
|
|
}
|
|
|
|
func (_self *DebugStackLineModel) ToDictionary() map[string]any {
|
|
return map[string]any{
|
|
"line": _self.Line,
|
|
"file": _self.File,
|
|
"method": _self.Method,
|
|
}
|
|
}
|
|
|
|
func DebugStackToDictionaries(lines []DebugStackLineModel) []map[string]any {
|
|
|
|
var stack []map[string]any = []map[string]any{}
|
|
|
|
for _, line := range lines {
|
|
stack = append(stack, line.ToDictionary())
|
|
}
|
|
|
|
return stack
|
|
}
|
|
|
|
func GetTraceStack(i int) []DebugStackLineModel {
|
|
|
|
var stack_map []DebugStackLineModel = []DebugStackLineModel{}
|
|
|
|
Utils.Patterns.Replace(string(debug.Stack()), Utils.Patterns.RE_TRACE_STACK, func(match Utils.PatternMatch) string {
|
|
|
|
stack_map = append(stack_map, NewDebugStackLineFromMatch(match))
|
|
|
|
return ""
|
|
})
|
|
|
|
return stack_map[i+2:]
|
|
}
|
|
|
|
func DebugPrintPointer[T any](item *T) {
|
|
print("*")
|
|
if item == nil {
|
|
print("nil")
|
|
return
|
|
}
|
|
print(*item)
|
|
}
|
|
|
|
func DebugPrintJSON(item any) {
|
|
data, error := json.Marshal(item)
|
|
if error == nil {
|
|
print(string(data))
|
|
} else {
|
|
print(item)
|
|
}
|
|
}
|
|
|
|
func DebugPrint(items ...any) {
|
|
|
|
var stack DebugStackLineModel = GetTraceStack(1)[0]
|
|
|
|
print(">[DEV]>[", stack.Line, "]", stack.File, "(", stack.Method, "): ")
|
|
for _, item := range items {
|
|
if item == nil {
|
|
print("nil")
|
|
continue
|
|
}
|
|
|
|
switch value := item.(type) {
|
|
case string:
|
|
print(value)
|
|
case *string:
|
|
DebugPrintPointer(value)
|
|
case int:
|
|
print(value)
|
|
case int8:
|
|
print(value)
|
|
case int16:
|
|
print(value)
|
|
case int32:
|
|
print(value)
|
|
case int64:
|
|
print(value)
|
|
case *int:
|
|
DebugPrintPointer(value)
|
|
case *int8:
|
|
DebugPrintPointer(value)
|
|
case *int16:
|
|
DebugPrintPointer(value)
|
|
case *int32:
|
|
DebugPrintPointer(value)
|
|
case *int64:
|
|
DebugPrintPointer(value)
|
|
case uint:
|
|
print(value)
|
|
case uint8:
|
|
print(value)
|
|
case uint16:
|
|
print(value)
|
|
case uint32:
|
|
print(value)
|
|
case uint64:
|
|
print(value)
|
|
case *uint:
|
|
DebugPrintPointer(value)
|
|
case *uint8:
|
|
DebugPrintPointer(value)
|
|
case *uint16:
|
|
DebugPrintPointer(value)
|
|
case *uint32:
|
|
DebugPrintPointer(value)
|
|
case *uint64:
|
|
DebugPrintPointer(value)
|
|
case float32, float64:
|
|
print(value)
|
|
case *float32:
|
|
DebugPrintPointer(value)
|
|
case *float64:
|
|
DebugPrintPointer(value)
|
|
case bool:
|
|
print(value)
|
|
default:
|
|
DebugPrintJSON(value)
|
|
}
|
|
|
|
}
|
|
print("\n")
|
|
|
|
}
|