From a592807dfff709e93fd1e33a2702967748178368 Mon Sep 17 00:00:00 2001 From: Jonah Bauer Date: Sun, 9 Jan 2022 22:49:32 +0100 Subject: [PATCH] improved libGDX client logging --- .../wizard/client/libgdx/Client.java | 3 +-- .../wizard/client/libgdx/ClientSocket.java | 10 ++++---- .../libgdx/screens/CreateGameScreen.java | 10 ++++---- .../client/libgdx/screens/LobbyScreen.java | 6 +++-- .../wizard/client/libgdx/state/Awaiting.java | 4 +++- .../libgdx/state/AwaitingConnection.java | 8 ++++--- .../libgdx/state/AwaitingJoinLobby.java | 5 +++- .../libgdx/state/AwaitingJoinSession.java | 23 +++++++++++++------ .../wizard/client/libgdx/state/BaseState.java | 8 ++++--- .../wizard/client/libgdx/state/Session.java | 4 +++- .../core/src/main/resources/log4j2.xml | 14 +++++++++++ 11 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 wizard-client/wizard-client-libgdx/core/src/main/resources/log4j2.xml diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/Client.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/Client.java index b754f65..79d2882 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/Client.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/Client.java @@ -44,13 +44,12 @@ public class Client extends TimeoutContext { } public void onMessage(ServerMessage message) { - log.debug(message.toString()); execute(s -> s.onMessage(this, message)); } @Override protected void onTransition(ClientState from, ClientState to) { - System.out.println("Transistion from " + from.getClass().getSimpleName() + " to " + to.getClass().getSimpleName()); + log.debug("Transition from {} to {}.", from.getClass().getSimpleName(), to.getClass().getSimpleName()); } public void send(ClientMessage message) { diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/ClientSocket.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/ClientSocket.java index 80b273a..ece6726 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/ClientSocket.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/ClientSocket.java @@ -2,6 +2,7 @@ package eu.jonahbauer.wizard.client.libgdx; import com.badlogic.gdx.Gdx; import eu.jonahbauer.wizard.common.messages.server.ServerMessage; +import lombok.extern.log4j.Log4j2; import org.java_websocket.client.WebSocketClient; import org.java_websocket.framing.CloseFrame; import org.java_websocket.handshake.ServerHandshake; @@ -9,6 +10,7 @@ import org.java_websocket.handshake.ServerHandshake; import javax.net.ssl.SSLSocketFactory; import java.net.URI; +@Log4j2 public class ClientSocket extends WebSocketClient { public ClientSocket(URI serverUri) { super(serverUri); @@ -25,9 +27,7 @@ public class ClientSocket extends WebSocketClient { @Override public void onMessage(String s) { - if (WizardGame.DEBUG) { - System.out.println("Received: " + s); - } + log.debug("Received: {}", s); ServerMessage message = ServerMessage.parse(s); Gdx.app.postRunnable(() -> getClient().onMessage(message)); } @@ -45,9 +45,7 @@ public class ClientSocket extends WebSocketClient { @Override public void send(String text) { - if (WizardGame.DEBUG) { - System.out.println("Sending: " + text); - } + log.debug("Sending: {}", text); super.send(text); } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java index 0f2952c..ac82020 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java @@ -11,7 +11,9 @@ import eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinSession; import eu.jonahbauer.wizard.client.libgdx.state.Lobby; import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage; import eu.jonahbauer.wizard.common.model.Configuration; +import lombok.extern.log4j.Log4j2; +@Log4j2 public class CreateGameScreen extends MenuScreen { private TextButton buttonBack; @@ -130,14 +132,14 @@ public class CreateGameScreen extends MenuScreen { String sessionName = this.sessionName.getText(); if (sessionName.isBlank()) { - System.out.println("Please choose a session name."); + log.warn("Please choose a session name."); this.sessionName.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class)); error = true; } String playerName = this.playerName.getText(); if (playerName.isBlank()) { - System.out.println("Please choose a name."); + log.warn("Please choose a name."); this.playerName.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class)); error = true; } @@ -146,7 +148,7 @@ public class CreateGameScreen extends MenuScreen { try { timeout = Long.parseLong(this.timeOut.getText()); } catch (NumberFormatException e) { - System.out.println("Please choose a valid timeout."); + log.warn("Please choose a valid timeout."); this.timeOut.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class)); error = true; } @@ -156,7 +158,7 @@ public class CreateGameScreen extends MenuScreen { int selected = configurations.getSelectedIndex(); config = Configuration.values()[selected]; } catch (ArrayIndexOutOfBoundsException e) { - System.out.println("Please select a valid configuration."); + log.warn("Please select a valid configuration."); this.configurations.setStyle(game.data.skin.get("error", SelectBox.SelectBoxStyle.class)); error = true; } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/LobbyScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/LobbyScreen.java index fd68931..00e5e7d 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/LobbyScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/LobbyScreen.java @@ -12,6 +12,7 @@ import eu.jonahbauer.wizard.client.libgdx.state.Lobby; import eu.jonahbauer.wizard.client.libgdx.state.Menu; import eu.jonahbauer.wizard.common.messages.client.JoinSessionMessage; import eu.jonahbauer.wizard.common.messages.data.SessionData; +import lombok.extern.log4j.Log4j2; import java.util.Arrays; import java.util.HashMap; @@ -19,6 +20,7 @@ import java.util.Map; import java.util.UUID; import java.util.stream.StreamSupport; +@Log4j2 public class LobbyScreen extends MenuScreen { private TextButton buttonBack; @@ -211,14 +213,14 @@ public class LobbyScreen extends MenuScreen { String playerName = this.playerName.getText(); if (playerName.isBlank()) { - System.out.println("Please choose a player name"); + log.warn("Please choose a player name"); this.playerName.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class)); error = true; } SessionData session = sessionData.get(selectedSession); if (session == null) { - System.out.println("Please select a session."); + log.warn("Please select a session."); this.sessionListContainer.setStyle(game.data.skin.get("error", ScrollPane.ScrollPaneStyle.class)); error = true; } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Awaiting.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Awaiting.java index e026357..1ba1553 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Awaiting.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Awaiting.java @@ -3,9 +3,11 @@ package eu.jonahbauer.wizard.client.libgdx.state; import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import lombok.SneakyThrows; +import lombok.extern.log4j.Log4j2; import java.util.Optional; +@Log4j2 public abstract class Awaiting extends BaseState implements ClientState { @Override public Optional onMessage(Client client, ServerMessage message) { @@ -21,7 +23,7 @@ public abstract class Awaiting extends BaseState implements ClientState { @Override public Optional onTimeout(Client client) { - System.out.println("Timed out. Returning to menu"); + log.error("Timed out. Returning to menu."); return Optional.of(new Menu()); } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingConnection.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingConnection.java index 9eda580..b3341cf 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingConnection.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingConnection.java @@ -2,27 +2,29 @@ package eu.jonahbauer.wizard.client.libgdx.state; import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen; +import lombok.extern.log4j.Log4j2; import java.util.Optional; +@Log4j2 public class AwaitingConnection extends Awaiting { @Override public Optional onEnter(Client client) { - System.out.println("Awaiting connection..."); + log.info("Awaiting connection..."); client.getGame().setScreen(new LoadingScreen(client.getGame())); return super.onEnter(client); } @Override public Optional onOpen(Client client) { - System.out.println("Connection established."); + log.info("Connection established."); return Optional.of(new AwaitingJoinLobby()); } @Override public Optional onClose(Client client, int code, String reason, boolean remote) { - System.out.printf("Connection could not be established. (code=%d, reason=%s)%n", code, reason); + log.error("Connection could not be established. (code={}, reason={}, remote={})", code, reason, remote); return Optional.of(new Menu()); } } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinLobby.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinLobby.java index 7f768b5..fd0bbfd 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinLobby.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinLobby.java @@ -4,14 +4,16 @@ import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen; import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import eu.jonahbauer.wizard.common.messages.server.SessionListMessage; +import lombok.extern.log4j.Log4j2; import java.util.Optional; +@Log4j2 public final class AwaitingJoinLobby extends Awaiting { @Override public Optional onEnter(Client client) { - System.out.println("Waiting for session list..."); + log.info("Waiting for session list..."); client.getGame().setScreen(new LoadingScreen(client.getGame())); return super.onEnter(client); } @@ -19,6 +21,7 @@ public final class AwaitingJoinLobby extends Awaiting { @Override public Optional onMessage(Client client, ServerMessage message) { if (message instanceof SessionListMessage list) { + log.info("There are {} open sessions.", list.getSessions().size()); return Optional.of(new Lobby(list)); } else { return super.onMessage(client, message); diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinSession.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinSession.java index a77b0be..2635842 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinSession.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/AwaitingJoinSession.java @@ -6,10 +6,12 @@ import eu.jonahbauer.wizard.common.messages.server.*; import eu.jonahbauer.wizard.common.model.Configuration; import lombok.Getter; import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; import java.util.Optional; import java.util.UUID; +@Log4j2 @Getter @RequiredArgsConstructor public final class AwaitingJoinSession extends Awaiting { @@ -21,7 +23,7 @@ public final class AwaitingJoinSession extends Awaiting { @Override public Optional onEnter(Client client) { - System.out.println("Waiting for acknowledgment..."); + log.info("Waiting for acknowledgment..."); client.getGame().setScreen(new LoadingScreen(client.getGame())); return super.onEnter(client); } @@ -29,19 +31,26 @@ public final class AwaitingJoinSession extends Awaiting { @Override public Optional onMessage(Client client, ServerMessage message) { if (message instanceof SessionJoinedMessage joined) { - if (session != null && !session.equals(joined.getSession())) throw new RuntimeException(); - return Optional.of(new Session(joined, sessionName, configuration, playerName)); + if (session != null && !session.equals(joined.getSession())) { + return super.onMessage(client, message); + } else { + log.info("There are {} players in this session.", joined.getPlayers().size()); + log.info("Your uuid is {}.", joined.getPlayer()); + log.info("Your secret is {}.", joined.getSecret()); + return Optional.of(new Session(joined, sessionName, configuration, playerName)); + } } else if (message instanceof NackMessage nack) { switch (nack.getCode()) { - case NackMessage.GAME_ALREADY_STARTED -> System.out.println("Error: Game has already started."); - case NackMessage.SESSION_FULL -> System.out.println("Error: The session is full."); - case NackMessage.SESSION_NOT_FOUND -> System.out.println("Error: Session not found."); - case NackMessage.NAME_TAKEN -> System.out.println("Error: Name already taken."); + case NackMessage.GAME_ALREADY_STARTED -> log.error("Game has already started."); + case NackMessage.SESSION_FULL -> log.error("The session is full."); + case NackMessage.SESSION_NOT_FOUND -> log.error("Session not found."); + case NackMessage.NAME_TAKEN -> log.error("Name already taken."); default -> { return super.onMessage(client, message); } } return Optional.of(new AwaitingJoinLobby()); } else if (message instanceof SessionModifiedMessage || message instanceof SessionRemovedMessage) { // drop + log.debug("Dropped message {}.", message); return Optional.empty(); } else { return super.onMessage(client, message); diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/BaseState.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/BaseState.java index 45f37f9..d568bc7 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/BaseState.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/BaseState.java @@ -2,9 +2,11 @@ package eu.jonahbauer.wizard.client.libgdx.state; import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.common.messages.server.ServerMessage; +import lombok.extern.log4j.Log4j2; import java.util.Optional; +@Log4j2 public abstract class BaseState implements ClientState { @Override @@ -20,16 +22,16 @@ public abstract class BaseState implements ClientState { @Override public Optional onClose(Client client, int code, String reason, boolean remote) { if (remote) { - System.out.println("Lost connection " + code + " " + reason); + log.error("Lost connection (code={}, reason={})", code, reason); } else { - System.out.println("Connection closed " + code + " " + reason); + log.info("Connection closed (code={}, reason={})", code, reason); } return Optional.of(new Menu()); } protected static Optional unexpectedMessage(Client client, ServerMessage message) { // return to menu on unexpected message - System.out.println("Fatal: Unexpected message " + message + ". Returning to menu."); + log.fatal("Unexpected message {}. Returning to menu.", message); return Optional.of(new Menu()); } } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Session.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Session.java index 0887f89..6b34077 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Session.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/state/Session.java @@ -8,10 +8,12 @@ import eu.jonahbauer.wizard.common.messages.server.*; import eu.jonahbauer.wizard.common.model.Configuration; import lombok.Getter; import lombok.Setter; +import lombok.extern.log4j.Log4j2; import java.util.Optional; import java.util.UUID; +@Log4j2 @Getter public final class Session extends BaseState { @@ -69,7 +71,7 @@ public final class Session extends BaseState { return Optional.empty(); } else if (nextReady != null && message instanceof NackMessage nack) { // TODO display error - System.out.println("Error: " + nack.getMessage()); + log.error(nack.getMessage()); sessionScreen.setReady(!nextReady); nextReady = null; return Optional.empty(); diff --git a/wizard-client/wizard-client-libgdx/core/src/main/resources/log4j2.xml b/wizard-client/wizard-client-libgdx/core/src/main/resources/log4j2.xml new file mode 100644 index 0000000..5597861 --- /dev/null +++ b/wizard-client/wizard-client-libgdx/core/src/main/resources/log4j2.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file