Add "Dump" to dump objects using valast

This commit is contained in:
2025-07-08 16:41:45 +02:00
parent f8a72d3579
commit 1712042f64
3 changed files with 107 additions and 0 deletions

71
main.go
View File

@@ -4,6 +4,7 @@ import (
"bytes"
"flag"
"fmt"
"github.com/hexops/valast"
"io"
"log"
"os"
@@ -33,6 +34,8 @@ const (
LevelDebug
// LevelTrace is for very detailed tracing information
LevelTrace
// LevelDump is for dumping objects to console for regressive tests
LevelDump
// LevelLua is specifically for output from Lua scripts
LevelLua
LevelPrefix
@@ -75,6 +78,12 @@ var levelStyles = map[LogLevel]LevelStyle{
Tag: "TRACE",
TagColor: BPurple, // Bold Purple
},
LevelDump: {
Tag: "DUMP",
TagColor: BIMagenta, // Bold Intense Magenta
TagBackgroundColor: On_White, // White background
MessageColor: IMagenta, // White text
},
LevelLua: {
Tag: "LUA",
TagColor: BBlue, // Bold Blue
@@ -121,6 +130,8 @@ func ParseLevel(levelStr string) LogLevel {
return LevelDebug
case "TRACE":
return LevelTrace
case "DUMP":
return LevelDump
case "LUA":
return LevelLua
default:
@@ -505,6 +516,27 @@ func (l *Logger) Trace(format string, args ...interface{}) {
l.log(LevelTrace, format, args...)
}
// Dump logs objects using valast for regressive tests
func (l *Logger) Dump(message string, objects ...interface{}) {
if len(objects) == 0 {
l.log(LevelDump, message)
return
}
var dumpContent strings.Builder
dumpContent.WriteString(message)
dumpContent.WriteString(":\n")
for i, obj := range objects {
if i > 0 {
dumpContent.WriteString("\n")
}
dumpContent.WriteString(valast.String(obj))
}
l.log(LevelDump, dumpContent.String())
}
// Lua logs a Lua message
func (l *Logger) Lua(format string, args ...interface{}) {
l.log(LevelLua, format, args...)
@@ -552,6 +584,14 @@ func Trace(format string, args ...interface{}) {
Default.Trace(format, args...)
}
// Dump logs objects using valast for regressive tests using the default logger
func Dump(message string, objects ...interface{}) {
if Default == nil {
Init(defaultLogLevel)
}
Default.Dump(message, objects...)
}
// Lua logs a Lua message using the default logger
func Lua(format string, args ...interface{}) {
if Default == nil {
@@ -657,4 +697,35 @@ func main() {
// Test with custom prefix
WithField("prefix", "custom").Info("Message with custom prefix")
Lua("This is a Lua message")
// Test Dump functionality
SetLevel(LevelDump) // Set level to show dump messages
type ProjectData struct {
Title string
Name string
Data string
Commits string
}
type Project struct {
Id int64
Data *ProjectData
}
p := Project{
Id: 1,
Data: &ProjectData{
Title: "Test",
Name: "Mihai",
Data: "Some data",
Commits: "Test Message",
},
}
// Test single object dump
Dump("FooBar called", p)
// Test multiple object dump
Dump("Multiple objects", p, fields, "string value", 42)
}