From c395c22bc21d2e3e7874d7a336def63abdad5292 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Wed, 21 Jan 2026 10:02:52 -0500 Subject: [PATCH] add more logging --- cmd/cli/guest/main.go | 15 ++++++++++++--- core/core.go | 5 +++-- systems/exit.go | 27 +++++++++++++++++++++++++++ systems/room.go | 11 +++-------- 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 systems/exit.go diff --git a/cmd/cli/guest/main.go b/cmd/cli/guest/main.go index c489c92..c6c143c 100644 --- a/cmd/cli/guest/main.go +++ b/cmd/cli/guest/main.go @@ -14,14 +14,23 @@ import ( func main() { var ( - level string + level string + logFile string ) - flag.StringVar(&level, "level", "INFO", "Log level: INFO/info, DEBUG/debug, or ERROR/error") + flag.StringVar(&level, "level", "INFO", "Log level: INFO/info, DEBUG/debug, or ERROR/error.") + flag.StringVar(&logFile, "log", "out.log", "Path to log file.") flag.Parse() - app := core.New(level) + f, err := os.Create(logFile) + if err != nil { + slog.With( + slog.Any("error", err), + ).Error("could not open log file") + os.Exit(1) + } + app := core.New(level, f) rs := &systems.RoomSystem{ Rooms: make(map[uint64]*systems.Room, 0), diff --git a/core/core.go b/core/core.go index f27528b..b7edec6 100644 --- a/core/core.go +++ b/core/core.go @@ -3,6 +3,7 @@ package core import ( "bufio" "context" + "io" "log/slog" "os" "time" @@ -15,7 +16,7 @@ type App struct { L *slog.Logger } -func New(level string) *App { +func New(level string, logFile io.Writer) *App { var logLevel slog.Level switch level { case "INFO", "info": @@ -27,7 +28,7 @@ func New(level string) *App { } l := slog.New(slog.NewTextHandler( - os.Stderr, &slog.HandlerOptions{ + logFile, &slog.HandlerOptions{ Level: logLevel, }, ), diff --git a/systems/exit.go b/systems/exit.go new file mode 100644 index 0000000..16f99a4 --- /dev/null +++ b/systems/exit.go @@ -0,0 +1,27 @@ +package systems + +import ( + "log/slog" + + "github.com/EngoEngine/ecs" + + "code.ndumas.com/ndumas/muddy/components" +) + +type Exit struct { + *components.ID + *components.Observable + To, From uint64 +} + +type ExitSystem struct { + L *slog.Logger + exits map[uint64]*Exit +} + +func (es *ExitSystem) New(w *ecs.World) {} + +func (es *ExitSystem) Update(dt float32) { +} + +func (es *ExitSystem) Remove(e ecs.BasicEntity) {} diff --git a/systems/room.go b/systems/room.go index dc135dc..68777b2 100644 --- a/systems/room.go +++ b/systems/room.go @@ -10,12 +10,6 @@ import ( "code.ndumas.com/ndumas/muddy/components" ) -type Exit struct { - *components.ID - *components.Observable - To, From uint64 -} - type Room struct { *components.ID *components.Location @@ -61,7 +55,6 @@ func (rs *RoomSystem) New(w *ecs.World) { } func (rs *RoomSystem) Update(dt float32) { - rs.L.Debug("tick") weather := []string{ "The sky is clear and birds sing.", "It's dark and rainy.", @@ -69,7 +62,9 @@ func (rs *RoomSystem) Update(dt float32) { } if rand.Intn(2000) > 1990 { - fmt.Println(weather[rand.Intn(len(weather))]) + idx := rand.Intn(len(weather)) + rs.L.With(slog.Int("idx", idx)).Debug("sending weather message") + fmt.Println(weather[idx]) } // flush changes to persistent storage