add more logging

main
Nick Dumas 2 weeks ago
parent 20e97aff70
commit c395c22bc2

@ -15,13 +15,22 @@ import (
func main() { func main() {
var ( 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() 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{ rs := &systems.RoomSystem{
Rooms: make(map[uint64]*systems.Room, 0), Rooms: make(map[uint64]*systems.Room, 0),

@ -3,6 +3,7 @@ package core
import ( import (
"bufio" "bufio"
"context" "context"
"io"
"log/slog" "log/slog"
"os" "os"
"time" "time"
@ -15,7 +16,7 @@ type App struct {
L *slog.Logger L *slog.Logger
} }
func New(level string) *App { func New(level string, logFile io.Writer) *App {
var logLevel slog.Level var logLevel slog.Level
switch level { switch level {
case "INFO", "info": case "INFO", "info":
@ -27,7 +28,7 @@ func New(level string) *App {
} }
l := slog.New(slog.NewTextHandler( l := slog.New(slog.NewTextHandler(
os.Stderr, &slog.HandlerOptions{ logFile, &slog.HandlerOptions{
Level: logLevel, Level: logLevel,
}, },
), ),

@ -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) {}

@ -10,12 +10,6 @@ import (
"code.ndumas.com/ndumas/muddy/components" "code.ndumas.com/ndumas/muddy/components"
) )
type Exit struct {
*components.ID
*components.Observable
To, From uint64
}
type Room struct { type Room struct {
*components.ID *components.ID
*components.Location *components.Location
@ -61,7 +55,6 @@ func (rs *RoomSystem) New(w *ecs.World) {
} }
func (rs *RoomSystem) Update(dt float32) { func (rs *RoomSystem) Update(dt float32) {
rs.L.Debug("tick")
weather := []string{ weather := []string{
"The sky is clear and birds sing.", "The sky is clear and birds sing.",
"It's dark and rainy.", "It's dark and rainy.",
@ -69,7 +62,9 @@ func (rs *RoomSystem) Update(dt float32) {
} }
if rand.Intn(2000) > 1990 { 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 // flush changes to persistent storage

Loading…
Cancel
Save