Skip to content

Commit

Permalink
Add log wrapper so logging can be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Nov 4, 2024
1 parent 878248f commit 42b895e
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 29 deletions.
4 changes: 2 additions & 2 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
10 changes: 4 additions & 6 deletions common/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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:
}
Expand All @@ -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:
}
Expand All @@ -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)
}

Expand Down
63 changes: 63 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -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...)
}
}
13 changes: 6 additions & 7 deletions screen/multiplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions screen/multiplayer_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
})

Expand Down Expand Up @@ -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")
Expand All @@ -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() })

Expand Down Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions screen/multiplayer_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
}
})
Expand Down Expand Up @@ -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(
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
})

Expand Down

0 comments on commit 42b895e

Please sign in to comment.