diff --git a/server/Makefile b/server/Makefile index 9226084..e1cab5e 100644 --- a/server/Makefile +++ b/server/Makefile @@ -12,7 +12,7 @@ build: run: $(MAKE) build - ./knowyt + ./knowyt -v run-loop: pexec -R -c -e TARGET \ diff --git a/server/src/application/collectQuotes.go b/server/src/application/collectQuotes.go index 73ce38d..8ff670c 100644 --- a/server/src/application/collectQuotes.go +++ b/server/src/application/collectQuotes.go @@ -3,6 +3,8 @@ package application import ( "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -23,7 +25,7 @@ func (app *Application) CollectQuotes(usr *user.User, w http.ResponseWriter, r * gm.CollectQuotes() if err := app.saveGameState(gm); err != nil { - fmt.Println(err) + log.ErrorLog(err) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "couldn't save game state") return diff --git a/server/src/application/continueGame.go b/server/src/application/continueGame.go index 41672e1..95b4cf8 100644 --- a/server/src/application/continueGame.go +++ b/server/src/application/continueGame.go @@ -3,6 +3,8 @@ package application import ( "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -24,6 +26,6 @@ func (app *Application) ContinueGame(usr *user.User, w http.ResponseWriter, r *h gm.ContinueGame() if err := app.saveGameState(gm); err != nil { - fmt.Println(err) + log.ErrorLog(err) } } diff --git a/server/src/application/createGame.go b/server/src/application/createGame.go index 14a9099..6d40e69 100644 --- a/server/src/application/createGame.go +++ b/server/src/application/createGame.go @@ -3,15 +3,17 @@ package application import ( "encoding/json" "fmt" - "github.com/google/uuid" "math/rand" "net/http" "os" "path" - "sirlab.de/go/knowyt/game" - "sirlab.de/go/knowyt/user" "strconv" "time" + + "github.com/google/uuid" + "sirlab.de/go/knowyt/game" + "sirlab.de/go/knowyt/log" + "sirlab.de/go/knowyt/user" ) type PlayerInfo struct { @@ -36,7 +38,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) { gm, err := app.createGame(teamname, lang) if err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Printf("%v\n", err) + log.ErrorLog(err) fmt.Fprintf(w, "server error") return } @@ -44,7 +46,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) { _, err = app.createUser(gm, name, user.ROLE_GAMEMASTER, authcode) if err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Printf("%v\n", err) + log.ErrorLog(err) fmt.Fprintf(w, "server error") return } @@ -53,11 +55,11 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) { Status: "ok", Authcode: authcode, } - fmt.Printf("Name %s, Teamname %s, Authcode %s\n", name, teamname, authcode) + log.Debug("Name %s, Teamname %s, Authcode %s\n", name, teamname, authcode) w.Header().Add("Content-Type", "application/json") jsonString, _ := json.Marshal(playerInfo) - fmt.Fprintf(w, string(jsonString)) + fmt.Fprintf(w, "%s", string(jsonString)) } func (app *Application) createGame(gamename, lang string) (*game.Game, error) { @@ -72,11 +74,12 @@ func (app *Application) createGame(gamename, lang string) (*game.Game, error) { if err := os.Mkdir(quotesDirName, 0777); err != nil { return nil, err } - fmt.Printf("createGame(\"%s\": \"%s\" [%s])\n", gameId, gamename, lang) + log.Info("createGame(\"%s\": \"%s\" [%s])\n", gameId, gamename, lang) gm, err := game.NewGame(gameId, gameFileName, gamename, lang) if err != nil { return nil, err } + if err := gm.SaveGame(); err != nil { return nil, err } diff --git a/server/src/application/deletePlayer.go b/server/src/application/deletePlayer.go index 479580a..f42abb1 100644 --- a/server/src/application/deletePlayer.go +++ b/server/src/application/deletePlayer.go @@ -3,6 +3,8 @@ package application import ( "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -25,20 +27,20 @@ func (app *Application) DeletePlayer(usr *user.User, w http.ResponseWriter, r *h deleteUser := app.users[id] if deleteUser.GetId() != id || deleteUser.GetGameId() != gm.GetId() { w.WriteHeader(http.StatusForbidden) - fmt.Printf("couldn't find player %s in game %s\n", id, gm.GetId()) + log.Warn("couldn't find player %s in game %s\n", id, gm.GetId()) fmt.Fprintf(w, "forbidden") return } if deleteUser.GetId() == usr.GetId() { w.WriteHeader(http.StatusForbidden) - fmt.Printf("can't delete yourself\n") + log.Warn("can't delete yourself\n") fmt.Fprintf(w, "forbidden") return } if err := deleteUser.DeleteUser(); err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Printf("%v\n", err) + log.ErrorLog(err) fmt.Fprintf(w, "internal server error") return } diff --git a/server/src/application/finishGame.go b/server/src/application/finishGame.go index 69a8081..c92fd60 100644 --- a/server/src/application/finishGame.go +++ b/server/src/application/finishGame.go @@ -3,6 +3,8 @@ package application import ( "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -22,7 +24,7 @@ func (app *Application) FinishGame(usr *user.User, w http.ResponseWriter, r *htt } if err := app.saveGameState(gm); err != nil { - fmt.Println(err) + log.ErrorLog(err) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "couldn't save game state") return @@ -31,7 +33,7 @@ func (app *Application) FinishGame(usr *user.User, w http.ResponseWriter, r *htt gm.FinishGame() if err := app.saveGameState(gm); err != nil { - fmt.Println(err) + log.ErrorLog(err) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "couldn't save game state") return diff --git a/server/src/application/getGameInfo.go b/server/src/application/getGameInfo.go index 1dfee15..7e00a8d 100644 --- a/server/src/application/getGameInfo.go +++ b/server/src/application/getGameInfo.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -25,7 +27,7 @@ func (app *Application) GetGameInfo(usr *user.User, w http.ResponseWriter, r *ht gameInfo := gm.GetGameInfo() for i, _ := range gameInfo.Players { if playerUser, err := app.GetUserById(gameInfo.Players[i].Id); err != nil { - fmt.Printf("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id) + log.Warn("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "internal server error") return diff --git a/server/src/application/getGames.go b/server/src/application/getGames.go index 0626bed..c2e4809 100644 --- a/server/src/application/getGames.go +++ b/server/src/application/getGames.go @@ -4,7 +4,9 @@ import ( "encoding/json" "fmt" "net/http" + "sirlab.de/go/knowyt/game" + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -31,7 +33,7 @@ func (app *Application) GetGames(usr *user.User, w http.ResponseWriter, r *http. for k, gameInfo := range games.Games { for i, _ := range gameInfo.Players { if playerUser, err := app.GetUserById(gameInfo.Players[i].Id); err != nil { - fmt.Printf("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id) + log.Warn("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "internal server error") return diff --git a/server/src/application/getQuotes.go b/server/src/application/getQuotes.go index aaa6267..fe2cc66 100644 --- a/server/src/application/getQuotes.go +++ b/server/src/application/getQuotes.go @@ -4,6 +4,8 @@ import ( "encoding/json" "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -11,15 +13,15 @@ func (app *Application) GetQuotes(usr *user.User, w http.ResponseWriter, r *http gameRef := r.URL.Query().Get("g") gm, err := app.GetGameById(gameRef) if err != nil { - fmt.Printf("attempt to get quotes for invalid game id %s\n", gameRef) + log.Warn("attempt to get quotes for invalid game id %s\n", gameRef) w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "game not found") return } if usr.GetGameId() != gameRef && !usr.IsGamemaster() { - fmt.Printf("user's game id is %s\n", usr.GetGameId()) - fmt.Printf("user not allowed to access game id %s\n", gameRef) + log.Warn("user's game id is %s\n", usr.GetGameId()) + log.Warn("user not allowed to access game id %s\n", gameRef) w.WriteHeader(http.StatusForbidden) fmt.Fprintf(w, "forbidden") return diff --git a/server/src/application/removeQuote.go b/server/src/application/removeQuote.go index 38bf326..ea9e395 100644 --- a/server/src/application/removeQuote.go +++ b/server/src/application/removeQuote.go @@ -4,6 +4,8 @@ import ( "fmt" "net/http" "path" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -28,7 +30,7 @@ func (app *Application) RemoveQuote(usr *user.User, w http.ResponseWriter, r *ht quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort) if err := gm.RemoveQuote(quoteFileName, usr.GetId(), quoteId); err != nil { - fmt.Printf("%s\n", err) + log.ErrorLog(err) w.WriteHeader(http.StatusForbidden) fmt.Fprintf(w, "forbidden") return diff --git a/server/src/application/resetGame.go b/server/src/application/resetGame.go index a230a6a..017d83c 100644 --- a/server/src/application/resetGame.go +++ b/server/src/application/resetGame.go @@ -3,6 +3,8 @@ package application import ( "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -24,7 +26,7 @@ func (app *Application) ResetGame(usr *user.User, w http.ResponseWriter, r *http gm.ResetGame() if err := app.saveGameState(gm); err != nil { - fmt.Println(err) + log.ErrorLog(err) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "couldn't save game state") return diff --git a/server/src/application/savePlayer.go b/server/src/application/savePlayer.go index 44ae679..1f4352b 100644 --- a/server/src/application/savePlayer.go +++ b/server/src/application/savePlayer.go @@ -3,9 +3,11 @@ package application import ( "fmt" "net/http" - "sirlab.de/go/knowyt/game" - "sirlab.de/go/knowyt/user" "strconv" + + "sirlab.de/go/knowyt/game" + "sirlab.de/go/knowyt/log" + "sirlab.de/go/knowyt/user" ) func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *http.Request) { @@ -29,14 +31,14 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt authcode := r.URL.Query().Get("authcode") if name == "" { w.WriteHeader(http.StatusBadRequest) - fmt.Printf("player name must not be empty\n") + log.Warn("player name must not be empty\n") fmt.Fprintf(w, "server error") return } if id != "" { if err := app.modifyUser(id, gm, name, authcode); err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Printf("%v\n", err) + log.ErrorLog(err) fmt.Fprintf(w, "server error") return } @@ -44,7 +46,7 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt newPlayerId, err := app.createUser(gm, name, user.ROLE_PLAYER, authcode) if err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Printf("%v\n", err) + log.ErrorLog(err) fmt.Fprintf(w, "server error") return } else { @@ -54,7 +56,7 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt if err := app.updateScore(gm, id, score); err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Printf("%v\n", err) + log.ErrorLog(err) fmt.Fprintf(w, "server error") return } diff --git a/server/src/application/saveQuote.go b/server/src/application/saveQuote.go index ab3f49c..6ffba4f 100644 --- a/server/src/application/saveQuote.go +++ b/server/src/application/saveQuote.go @@ -7,6 +7,7 @@ import ( "github.com/google/uuid" "sirlab.de/go/knowyt/game" + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -47,7 +48,7 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http } if err != nil { - fmt.Printf("%s", err) + log.ErrorLog(err) w.WriteHeader(http.StatusForbidden) fmt.Fprintf(w, "forbidden") return diff --git a/server/src/application/setGameLang.go b/server/src/application/setGameLang.go index 43d6e6d..e18b9b8 100644 --- a/server/src/application/setGameLang.go +++ b/server/src/application/setGameLang.go @@ -3,6 +3,8 @@ package application import ( "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -24,7 +26,7 @@ func (app *Application) SetGameLang(usr *user.User, w http.ResponseWriter, r *ht lang := r.URL.Query().Get("lang") if err := gm.SetGameLang(lang); err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Printf("%v", err) + log.ErrorLog(err) fmt.Fprintf(w, "server error") return } diff --git a/server/src/application/setGameName.go b/server/src/application/setGameName.go index 281754a..d591b54 100644 --- a/server/src/application/setGameName.go +++ b/server/src/application/setGameName.go @@ -3,6 +3,8 @@ package application import ( "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -24,7 +26,7 @@ func (app *Application) SetGameName(usr *user.User, w http.ResponseWriter, r *ht name := r.URL.Query().Get("name") if err := gm.SetGameName(name); err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Printf("%v", err) + log.ErrorLog(err) fmt.Fprintf(w, "server error") return } diff --git a/server/src/application/startGame.go b/server/src/application/startGame.go index 67180d6..f96e931 100644 --- a/server/src/application/startGame.go +++ b/server/src/application/startGame.go @@ -3,6 +3,8 @@ package application import ( "fmt" "net/http" + + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -24,7 +26,7 @@ func (app *Application) StartGame(usr *user.User, w http.ResponseWriter, r *http gm.StartGame() if err := app.saveGameState(gm); err != nil { - fmt.Println(err) + log.ErrorLog(err) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "couldn't save game state") return diff --git a/server/src/applicationConfig/appconfig.go b/server/src/applicationConfig/appconfig.go index 3de92b5..25e7d3c 100644 --- a/server/src/applicationConfig/appconfig.go +++ b/server/src/applicationConfig/appconfig.go @@ -1,10 +1,30 @@ package applicationConfig +import ( + "flag" + + "sirlab.de/go/knowyt/log" +) + type ApplicationConfig struct { DataDir string } func NewApplicationConfig() ApplicationConfig { + + flagVerbose := flag.Bool("v", false, "log debug messages, too") + flagQuiet := flag.Bool("q", false, "be quiet; warning and error messages only") + flag.Parse() + + log.SetLoglevel(log.LOG_INFO) + + if *flagVerbose { + log.SetLoglevel(log.LOG_DEBUG) + } + if *flagQuiet { + log.SetLoglevel(log.LOG_WARN) + } + return ApplicationConfig{ DataDir: "data/", } diff --git a/server/src/engine/publish.go b/server/src/engine/publish.go index 6d14179..0510e60 100644 --- a/server/src/engine/publish.go +++ b/server/src/engine/publish.go @@ -1,7 +1,7 @@ package engine import ( - "fmt" + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/syncdata" ) @@ -18,6 +18,6 @@ func (eng *Engine) publish(populateSyncDataCb PopulateSyncDataCb) { populateSyncDataCb(&data) } - fmt.Printf("%s engine versionRef %d\n", data.GameInfo.GameId, eng.versionRef) + log.Debug("%s engine versionRef %d\n", data.GameInfo.GameId, eng.versionRef) eng.obs.Update(data) } diff --git a/server/src/engine/sync.go b/server/src/engine/sync.go index 84bc12c..065c607 100644 --- a/server/src/engine/sync.go +++ b/server/src/engine/sync.go @@ -4,8 +4,9 @@ import ( "encoding/json" "fmt" "net/http" - "sirlab.de/go/knowyt/syncdata" "strconv" + + "sirlab.de/go/knowyt/syncdata" ) func (eng *Engine) SyncHandler(w http.ResponseWriter, r *http.Request) { @@ -25,13 +26,11 @@ func (eng *Engine) SyncHandler(w http.ResponseWriter, r *http.Request) { break } - select { - case <-stream.Changes(): - stream.Next() - } + <-stream.Changes() + stream.Next() } jsonString, _ := json.MarshalIndent(value, "", " ") w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, string(jsonString)) + fmt.Fprintf(w, "%s", string(jsonString)) } diff --git a/server/src/game/collectQuotes.go b/server/src/game/collectQuotes.go index 3ea2ca9..9a6fdf8 100644 --- a/server/src/game/collectQuotes.go +++ b/server/src/game/collectQuotes.go @@ -1,13 +1,13 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" ) func (gm *Game) CollectQuotes() { err := gm.changeGameState(STATE_IDLE, STATE_COLLECT, PHASE_NONE) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } diff --git a/server/src/game/continueGame.go b/server/src/game/continueGame.go index 860533d..c05677e 100644 --- a/server/src/game/continueGame.go +++ b/server/src/game/continueGame.go @@ -1,13 +1,13 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" ) func (gm *Game) ContinueGame() { state, phase := gm.GetState() if state != STATE_PLAY { - fmt.Printf("invalid state, can't continue game in %s state\n", state) + log.Warn("invalid state, can't continue game in %s state\n", state) return } @@ -19,7 +19,7 @@ func (gm *Game) ContinueGame() { case PHASE_REVEAL_SOURCE: gm.nextRound() default: - fmt.Printf("invalid state, can't continue game in %s state, %s phase\n", state, phase) + log.Warn("invalid state, can't continue game in %s state, %s phase\n", state, phase) return } } diff --git a/server/src/game/finishGame.go b/server/src/game/finishGame.go index 49b0409..57ab23e 100644 --- a/server/src/game/finishGame.go +++ b/server/src/game/finishGame.go @@ -1,13 +1,13 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" ) func (gm *Game) FinishGame() { state, _ := gm.GetState() if state != STATE_IDLE && state != STATE_PLAY { - fmt.Printf("%s invalid state, can't finish game in %s state\n", gm.id, state) + log.Warn("%s invalid state, can't finish game in %s state\n", gm.id, state) return } diff --git a/server/src/game/nextRound.go b/server/src/game/nextRound.go index a0997c1..c213d59 100644 --- a/server/src/game/nextRound.go +++ b/server/src/game/nextRound.go @@ -1,13 +1,14 @@ package game import ( - "fmt" "time" + + "sirlab.de/go/knowyt/log" ) func (gm *Game) nextRound() { if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SOURCE, PHASE_ROUND_END); err != nil { - fmt.Println(err) + log.ErrorLog(err) return } gm.notifyClients() @@ -16,7 +17,7 @@ func (gm *Game) nextRound() { err := gm.changeGameState(STATE_PLAY, STATE_READY_SET, "Go!") if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } gm.notifyClients() @@ -25,7 +26,7 @@ func (gm *Game) nextRound() { err = gm.changeGameState(STATE_READY_SET, STATE_READY_SET, "") if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } gm.notifyClients() @@ -34,7 +35,7 @@ func (gm *Game) nextRound() { err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } gm.runRound() diff --git a/server/src/game/player.go b/server/src/game/player.go index 1330c87..7d1e1c5 100644 --- a/server/src/game/player.go +++ b/server/src/game/player.go @@ -1,7 +1,7 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -24,9 +24,9 @@ func (gm *Game) EventPlayerJoins(usr *user.User) { gm.players[usrId] = player if prevPlayer.id != "" { - fmt.Printf("%s player \"%s\" re-joined\n", gm.id, gm.players[usrId].name) + log.Debug("%s player \"%s\" re-joined\n", gm.id, gm.players[usrId].name) } else { - fmt.Printf("%s player \"%s\" joined\n", gm.id, usrName) + log.Debug("%s player \"%s\" joined\n", gm.id, usrName) } gm.eng.Update() @@ -45,7 +45,7 @@ func (gm *Game) EventPlayerLeaves(usr *user.User) { player.isIdle = true gm.players[usrId] = player - fmt.Printf("%s player \"%s\" is idle\n", gm.id, usr.GetName()) + log.Debug("%s player \"%s\" is idle\n", gm.id, usr.GetName()) gm.eng.Update() } diff --git a/server/src/game/populateGetRoundInfo.go b/server/src/game/populateGetRoundInfo.go index 78249f8..dc435f9 100644 --- a/server/src/game/populateGetRoundInfo.go +++ b/server/src/game/populateGetRoundInfo.go @@ -1,7 +1,7 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/syncdata" ) @@ -12,7 +12,7 @@ func (gm *Game) populateGetRoundInfo() *syncdata.RoundInfo { quote := gm.quotes[gm.round.quoteId] if quote == nil { - fmt.Printf("%s fatal error: quote id \"%s\" not found\n", gm.id, gm.round.quoteId) + log.Error("%s fatal error: quote id \"%s\" not found\n", gm.id, gm.round.quoteId) return nil } diff --git a/server/src/game/revealShowCount.go b/server/src/game/revealShowCount.go index 06157b4..3e26636 100644 --- a/server/src/game/revealShowCount.go +++ b/server/src/game/revealShowCount.go @@ -1,17 +1,17 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" ) func (gm *Game) revealShowCount() { if !gm.allPlayersHaveSelectedAQuote() { - fmt.Printf("%s not all players have selected a quote yet\n", gm.GetId()) + log.Warn("%s not all players have selected a quote yet\n", gm.GetId()) return } if err := gm.changeGamePhase(STATE_PLAY, PHASE_SELECT_QUOTE, PHASE_REVEAL_SHOW_COUNT); err != nil { - fmt.Println(err) + log.ErrorLog(err) return } for key, val := range gm.round.selections { diff --git a/server/src/game/revealSource.go b/server/src/game/revealSource.go index a93dd91..68d016a 100644 --- a/server/src/game/revealSource.go +++ b/server/src/game/revealSource.go @@ -1,12 +1,12 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" ) func (gm *Game) revealSource() { if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SHOW_COUNT, PHASE_REVEAL_SOURCE); err != nil { - fmt.Println(err) + log.ErrorLog(err) return } diff --git a/server/src/game/runCountdown.go b/server/src/game/runCountdown.go index 0857ca2..b85b1b2 100644 --- a/server/src/game/runCountdown.go +++ b/server/src/game/runCountdown.go @@ -1,8 +1,9 @@ package game import ( - "fmt" "time" + + "sirlab.de/go/knowyt/log" ) func (gm *Game) runCountdown() { @@ -17,7 +18,7 @@ func (gm *Game) runCountdown() { err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0]) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } gm.notifyClients() @@ -26,7 +27,7 @@ func (gm *Game) runCountdown() { for i := 1; i < len(phases); i++ { err = gm.changeGamePhase(STATE_READY_SET, phases[i-1], phases[i]) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } gm.notifyClients() @@ -35,7 +36,7 @@ func (gm *Game) runCountdown() { err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } diff --git a/server/src/game/runFinal.go b/server/src/game/runFinal.go index 33869cc..c0f285a 100644 --- a/server/src/game/runFinal.go +++ b/server/src/game/runFinal.go @@ -1,19 +1,19 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" ) func (gm *Game) runFinal() { state, _ := gm.GetState() if state != STATE_IDLE && state != STATE_PLAY { - fmt.Printf("%s expected state \"IDLE\" | \"PLAY\" != \"%s\"", gm.GetId(), state) + log.Warn("%s expected state \"IDLE\" | \"PLAY\" != \"%s\"", gm.GetId(), state) return } err := gm.changeGameState(STATE_ANY, STATE_FINAL, PHASE_NONE) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } diff --git a/server/src/game/runRound.go b/server/src/game/runRound.go index 0d37055..c5785dc 100644 --- a/server/src/game/runRound.go +++ b/server/src/game/runRound.go @@ -1,19 +1,19 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" ) func (gm *Game) runRound() { state, _ := gm.GetState() if state != STATE_IDLE && state != STATE_PLAY && state != STATE_COLLECT { - fmt.Printf("%s expected state \"IDLE\" | \"PLAY\" | \"COLLECT\" != \"%s\"", gm.GetId(), state) + log.Warn("%s expected state \"IDLE\" | \"PLAY\" | \"COLLECT\" != \"%s\"", gm.GetId(), state) return } err := gm.changeGameState(STATE_ANY, STATE_PLAY, PHASE_SELECT_QUOTE) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } diff --git a/server/src/game/saveSelection.go b/server/src/game/saveSelection.go index 5bc9f12..5264421 100644 --- a/server/src/game/saveSelection.go +++ b/server/src/game/saveSelection.go @@ -1,7 +1,7 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) @@ -26,5 +26,5 @@ func (gm *Game) updateSelection(usr *user.User, selection string) { } } - fmt.Printf("%s invalid selection id \"%s\"\n", gm.id, selection) + log.Warn("%s invalid selection id \"%s\"\n", gm.id, selection) } diff --git a/server/src/game/setupRound.go b/server/src/game/setupRound.go index 0304c34..f1bdee3 100644 --- a/server/src/game/setupRound.go +++ b/server/src/game/setupRound.go @@ -3,8 +3,10 @@ package game import ( "fmt" "math/rand" - "sirlab.de/go/knowyt/quote" "time" + + "sirlab.de/go/knowyt/log" + "sirlab.de/go/knowyt/quote" ) func (gm *Game) setupRound() error { @@ -66,7 +68,7 @@ func (gm *Game) selectQuote() error { func (gm *Game) getSourceById(id string) Source { plInfo, err := gm.getPlayerById(id) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return Source{} } diff --git a/server/src/game/startGame.go b/server/src/game/startGame.go index 18f0d99..5a709f6 100644 --- a/server/src/game/startGame.go +++ b/server/src/game/startGame.go @@ -1,7 +1,7 @@ package game import ( - "fmt" + "sirlab.de/go/knowyt/log" ) func (gm *Game) StartGame() { @@ -11,7 +11,7 @@ func (gm *Game) StartGame() { func (gm *Game) ResetGame() { err := gm.changeGameState(STATE_ANY, STATE_IDLE, PHASE_NONE) if err != nil { - fmt.Println(err) + log.ErrorLog(err) return } diff --git a/server/src/go.mod b/server/src/go.mod index 2120fde..eb916d3 100644 --- a/server/src/go.mod +++ b/server/src/go.mod @@ -1,6 +1,6 @@ module sirlab.de/go/knowyt -go 1.16 +go 1.18 require ( github.com/google/uuid v1.3.0 diff --git a/server/src/handler/filehandler.go b/server/src/handler/filehandler.go index 45b5c0a..6ed3eed 100644 --- a/server/src/handler/filehandler.go +++ b/server/src/handler/filehandler.go @@ -5,7 +5,9 @@ import ( "mime" "net/http" "path" - "sirlab.de/go/knowyt/resources" + + "sirlab.de/go/knowyt/log" + Resources "sirlab.de/go/knowyt/resources" ) func FileHandler(w http.ResponseWriter, req *http.Request) { @@ -24,7 +26,7 @@ func FileHandler(w http.ResponseWriter, req *http.Request) { } } if err != nil { - fmt.Printf("%s: %v\n", uri, err) + log.Error("%s: %v\n", uri, err) http.NotFoundHandler().ServeHTTP(w, req) return } diff --git a/server/src/knowyt.go b/server/src/knowyt.go index 5b40436..765c2c8 100644 --- a/server/src/knowyt.go +++ b/server/src/knowyt.go @@ -1,18 +1,19 @@ package main import ( - "fmt" "os" + "sirlab.de/go/knowyt/application" "sirlab.de/go/knowyt/applicationConfig" "sirlab.de/go/knowyt/handler" + "sirlab.de/go/knowyt/log" ) func main() { appConfig := applicationConfig.NewApplicationConfig() app, err := application.NewApplication(appConfig) if err != nil { - fmt.Printf("%v\n", err) + log.ErrorLog(err) os.Exit(1) } mux := handler.NewAuthMux(app) @@ -44,6 +45,6 @@ func main() { mux.PublicHandleFunc("/", handler.FileHandler) // start listening - fmt.Printf("http://localhost:%d/\n", mux.Port) + log.Info("http://localhost:%d/\n", mux.Port) mux.ListenAndServe() } diff --git a/server/src/log/main.go b/server/src/log/main.go new file mode 100644 index 0000000..1a832b1 --- /dev/null +++ b/server/src/log/main.go @@ -0,0 +1,59 @@ +package log + +import ( + "fmt" + "sync" +) + +var lock = &sync.Mutex{} +var loglevel = LOG_NONE + +func SetLoglevel(_loglevel int) { + lock.Lock() + defer lock.Unlock() + + loglevel = _loglevel +} + +func Debug(format string, v ...any) { + lock.Lock() + defer lock.Unlock() + + if loglevel >= LOG_DEBUG { + fmt.Printf(format, v...) + } +} + +func Info(format string, v ...any) { + lock.Lock() + defer lock.Unlock() + + if loglevel >= LOG_INFO { + fmt.Printf(format, v...) + } +} + +func Warn(format string, v ...any) { + lock.Lock() + defer lock.Unlock() + + if loglevel >= LOG_WARN { + fmt.Printf(format, v...) + } +} + +func Error(format string, v ...any) { + lock.Lock() + defer lock.Unlock() + + if loglevel >= LOG_ERROR { + fmt.Printf(format, v...) + } +} + +func ErrorLog(err error) { + lock.Lock() + defer lock.Unlock() + + Error("%v\n", err) +} diff --git a/server/src/log/struct.go b/server/src/log/struct.go new file mode 100644 index 0000000..24c9d2c --- /dev/null +++ b/server/src/log/struct.go @@ -0,0 +1,13 @@ +package log + +const ( + LOG_NONE = 1 << iota + LOG_ERROR + LOG_WARN + LOG_INFO + LOG_DEBUG +) + +type Log struct { + Loglevel int +}