From e99feb03d5b86de7053bb8cac12143e4b2b3569b Mon Sep 17 00:00:00 2001 From: dongresource Date: Sun, 23 Aug 2020 23:09:31 +0200 Subject: [PATCH] Add verbosity levels. --- config.ini | 8 ++++++-- src/CNLoginServer.cpp | 6 +++--- src/CNProtocol.cpp | 22 ++++++++++++++++++++++ src/CNProtocol.hpp | 6 +++++- src/CNShardServer.cpp | 6 ++---- src/settings.cpp | 4 ++-- src/settings.hpp | 2 +- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/config.ini b/config.ini index 5586a25..c63fe78 100644 --- a/config.ini +++ b/config.ini @@ -1,5 +1,9 @@ -# should the server print every packet it receives? -verbose=false +# verbosity level +# 0 = mostly silence +# 1 = debug prints and unknown packets +# 2 = print all packets except LIVE_CHECK and movement +# 3 = print all packets +verbosity=1 # Login Server configuration [login] diff --git a/src/CNLoginServer.cpp b/src/CNLoginServer.cpp index e165c55..5e62ea4 100644 --- a/src/CNLoginServer.cpp +++ b/src/CNLoginServer.cpp @@ -17,8 +17,7 @@ CNLoginServer::CNLoginServer(uint16_t p) { } void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { - if (settings::VERBOSE) - std::cout << "OpenFusion: received " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl; + printPacket(data, CL2LS); switch (data->type) { case P_CL2LS_REQ_LOGIN: { @@ -280,7 +279,8 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { break; } default: - std::cerr << "OpenFusion: LOGIN UNIMPLM ERR. PacketType: " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl; + if (settings::VERBOSITY) + std::cerr << "OpenFusion: LOGIN UNIMPLM ERR. PacketType: " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl; break; } } diff --git a/src/CNProtocol.cpp b/src/CNProtocol.cpp index d2213d9..d5537a0 100644 --- a/src/CNProtocol.cpp +++ b/src/CNProtocol.cpp @@ -352,6 +352,28 @@ void CNServer::kill() { connections.clear(); } +void CNServer::printPacket(CNPacketData *data, int type) { + if (settings::VERBOSITY < 2) + return; + + if (settings::VERBOSITY < 3) switch (data->type) { + case P_CL2LS_REP_LIVE_CHECK: + case P_CL2FE_REP_LIVE_CHECK: + case P_CL2FE_REQ_PC_MOVE: + case P_CL2FE_REQ_PC_JUMP: + case P_CL2FE_REQ_PC_SLOPE: + case P_CL2FE_REQ_PC_MOVEPLATFORM: + case P_CL2FE_REQ_PC_MOVETRANSPORTATION: + case P_CL2FE_REQ_PC_ZIPLINE: + case P_CL2FE_REQ_PC_JUMPPAD: + case P_CL2FE_REQ_PC_LAUNCHER: + case P_CL2FE_REQ_PC_STOP: + return; + } + + std::cout << "OpenFusion: received " << Defines::p2str(type, data->type) << " (" << data->type << ")" << std::endl; +} + void CNServer::newConnection(CNSocket* cns) {} // stubbed void CNServer::killConnection(CNSocket* cns) {} // stubbed void CNServer::onTimer() {} // stubbed diff --git a/src/CNProtocol.hpp b/src/CNProtocol.hpp index 07620b7..654bd81 100644 --- a/src/CNProtocol.hpp +++ b/src/CNProtocol.hpp @@ -1,7 +1,7 @@ #pragma once #define MAX_PACKETSIZE 8192 -#define DEBUGLOG(x) x +#define DEBUGLOG(x) if (settings::VERBOSITY) {x}; #include #include @@ -38,6 +38,9 @@ #include #include +#include "Defines.hpp" +#include "settings.hpp" + #if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS) #include "mingw/mingw.mutex.h" #else @@ -147,6 +150,7 @@ public: void start(); void kill(); + static void printPacket(CNPacketData *data, int type); virtual void newConnection(CNSocket* cns); virtual void killConnection(CNSocket* cns); virtual void onTimer(); // called every 2 seconds diff --git a/src/CNShardServer.cpp b/src/CNShardServer.cpp index 9a202ba..accd53a 100644 --- a/src/CNShardServer.cpp +++ b/src/CNShardServer.cpp @@ -18,13 +18,11 @@ CNShardServer::CNShardServer(uint16_t p) { } void CNShardServer::handlePacket(CNSocket* sock, CNPacketData* data) { - if (settings::VERBOSE) - std::cout << "OpenFusion: received " << Defines::p2str(CL2FE, data->type) << " (" << data->type << ")" << std::endl; - + printPacket(data, CL2FE); if (ShardPackets.find(data->type) != ShardPackets.end()) ShardPackets[data->type](sock, data); - else + else if (settings::VERBOSITY) std::cerr << "OpenFusion: SHARD UNIMPLM ERR. PacketType: " << Defines::p2str(CL2FE, data->type) << " (" << data->type << ")" << std::endl; } diff --git a/src/settings.cpp b/src/settings.cpp index 4922f5d..efa5c09 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -3,7 +3,7 @@ #include "contrib/INIReader.hpp" // defaults :) -bool settings::VERBOSE = false; +int settings::VERBOSITY = 1; int settings::LOGINPORT = 8001; bool settings::LOGINRANDCHARACTERS = false; @@ -32,7 +32,7 @@ void settings::init() { return; } - VERBOSE = reader.GetBoolean("", "verbose", VERBOSE); + VERBOSITY = reader.GetInteger("", "verbosity", VERBOSITY); LOGINPORT = reader.GetInteger("login", "port", LOGINPORT); LOGINRANDCHARACTERS = reader.GetBoolean("login", "randomcharacters", LOGINRANDCHARACTERS); SHARDPORT = reader.GetInteger("shard", "port", SHARDPORT); diff --git a/src/settings.hpp b/src/settings.hpp index 82c4c75..ef9e54c 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -1,7 +1,7 @@ #pragma once namespace settings { - extern bool VERBOSE; + extern int VERBOSITY; extern int LOGINPORT; extern bool LOGINRANDCHARACTERS; extern int SHARDPORT;