improved libGDX client logging

main
Jonah Bauer 3 years ago
parent 42e87ae3a4
commit a592807dff

@ -44,13 +44,12 @@ public class Client extends TimeoutContext<ClientState, Client> {
} }
public void onMessage(ServerMessage message) { public void onMessage(ServerMessage message) {
log.debug(message.toString());
execute(s -> s.onMessage(this, message)); execute(s -> s.onMessage(this, message));
} }
@Override @Override
protected void onTransition(ClientState from, ClientState to) { 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) { public void send(ClientMessage message) {

@ -2,6 +2,7 @@ package eu.jonahbauer.wizard.client.libgdx;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
import lombok.extern.log4j.Log4j2;
import org.java_websocket.client.WebSocketClient; import org.java_websocket.client.WebSocketClient;
import org.java_websocket.framing.CloseFrame; import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ServerHandshake; import org.java_websocket.handshake.ServerHandshake;
@ -9,6 +10,7 @@ import org.java_websocket.handshake.ServerHandshake;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import java.net.URI; import java.net.URI;
@Log4j2
public class ClientSocket extends WebSocketClient { public class ClientSocket extends WebSocketClient {
public ClientSocket(URI serverUri) { public ClientSocket(URI serverUri) {
super(serverUri); super(serverUri);
@ -25,9 +27,7 @@ public class ClientSocket extends WebSocketClient {
@Override @Override
public void onMessage(String s) { public void onMessage(String s) {
if (WizardGame.DEBUG) { log.debug("Received: {}", s);
System.out.println("Received: " + s);
}
ServerMessage message = ServerMessage.parse(s); ServerMessage message = ServerMessage.parse(s);
Gdx.app.postRunnable(() -> getClient().onMessage(message)); Gdx.app.postRunnable(() -> getClient().onMessage(message));
} }
@ -45,9 +45,7 @@ public class ClientSocket extends WebSocketClient {
@Override @Override
public void send(String text) { public void send(String text) {
if (WizardGame.DEBUG) { log.debug("Sending: {}", text);
System.out.println("Sending: " + text);
}
super.send(text); super.send(text);
} }

@ -11,7 +11,9 @@ import eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinSession;
import eu.jonahbauer.wizard.client.libgdx.state.Lobby; import eu.jonahbauer.wizard.client.libgdx.state.Lobby;
import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage; import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage;
import eu.jonahbauer.wizard.common.model.Configuration; import eu.jonahbauer.wizard.common.model.Configuration;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class CreateGameScreen extends MenuScreen { public class CreateGameScreen extends MenuScreen {
private TextButton buttonBack; private TextButton buttonBack;
@ -130,14 +132,14 @@ public class CreateGameScreen extends MenuScreen {
String sessionName = this.sessionName.getText(); String sessionName = this.sessionName.getText();
if (sessionName.isBlank()) { 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)); this.sessionName.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class));
error = true; error = true;
} }
String playerName = this.playerName.getText(); String playerName = this.playerName.getText();
if (playerName.isBlank()) { 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)); this.playerName.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class));
error = true; error = true;
} }
@ -146,7 +148,7 @@ public class CreateGameScreen extends MenuScreen {
try { try {
timeout = Long.parseLong(this.timeOut.getText()); timeout = Long.parseLong(this.timeOut.getText());
} catch (NumberFormatException e) { } 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)); this.timeOut.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class));
error = true; error = true;
} }
@ -156,7 +158,7 @@ public class CreateGameScreen extends MenuScreen {
int selected = configurations.getSelectedIndex(); int selected = configurations.getSelectedIndex();
config = Configuration.values()[selected]; config = Configuration.values()[selected];
} catch (ArrayIndexOutOfBoundsException e) { } 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)); this.configurations.setStyle(game.data.skin.get("error", SelectBox.SelectBoxStyle.class));
error = true; error = true;
} }

@ -12,6 +12,7 @@ import eu.jonahbauer.wizard.client.libgdx.state.Lobby;
import eu.jonahbauer.wizard.client.libgdx.state.Menu; import eu.jonahbauer.wizard.client.libgdx.state.Menu;
import eu.jonahbauer.wizard.common.messages.client.JoinSessionMessage; import eu.jonahbauer.wizard.common.messages.client.JoinSessionMessage;
import eu.jonahbauer.wizard.common.messages.data.SessionData; import eu.jonahbauer.wizard.common.messages.data.SessionData;
import lombok.extern.log4j.Log4j2;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -19,6 +20,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@Log4j2
public class LobbyScreen extends MenuScreen { public class LobbyScreen extends MenuScreen {
private TextButton buttonBack; private TextButton buttonBack;
@ -211,14 +213,14 @@ public class LobbyScreen extends MenuScreen {
String playerName = this.playerName.getText(); String playerName = this.playerName.getText();
if (playerName.isBlank()) { 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)); this.playerName.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class));
error = true; error = true;
} }
SessionData session = sessionData.get(selectedSession); SessionData session = sessionData.get(selectedSession);
if (session == null) { 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)); this.sessionListContainer.setStyle(game.data.skin.get("error", ScrollPane.ScrollPaneStyle.class));
error = true; error = true;
} }

