Unverified Commit 43b7d7a3 authored by BiosNod's avatar BiosNod Committed by GitHub
Browse files

Debug system (#1894)

* Add build (compile) script: gradlew jar

* Move server and services log levels to ConfigContainer, ability to enable/disable show packet payload and loop packets

* Add some loop packets to known list
parent 55928d91
call .\gradlew jar
pause
package emu.grasscutter.config;
import ch.qos.logback.classic.Level;
import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerDebugMode;
......@@ -101,6 +102,7 @@ public class ConfigContainer {
public Game game = new Game();
public Dispatch dispatch = new Dispatch();
public DebugMode debugMode = new DebugMode();
}
public static class Language {
......@@ -150,6 +152,10 @@ public class ConfigContainer {
public int kcpInterval = 20;
/* Controls whether packets should be logged in console or not */
public ServerDebugMode logPackets = ServerDebugMode.NONE;
/* Show packet payload in console or no (in any case the payload is shown in encrypted view) */
public Boolean isShowPacketPayload = false;
/* Show annoying loop packets or no */
public Boolean isShowLoopPackets = false;
public GameOptions gameOptions = new GameOptions();
public JoinOptions joinOptions = new JoinOptions();
......@@ -163,9 +169,33 @@ public class ConfigContainer {
public String defaultName = "Grasscutter";
/* Controls whether http requests should be logged in console or not */
public ServerDebugMode logRequests = ServerDebugMode.NONE;
}
/* Debug options container, used when jar launch argument is -debug | -debugall and override default values
* (see StartupArguments.enableDebug) */
public static class DebugMode {
/* Log level of the main server code (works only with -debug arg) */
public Level serverLoggerLevel = Level.DEBUG;
/* Log level of the third-party services (works only with -debug arg):
javalin, quartz, reflections, jetty, mongodb.driver*/
public Level servicesLoggersLevel = Level.INFO;
/* Controls whether packets should be logged in console or not */
public ServerDebugMode logPackets = ServerDebugMode.ALL;
/* Show packet payload in console or no (in any case the payload is shown in encrypted view) */
public Boolean isShowPacketPayload = false;
/* Show annoying loop packets or no */
public Boolean isShowLoopPackets = false;
/* Controls whether http requests should be logged in console or not */
public ServerDebugMode logRequests = ServerDebugMode.ALL;
}
public static class Encryption {
public boolean useEncryption = true;
/* Should 'https' be appended to URLs? */
......
......@@ -38,6 +38,7 @@ public final class Configuration extends ConfigContainer {
public static final HTTP HTTP_INFO = config.server.http;
public static final Game GAME_INFO = config.server.game;
public static final Dispatch DISPATCH_INFO = config.server.dispatch;
public static final DebugMode DEBUG_MODE_INFO = config.server.debugMode;
public static final Encryption HTTP_ENCRYPTION = config.server.http.encryption;
public static final Policies HTTP_POLICIES = config.server.http.policies;
......
......@@ -27,7 +27,8 @@ public class PacketOpcodesUtils {
PacketOpcodes.PingRsp,
PacketOpcodes.WorldPlayerRTTNotify,
PacketOpcodes.UnionCmdNotify,
PacketOpcodes.QueryPathReq
PacketOpcodes.QueryPathReq,
PacketOpcodes.QueryPathRsp
);
static {
......
......@@ -99,7 +99,7 @@ public class GameServerPacketHandler {
}
// Log unhandled packets
if (GAME_INFO.logPackets == ServerDebugMode.MISSING) {
if (GAME_INFO.logPackets == ServerDebugMode.MISSING || GAME_INFO.logPackets == ServerDebugMode.ALL) {
Grasscutter.getLogger().info("Unhandled packet (" + opcode + "): " + emu.grasscutter.net.packet.PacketOpcodesUtils.getOpcodeName(opcode));
}
}
......
......@@ -98,6 +98,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
public void logPacket(String sendOrRecv, int opcode, byte[] payload) {
Grasscutter.getLogger().info(sendOrRecv + ": " + PacketOpcodesUtils.getOpcodeName(opcode) + " (" + opcode + ")");
if (GAME_INFO.isShowPacketPayload)
System.out.println(Utils.bytesToHex(payload));
}
......@@ -122,7 +123,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
// Log
switch (GAME_INFO.logPackets) {
case ALL -> {
if (!PacketOpcodesUtils.LOOP_PACKETS.contains(packet.getOpcode())) {
if (!PacketOpcodesUtils.LOOP_PACKETS.contains(packet.getOpcode()) || GAME_INFO.isShowLoopPackets) {
logPacket("SEND", packet.getOpcode(), packet.getData());
}
}
......@@ -154,7 +155,6 @@ public class GameSession implements GameSessionManager.KcpChannel {
Grasscutter.getLogger().info(translate("messages.game.connect", this.getAddress().toString()));
}
@Override
public void handleReceive(byte[] bytes) {
// Decrypt and turn back into a packet
......@@ -200,7 +200,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
// Log packet
switch (GAME_INFO.logPackets) {
case ALL -> {
if (!PacketOpcodesUtils.LOOP_PACKETS.contains(opcode)) {
if (!PacketOpcodesUtils.LOOP_PACKETS.contains(opcode) || GAME_INFO.isShowLoopPackets) {
logPacket("RECV", opcode, payload);
}
}
......
......@@ -6,12 +6,13 @@ import emu.grasscutter.BuildConfig;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerRunMode;
import emu.grasscutter.net.packet.PacketOpcodesUtils;
import io.javalin.core.util.JavalinLogger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.function.Function;
import static emu.grasscutter.config.Configuration.*;
/**
* A parser for start-up arguments.
*/
......@@ -80,14 +81,21 @@ public final class StartupArguments {
* @return False to continue execution.
*/
private static boolean enableDebug(String parameter) {
// Get the level by parameter.
var loggerLevel = parameter != null && parameter.equals("all")
? Level.DEBUG : Level.INFO;
if (parameter != null && parameter.equals("all")) {
// Override default debug configs
GAME_INFO.isShowLoopPackets = DEBUG_MODE_INFO.isShowLoopPackets;
GAME_INFO.isShowPacketPayload = DEBUG_MODE_INFO.isShowPacketPayload;
GAME_INFO.logPackets = DEBUG_MODE_INFO.logPackets;
DISPATCH_INFO.logRequests = DEBUG_MODE_INFO.logRequests;
}
// Set the logger to debug.
Grasscutter.getLogger().setLevel(Level.DEBUG);
// Set the main logger to debug.
Grasscutter.getLogger().setLevel(DEBUG_MODE_INFO.serverLoggerLevel);
Grasscutter.getLogger().debug("The logger is now running in debug mode.");
// Log level to other third-party services
Level loggerLevel = DEBUG_MODE_INFO.servicesLoggersLevel;
// Change loggers to debug.
((Logger) LoggerFactory.getLogger("io.javalin"))
.setLevel(loggerLevel);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment