diff --git a/go.mod b/go.mod index 65436b5..20bb2c8 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,13 @@ module git.site.quack-lab.dev/dave/cylogger go 1.23 + +require github.com/hexops/valast v1.5.0 + +require ( + github.com/google/go-cmp v0.5.9 // indirect + golang.org/x/mod v0.7.0 // indirect + golang.org/x/sys v0.3.0 // indirect + golang.org/x/tools v0.4.0 // indirect + mvdan.cc/gofumpt v0.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..db3f9ae --- /dev/null +++ b/go.sum @@ -0,0 +1,26 @@ +github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= +github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/hexops/autogold v0.8.1 h1:wvyd/bAJ+Dy+DcE09BoLk6r4Fa5R5W+O+GUzmR985WM= +github.com/hexops/autogold v0.8.1/go.mod h1:97HLDXyG23akzAoRYJh/2OBs3kd80eHyKPvZw0S5ZBY= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= +github.com/hexops/valast v1.5.0 h1:FBTuvVi0wjTngtXJRZXMbkN/Dn6DgsUsBwch2DUJU8Y= +github.com/hexops/valast v1.5.0/go.mod h1:Jcy1pNH7LNraVaAZDLyv21hHg2WBv9Nf9FL6fGxU7o4= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4= +golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= +mvdan.cc/gofumpt v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM= +mvdan.cc/gofumpt v0.4.0/go.mod h1:PljLOHDeZqgS8opHRKLzp2It2VBuSdteAgqUfzMTxlQ= diff --git a/main.go b/main.go index 3ed6c74..7a9cfc3 100644 --- a/main.go +++ b/main.go @@ -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) }