@ -3,9 +3,11 @@ package eu.jonahbauer.wizard.client.libgdx.state;
import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.Client;
import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import java.util.Optional; import java.util.Optional;
@Log4j2
public abstract class Awaiting extends BaseState implements ClientState { public abstract class Awaiting extends BaseState implements ClientState {
@Override @Override
public Optional<ClientState> onMessage(Client client, ServerMessage message) { public Optional<ClientState> onMessage(Client client, ServerMessage message) {
@ -21,7 +23,7 @@ public abstract class Awaiting extends BaseState implements ClientState {
@Override @Override
public Optional<ClientState> onTimeout(Client client) { public Optional<ClientState> onTimeout(Client client) {
System.out.println("Timed out. Returning to menu"); log.error("Timed out. Returning to menu.");
return Optional.of(new Menu()); return Optional.of(new Menu());
} }

@ -2,27 +2,29 @@ package eu.jonahbauer.wizard.client.libgdx.state;
import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.Client;
import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen; import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen;
import lombok.extern.log4j.Log4j2;
import java.util.Optional; import java.util.Optional;
@Log4j2
public class AwaitingConnection extends Awaiting { public class AwaitingConnection extends Awaiting {
@Override @Override
public Optional<ClientState> onEnter(Client client) { public Optional<ClientState> onEnter(Client client) {
System.out.println("Awaiting connection..."); log.info("Awaiting connection...");
client.getGame().setScreen(new LoadingScreen(client.getGame())); client.getGame().setScreen(new LoadingScreen(client.getGame()));
return super.onEnter(client); return super.onEnter(client);
} }
@Override @Override
public Optional<ClientState> onOpen(Client client) { public Optional<ClientState> onOpen(Client client) {
System.out.println("Connection established."); log.info("Connection established.");
return Optional.of(new AwaitingJoinLobby()); return Optional.of(new AwaitingJoinLobby());
} }
@Override @Override
public Optional<ClientState> onClose(Client client, int code, String reason, boolean remote) { public Optional<ClientState> 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()); return Optional.of(new Menu());
} }
} }

@ -4,14 +4,16 @@ import eu.jonahbauer.wizard.client.libgdx.Client;
import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen; import eu.jonahbauer.wizard.client.libgdx.screens.LoadingScreen;
import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
import eu.jonahbauer.wizard.common.messages.server.SessionListMessage; import eu.jonahbauer.wizard.common.messages.server.SessionListMessage;
import lombok.extern.log4j.Log4j2;
import java.util.Optional; import java.util.Optional;
@Log4j2
public final class AwaitingJoinLobby extends Awaiting { public final class AwaitingJoinLobby extends Awaiting {
@Override @Override
public Optional<ClientState> onEnter(Client client) { public Optional<ClientState> onEnter(Client client) {
System.out.println("Waiting for session list..."); log.info("Waiting for session list...");
client.getGame().setScreen(new LoadingScreen(client.getGame())); client.getGame().setScreen(new LoadingScreen(client.getGame()));
return super.onEnter(client); return super.onEnter(client);
} }
@ -19,6 +21,7 @@ public final class AwaitingJoinLobby extends Awaiting {
@Override @Override
public Optional<ClientState> onMessage(Client client, ServerMessage message) { public Optional<ClientState> onMessage(Client client, ServerMessage message) {
if (message instanceof SessionListMessage list) { if (message instanceof SessionListMessage list) {
log.info("There are {} open sessions.", list.getSessions().size());
return Optional.of(new Lobby(list)); return Optional.of(new Lobby(list));
} else { } else {
return super.onMessage(client, message); return super.onMessage(client, message);

@ -6,10 +6,12 @@ import eu.jonahbauer.wizard.common.messages.server.*;
import eu.jonahbauer.wizard.common.model.Configuration; import eu.jonahbauer.wizard.common.model.Configuration;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@Log4j2
@Getter @Getter
@RequiredArgsConstructor @RequiredArgsConstructor
public final class AwaitingJoinSession extends Awaiting { public final class AwaitingJoinSession extends Awaiting {
@ -21,7 +23,7 @@ public final class AwaitingJoinSession extends Awaiting {
@Override @Override
public Optional<ClientState> onEnter(Client client) { public Optional<ClientState> onEnter(Client client) {
System.out.println("Waiting for acknowledgment..."); log.info("Waiting for acknowledgment...");
client.getGame().setScreen(new LoadingScreen(client.getGame())); client.getGame().setScreen(new LoadingScreen(client.getGame()));
return super.onEnter(client); return super.onEnter(client);
} }
@ -29,19 +31,26 @@ public final class AwaitingJoinSession extends Awaiting {
@Override @Override
public Optional<ClientState> onMessage(Client client, ServerMessage message) { public Optional<ClientState> onMessage(Client client, ServerMessage message) {
if (message instanceof SessionJoinedMessage joined) { if (message instanceof SessionJoinedMessage joined) {
if (session != null && !session.equals(joined.getSession())) throw new RuntimeException(); if (session != null && !session.equals(joined.getSession())) {
return Optional.of(new Session(joined, sessionName, configuration, playerName)); 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) { } else if (message instanceof NackMessage nack) {
switch (nack.getCode()) { switch (nack.getCode()) {
case NackMessage.GAME_ALREADY_STARTED -> System.out.println("Error: Game has already started."); case NackMessage.GAME_ALREADY_STARTED -> log.error("Game has already started.");
case NackMessage.SESSION_FULL -> System.out.println("Error: The session is full."); case NackMessage.SESSION_FULL -> log.error("The session is full.");
case NackMessage.SESSION_NOT_FOUND -> System.out.println("Error: Session not found."); case NackMessage.SESSION_NOT_FOUND -> log.error("Session not found.");
case NackMessage.NAME_TAKEN -> System.out.println("Error: Name already taken."); case NackMessage.NAME_TAKEN -> log.error("Name already taken.");
default -> { return super.onMessage(client, message); } default -> { return super.onMessage(client, message); }
} }
return Optional.of(new AwaitingJoinLobby()); return Optional.of(new AwaitingJoinLobby());
} else if (message instanceof SessionModifiedMessage || message instanceof SessionRemovedMessage) { } else if (message instanceof SessionModifiedMessage || message instanceof SessionRemovedMessage) {
// drop // drop
log.debug("Dropped message {}.", message);
return Optional.empty(); return Optional.empty();
} else { } else {
return super.onMessage(client, message); return super.onMessage(client, message);

@ -2,9 +2,11 @@ package eu.jonahbauer.wizard.client.libgdx.state;
import eu.jonahbauer.wizard.client.libgdx.Client; import eu.jonahbauer.wizard.client.libgdx.Client;
import eu.jonahbauer.wizard.common.messages.server.ServerMessage; import eu.jonahbauer.wizard.common.messages.server.ServerMessage;
import lombok.extern.log4j.Log4j2;
import java.util.Optional; import java.util.Optional;
@Log4j2
public abstract class BaseState implements ClientState { public abstract class BaseState implements ClientState {
@Override @Override
@ -20,16 +22,16 @@ public abstract class BaseState implements ClientState {
@Override @Override
public Optional<ClientState> onClose(Client client, int code, String reason, boolean remote) { public Optional<ClientState> onClose(Client client, int code, String reason, boolean remote) {
if (remote) { if (remote) {
System.out.println("Lost connection " + code + " " + reason); log.error("Lost connection (code={}, reason={})", code, reason);
} else { } else {
System.out.println("Connection closed " + code + " " + reason); log.info("Connection closed (code={}, reason={})", code, reason);
} }
return Optional.of(new Menu()); return Optional.of(new Menu());
} }
protected static Optional<ClientState> unexpectedMessage(Client client, ServerMessage message) { protected static Optional<ClientState> unexpectedMessage(Client client, ServerMessage message) {
// return to menu on unexpected 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()); return Optional.of(new Menu());
} }
} }

@ -8,10 +8,12 @@ import eu.jonahbauer.wizard.common.messages.server.*;
import eu.jonahbauer.wizard.common.model.Configuration; import eu.jonahbauer.wizard.common.model.Configuration;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@Log4j2
@Getter @Getter
public final class Session extends BaseState { public final class Session extends BaseState {
@ -69,7 +71,7 @@ public final class Session extends BaseState {
return Optional.empty(); return Optional.empty();
} else if (nextReady != null && message instanceof NackMessage nack) { } else if (nextReady != null && message instanceof NackMessage nack) {
// TODO display error // TODO display error
System.out.println("Error: " + nack.getMessage()); log.error(nack.getMessage());
sessionScreen.setReady(!nextReady); sessionScreen.setReady(!nextReady);
nextReady = null; nextReady = null;
return Optional.empty(); return Optional.empty();

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="STDOUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%-5level] %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Loading…
Cancel
Save