diff --git a/backend/backend.go b/backend/backend.go index 82f78d0..cfdd0d3 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -2,10 +2,10 @@ package backend import ( "encoding/json" - "fmt" "github.com/z-riley/go-2048-battle/backend/grid" "github.com/z-riley/go-2048-battle/backend/store" + "github.com/z-riley/go-2048-battle/log" ) // Game contains all reuquired data for a 2048 game. @@ -41,7 +41,7 @@ func NewGame(opts *Opts) *Game { if g.opts.SaveToDisk { err := g.Load() if err != nil { - fmt.Println("No save file found. Creating new one") + log.Println("No save file found. Creating new one") if err := g.Save(); err != nil { panic(err) } diff --git a/cmd/main.go b/cmd/main.go index 10e54c5..75ff3fe 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,11 +2,11 @@ package main import ( "flag" - "log" "os" "github.com/z-riley/go-2048-battle/config" "github.com/z-riley/go-2048-battle/debug" + "github.com/z-riley/go-2048-battle/log" "github.com/z-riley/go-2048-battle/screen" "github.com/z-riley/turdgl" ) diff --git a/common/arena.go b/common/arena.go index 138dad0..547db08 100644 --- a/common/arena.go +++ b/common/arena.go @@ -9,7 +9,7 @@ import ( "github.com/google/uuid" "github.com/z-riley/go-2048-battle/backend" "github.com/z-riley/go-2048-battle/backend/grid" - "github.com/z-riley/go-2048-battle/config" + "github.com/z-riley/go-2048-battle/log" "github.com/z-riley/turdgl" "golang.org/x/exp/constraints" ) @@ -235,7 +235,7 @@ func (a *Arena) handleAnimations() { // Handle errors from stage 1 select { case err := <-errCh: - fmt.Printf("Error \"%v\". Resetting to latest game state\n", err) + log.Printf("Error \"%v\". Resetting to latest game state\n", err) a.Load(animationState.gameState) default: } @@ -259,7 +259,7 @@ func (a *Arena) handleAnimations() { // Handle errors from stage 2 select { case err := <-errCh: - fmt.Printf("Error \"%v\". Resetting to latest game state\n", err) + log.Printf("Error \"%v\". Resetting to latest game state\n", err) a.Load(animationState.gameState) default: } @@ -269,9 +269,7 @@ func (a *Arena) handleAnimations() { uiTiles := len(a.tiles) backendTiles := animationState.gameState.Grid.NumTiles() if uiTiles != backendTiles { - if config.Debug { - fmt.Println("Found tile count mismatch. Reloading grid") - } + log.Println("Found tile count mismatch. Reloading grid") a.Load(animationState.gameState) } diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..9e70b06 --- /dev/null +++ b/log/log.go @@ -0,0 +1,63 @@ +// Package log wraps the standard library log package. This allows the logging +// functionality to be easily disabled. +package log + +import ( + "log" + + "github.com/z-riley/go-2048-battle/config" +) + +func Print(v ...any) { + if config.Debug { + log.Print(v...) + } +} + +func Printf(format string, v ...any) { + if config.Debug { + log.Printf(format, v...) + } +} + +func Println(v ...any) { + if config.Debug { + log.Println(v...) + } +} + +func Fatal(v ...any) { + if config.Debug { + log.Fatal(v...) + } +} + +func Fatalf(format string, v ...any) { + if config.Debug { + log.Fatalf(format, v...) + } +} + +func Fatalln(v ...any) { + if config.Debug { + log.Fatalln(v...) + } +} + +func Panic(v ...any) { + if config.Debug { + log.Panic(v...) + } +} + +func Panicf(format string, v ...any) { + if config.Debug { + log.Panicf(format, v...) + } +} + +func Panicln(v ...any) { + if config.Debug { + log.Panicln(v...) + } +} diff --git a/screen/multiplayer.go b/screen/multiplayer.go index f63cb68..7e90566 100644 --- a/screen/multiplayer.go +++ b/screen/multiplayer.go @@ -10,6 +10,7 @@ import ( "github.com/z-riley/go-2048-battle/common" "github.com/z-riley/go-2048-battle/comms" "github.com/z-riley/go-2048-battle/config" + "github.com/z-riley/go-2048-battle/log" "github.com/z-riley/turdgl" "github.com/z-riley/turdserve" ) @@ -164,17 +165,17 @@ func (s *MultiplayerScreen) Enter(initData InitData) { s.server = server.(*turdserve.Server) s.server.SetCallback(func(id int, b []byte) { if err := s.handleOpponentData(b); err != nil { - fmt.Println("Failed to handle opponent data as server", err) + log.Println("Failed to handle opponent data as server", err) } }).SetDisconnectCallback(func(id int) { - fmt.Println("Opponent has left the game") + log.Println("Opponent has left the game") }) } else if client, ok := initData[clientKey]; ok { // Guest mode - initialise client s.client = client.(*turdserve.Client) s.client.SetCallback(func(b []byte) { if err := s.handleOpponentData(b); err != nil { - fmt.Println("Failed to handle opponent data as client", err) + log.Println("Failed to handle opponent data as client", err) } }) } else { @@ -183,9 +184,7 @@ func (s *MultiplayerScreen) Enter(initData InitData) { // Tell the opponent that the local server/client is ready to receive data if err := s.sendScreenLoadedEvent(); err != nil { - if config.Debug { - fmt.Println("Failed to send game update", err) - } + log.Println("Failed to send game update", err) } } @@ -268,7 +267,7 @@ func (s *MultiplayerScreen) Update() { case inputFunc := <-s.arenaInputCh: inputFunc() if err := s.sendGameData(); err != nil { - fmt.Println("Failed to send game update:", err) + log.Println("Failed to send game update:", err) } default: diff --git a/screen/multiplayer_host.go b/screen/multiplayer_host.go index 71ea777..4b01f0b 100644 --- a/screen/multiplayer_host.go +++ b/screen/multiplayer_host.go @@ -9,6 +9,7 @@ import ( "github.com/z-riley/go-2048-battle/common" "github.com/z-riley/go-2048-battle/comms" "github.com/z-riley/go-2048-battle/config" + "github.com/z-riley/go-2048-battle/log" "github.com/z-riley/turdgl" "github.com/z-riley/turdserve" ) @@ -62,7 +63,7 @@ func (s *MultiplayerHostScreen) Enter(_ InitData) { SetModifiedCB(func() { // Update guest with new username if err := s.sendPlayerData(); err != nil { - fmt.Println("Failed to send username update to guests:", err) + log.Println("Failed to send username update to guests:", err) } }) @@ -113,7 +114,7 @@ func (s *MultiplayerHostScreen) Enter(_ InitData) { } if err := s.startGame(); err != nil { - fmt.Println("Failed to start game:", err) + log.Println("Failed to start game:", err) } }, ).SetLabelText("Start") @@ -140,7 +141,7 @@ func (s *MultiplayerHostScreen) Enter(_ InitData) { s.server = turdserve.NewServer(maxClients). SetCallback(func(_ int, b []byte) { if err := s.handleClientData(b); err != nil { - fmt.Println("Host screen failed to handle data from client:", err) + log.Println("Host screen failed to handle data from client:", err) } }).SetDisconnectCallback(func(_ int) { s.handleOpponentDisconnect() }) @@ -204,6 +205,7 @@ func (s *MultiplayerHostScreen) handleClientData(data []byte) error { return fmt.Errorf("failed to parse player data: %w", err) } return s.handlePlayerData(data) + default: // Ignore other message type - don't return error return nil diff --git a/screen/multiplayer_join.go b/screen/multiplayer_join.go index 9d7c407..f40e275 100644 --- a/screen/multiplayer_join.go +++ b/screen/multiplayer_join.go @@ -9,6 +9,7 @@ import ( "github.com/z-riley/go-2048-battle/common" "github.com/z-riley/go-2048-battle/comms" "github.com/z-riley/go-2048-battle/config" + "github.com/z-riley/go-2048-battle/log" "github.com/z-riley/turdgl" "github.com/z-riley/turdserve" ) @@ -61,9 +62,7 @@ func (s *MultiplayerJoinScreen) Enter(_ InitData) { // Update host with new username if s.client != nil { if err := s.sendPlayerData(); err != nil { - if config.Debug { - fmt.Println("Failed to send username update to host:", err) - } + log.Println("Failed to send username update to host:", err) } } }) @@ -180,9 +179,7 @@ func (s *MultiplayerJoinScreen) joinButtonHandler() { go func() { for err := range errCh { if err != nil { - if config.Debug { - fmt.Println(fmt.Errorf("client error: %w", err).Error()) - } + log.Println("Client error: %w", err) // Re-enable button s.join.SetCallback( @@ -209,9 +206,7 @@ func (s *MultiplayerJoinScreen) joinButtonHandler() { time.Sleep(time.Second) s.opponentStatus.SetText("") }() - if config.Debug { - fmt.Println("Failed to join game:", err) - } + log.Println("Failed to join game:", err) return } @@ -243,7 +238,7 @@ func (s *MultiplayerJoinScreen) joinGame(errCh chan error) error { s.client.SetCallback(func(b []byte) { if err := s.handleServerData(b); err != nil { - fmt.Println("Join screen failed to handle data from server:", err) + log.Println("Join screen failed to handle data from server:", err) } })