From 809a851c71d1c8288e1fbe07d2bb4cade5766230 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Tue, 31 Aug 2021 11:50:06 +0300 Subject: [PATCH] add trace() as DEBUG debug() Replace DEBUG debug() with trace() everywhere --- include/Common.h | 11 ++++++++++ include/CustomAssert.h | 2 +- src/Client.cpp | 1 - src/Common.cpp | 4 +--- src/THeartbeatThread.cpp | 8 ++------ src/TNetwork.cpp | 30 +--------------------------- src/TServer.cpp | 43 +++++++++++----------------------------- src/VehicleData.cpp | 10 +++------- src/main.cpp | 6 +++--- 9 files changed, 34 insertions(+), 81 deletions(-) diff --git a/include/Common.h b/include/Common.h index 4166c1c..0aa92cb 100644 --- a/include/Common.h +++ b/include/Common.h @@ -129,6 +129,17 @@ void RegisterThread(const std::string& str); Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \ } \ } while (false) +// trace() is a debug-build debug() +#if defined(DEBUG) +#define trace(x) \ + do { \ + if (Application::Settings.DebugModeEnabled) { \ + Application::Console().Write(_this_location + std::string("[TRACE] ") + (x)); \ + } \ + } while (false) +#else +#define trace(x) +#endif // defined(DEBUG) void LogChatMessage(const std::string& name, int id, const std::string& msg); diff --git a/include/CustomAssert.h b/include/CustomAssert.h index 85db86d..f2cdb74 100644 --- a/include/CustomAssert.h +++ b/include/CustomAssert.h @@ -40,7 +40,7 @@ static const char* const ANSI_WHITE_BOLD = "\u001b[37;1m"; static const char* const ANSI_BOLD = "\u001b[1m"; static const char* const ANSI_UNDERLINE = "\u001b[4m"; -#if DEBUG +#ifdef DEBUG #include inline void _assert([[maybe_unused]] const char* file, [[maybe_unused]] const char* function, [[maybe_unused]] unsigned line, [[maybe_unused]] const char* condition_string, [[maybe_unused]] bool result) { diff --git a/src/Client.cpp b/src/Client.cpp index a57837a..ae64ba6 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -92,7 +92,6 @@ TClient::TClient(TServer& Server) void TClient::UpdatePingTime() { mLastPingTime = std::chrono::high_resolution_clock::now(); - //debug(GetName() + ": " + std::string("ping time updated!: ") + ((SecondsSinceLastPing() == 0) ? "OK" : "ERR")); } int TClient::SecondsSinceLastPing() { auto seconds = std::chrono::duration_cast( diff --git a/src/Common.cpp b/src/Common.cpp index 72ca0eb..3fe52bf 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -71,9 +71,7 @@ void Application::CheckForUpdates() { } } else { warn("Unable to fetch version from backend."); -#if DEBUG - debug("got " + Response); -#endif // DEBUG + trace("got " + Response); auto Lock = Sentry.CreateExclusiveContext(); Sentry.SetContext("get-response", { { "response", Response } }); Sentry.LogError("failed to get server version", _file_basename, _line); diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 181f40f..d650966 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -44,9 +44,7 @@ void THeartbeatThread::operator()() { { { "response-body", T }, { "request-body", Body } }); Sentry.SetTransaction(transaction); -#if DEBUG - debug("sending log to sentry: " + std::to_string(status) + " for " + transaction); -#endif // DEBUG + trace("sending log to sentry: " + std::to_string(status) + " for " + transaction); Sentry.Log(SentryLevel::Error, "default", Http::Status::ToString(status) + " (" + std::to_string(status) + ")"); }; @@ -55,9 +53,7 @@ void THeartbeatThread::operator()() { T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false, &ResponseCode); if (T.substr(0, 2) != "20" || ResponseCode != 200) { -#if DEBUG - debug("got " + T + " from backend"); -#endif // DEBUG + trace("got " + T + " from backend"); SentryReportError(Application::GetBackendHostname() + Target, ResponseCode); std::this_thread::sleep_for(std::chrono::milliseconds(500)); T = Http::POST(Application::GetBackup1Hostname(), Target, {}, Body, false, &ResponseCode); diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index ff0f739..d0972f3 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -331,7 +331,6 @@ void TNetwork::Authentication(SOCKET TCPSock) { } debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles()); - debug("There are " + std::to_string(mServer.ClientCount()) + " known clients"); mServer.ForEachClient([&](const std::weak_ptr& ClientPtr) -> bool { std::shared_ptr Cl; { @@ -341,10 +340,7 @@ void TNetwork::Authentication(SOCKET TCPSock) { } else return true; } - info("Client Iteration: Name -> " + Cl->GetName() + ", Guest -> " + std::to_string(Cl->IsGuest()) + ", Roles -> " + Cl->GetRoles()); if (Cl->GetName() == Client->GetName() && Cl->IsGuest() == Client->IsGuest()) { - info("New client matched with current iteration"); - info("Old client (" + Cl->GetName() + ") kicked: Reconnecting"); CloseSocketProper(Cl->GetTCPSock()); Cl->SetStatus(-2); return false; @@ -422,7 +418,7 @@ bool TNetwork::TCPSend(TClient& c, const std::string& Data, bool IsSync) { bool TNetwork::CheckBytes(TClient& c, int32_t BytesRcv) { if (BytesRcv == 0) { - debug("(TCP) Connection closing..."); + trace("(TCP) Connection closing..."); if (c.GetStatus() > -1) c.SetStatus(-1); return false; @@ -434,7 +430,6 @@ bool TNetwork::CheckBytes(TClient& c, int32_t BytesRcv) { #endif // WIN32 if (c.GetStatus() > -1) c.SetStatus(-1); - info(("Closing socket in CheckBytes, BytesRcv < 0")); CloseSocketProper(c.GetTCPSock()); return false; } @@ -450,22 +445,13 @@ std::string TNetwork::TCPRcv(TClient& c) { do { Temp = recv(c.GetTCPSock(), &Data[BytesRcv], 4 - BytesRcv, 0); if (!CheckBytes(c, Temp)) { -#ifdef DEBUG - warn(std::string(__func__) + (": failed on CheckBytes in while(BytesRcv < 4)")); -#endif // DEBUG return ""; } BytesRcv += Temp; } while (size_t(BytesRcv) < sizeof(Header)); memcpy(&Header, &Data[0], sizeof(Header)); -#ifdef DEBUG - //debug(std::string(__func__) + (": expecting ") + std::to_string(Header) + (" bytes.")); -#endif // DEBUG if (!CheckBytes(c, BytesRcv)) { -#ifdef DEBUG - warn(std::string(__func__) + (": failed on CheckBytes")); -#endif // DEBUG return ""; } if (Header < 100 * MB) { @@ -479,29 +465,15 @@ std::string TNetwork::TCPRcv(TClient& c) { do { Temp = recv(c.GetTCPSock(), &Data[BytesRcv], Header - BytesRcv, 0); if (!CheckBytes(c, Temp)) { -#ifdef DEBUG - warn(std::string(__func__) + (": failed on CheckBytes in while(BytesRcv < Header)")); -#endif // DEBUG - return ""; } -#ifdef DEBUG - //debug(std::string(__func__) + (": Temp: ") + std::to_string(Temp) + (", BytesRcv: ") + std::to_string(BytesRcv)); -#endif // DEBUG BytesRcv += Temp; } while (BytesRcv < Header); -#ifdef DEBUG - //debug(std::string(__func__) + (": finished recv with Temp: ") + std::to_string(Temp) + (", BytesRcv: ") + std::to_string(BytesRcv)); -#endif // DEBUG std::string Ret(Data.data(), Header); if (Ret.substr(0, 4) == "ABG:") { Ret = DeComp(Ret.substr(4)); } -#ifdef DEBUG - //debug("Parsing from " + c->GetName() + " -> " +std::to_string(Ret.size())); -#endif - return Ret; } diff --git a/src/TServer.cpp b/src/TServer.cpp index f7531e6..9c294a1 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -90,9 +90,7 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::string Pac } switch (Code) { case 'H': // initial connection -#ifdef DEBUG - debug(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")"); -#endif + trace(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")"); if (!Network.SyncClient(Client)) { // TODO handle } @@ -114,15 +112,11 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::string Pac ParseVehicle(*LockedClient, Packet, Network); return; case 'J': -#ifdef DEBUG - debug(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); -#endif + trace(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); Network.SendToAll(LockedClient.get(), Packet, false, true); return; case 'C': -#ifdef DEBUG - debug(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); -#endif + trace(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos) break; Res = TriggerLuaEvent("onChatMessage", false, nullptr, std::make_unique(TLuaArg { { LockedClient->GetID(), LockedClient->GetName(), Packet.substr(Packet.find(':', 3) + 1) } }), true); @@ -132,14 +126,13 @@ void TServer::GlobalParser(const std::weak_ptr& Client, std::string Pac Network.SendToAll(nullptr, Packet, true, true); return; case 'E': -#ifdef DEBUG - debug(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); -#endif + trace(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); HandleEvent(*LockedClient, Packet); return; case 'N': - debug("got 'N' packet (" + std::to_string(Packet.size()) + ")"); + trace("got 'N' packet (" + std::to_string(Packet.size()) + ")"); Network.SendToAll(LockedClient.get(), Packet, false, true); + return; default: return; } @@ -199,9 +192,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ std::string Data = Packet.substr(3), pid, vid; switch (Code) { //Spawned Destroyed Switched/Moved NotFound Reset case 's': -#ifdef DEBUG - debug(std::string(("got 'Os' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); -#endif + trace(std::string(("got 'Os' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); if (Data.at(0) == '0') { int CarID = c.GetOpenCarID(); debug(c.GetName() + (" created a car with ID ") + std::to_string(CarID)); @@ -226,9 +217,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ } return; case 'c': -#ifdef DEBUG - debug(std::string(("got 'Oc' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); -#endif + trace(std::string(("got 'Oc' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); pid = Data.substr(0, Data.find('-')); vid = Data.substr(Data.find('-') + 1, Data.find(':', 1) - Data.find('-') - 1); if (pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos) { @@ -259,9 +248,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ } return; case 'd': -#ifdef DEBUG - debug(std::string(("got 'Od' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); -#endif + trace(std::string(("got 'Od' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); pid = Data.substr(0, Data.find('-')); vid = Data.substr(Data.find('-') + 1); if (pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos) { @@ -280,9 +267,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ } return; case 'r': -#ifdef DEBUG - debug(std::string(("got 'Or' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); -#endif + trace(std::string(("got 'Or' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); Pos = int(Data.find('-')); pid = Data.substr(0, Pos++); vid = Data.substr(Pos, Data.find(':') - Pos); @@ -301,15 +286,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ } return; case 't': -#ifdef DEBUG - debug(std::string(("got 'Ot' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); -#endif + trace(std::string(("got 'Ot' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")")); Network.SendToAll(&c, Packet, false, true); return; default: -#ifdef DEBUG - warn(std::string(("possibly not implemented: '") + Packet + ("' (") + std::to_string(Packet.size()) + (")"))); -#endif // DEBUG + trace(std::string(("possibly not implemented: '") + Packet + ("' (") + std::to_string(Packet.size()) + (")"))); return; } } diff --git a/src/VehicleData.cpp b/src/VehicleData.cpp index dd56d1e..d13bf68 100644 --- a/src/VehicleData.cpp +++ b/src/VehicleData.cpp @@ -1,18 +1,14 @@ #include "VehicleData.h" -#include #include "Common.h" +#include TVehicleData::TVehicleData(int ID, std::string Data) : mID(ID) , mData(std::move(Data)) { -#ifdef DEBUG - debug("vehicle " + std::to_string(mID) + " constructed"); -#endif + trace("vehicle " + std::to_string(mID) + " constructed"); } TVehicleData::~TVehicleData() { -#ifdef DEBUG - debug("vehicle " + std::to_string(mID) + " destroyed"); -#endif + trace("vehicle " + std::to_string(mID) + " destroyed"); } diff --git a/src/main.cpp b/src/main.cpp index 888be84..e9620f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,9 +48,7 @@ TSentry Sentry { SECRET_SENTRY_URL }; int main(int argc, char** argv) try { #ifdef __unix -#if DEBUG - info("registering handlers for SIGINT, SIGTERM, SIGPIPE"); -#endif // DEBUG + trace("registering handlers for SIGINT, SIGTERM, SIGPIPE"); signal(SIGPIPE, UnixSignalHandler); signal(SIGTERM, UnixSignalHandler); #ifndef DEBUG @@ -85,6 +83,8 @@ int main(int argc, char** argv) try { RegisterThread("Main"); + trace("Running in debug mode on a debug build"); + Sentry.SetupUser(); Sentry.PrintWelcome(); TResourceManager ResourceManager;