add trace() as DEBUG debug()

Replace DEBUG debug() with trace() everywhere
This commit is contained in:
Lion Kortlepel 2021-08-31 11:50:06 +03:00 committed by Lion
parent fe36191baf
commit 809a851c71
9 changed files with 34 additions and 81 deletions

View File

@ -129,6 +129,17 @@ void RegisterThread(const std::string& str);
Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \ Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \
} \ } \
} while (false) } 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); void LogChatMessage(const std::string& name, int id, const std::string& msg);

View File

@ -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_BOLD = "\u001b[1m";
static const char* const ANSI_UNDERLINE = "\u001b[4m"; static const char* const ANSI_UNDERLINE = "\u001b[4m";
#if DEBUG #ifdef DEBUG
#include <iostream> #include <iostream>
inline void _assert([[maybe_unused]] const char* file, [[maybe_unused]] const char* function, [[maybe_unused]] unsigned line, 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) { [[maybe_unused]] const char* condition_string, [[maybe_unused]] bool result) {

View File

@ -92,7 +92,6 @@ TClient::TClient(TServer& Server)
void TClient::UpdatePingTime() { void TClient::UpdatePingTime() {
mLastPingTime = std::chrono::high_resolution_clock::now(); mLastPingTime = std::chrono::high_resolution_clock::now();
//debug(GetName() + ": " + std::string("ping time updated!: ") + ((SecondsSinceLastPing() == 0) ? "OK" : "ERR"));
} }
int TClient::SecondsSinceLastPing() { int TClient::SecondsSinceLastPing() {
auto seconds = std::chrono::duration_cast<std::chrono::seconds>( auto seconds = std::chrono::duration_cast<std::chrono::seconds>(

View File

@ -71,9 +71,7 @@ void Application::CheckForUpdates() {
} }
} else { } else {
warn("Unable to fetch version from backend."); warn("Unable to fetch version from backend.");
#if DEBUG trace("got " + Response);
debug("got " + Response);
#endif // DEBUG
auto Lock = Sentry.CreateExclusiveContext(); auto Lock = Sentry.CreateExclusiveContext();
Sentry.SetContext("get-response", { { "response", Response } }); Sentry.SetContext("get-response", { { "response", Response } });
Sentry.LogError("failed to get server version", _file_basename, _line); Sentry.LogError("failed to get server version", _file_basename, _line);

View File

@ -44,9 +44,7 @@ void THeartbeatThread::operator()() {
{ { "response-body", T }, { { "response-body", T },
{ "request-body", Body } }); { "request-body", Body } });
Sentry.SetTransaction(transaction); Sentry.SetTransaction(transaction);
#if DEBUG trace("sending log to sentry: " + std::to_string(status) + " for " + transaction);
debug("sending log to sentry: " + std::to_string(status) + " for " + transaction);
#endif // DEBUG
Sentry.Log(SentryLevel::Error, "default", Http::Status::ToString(status) + " (" + std::to_string(status) + ")"); 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); T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false, &ResponseCode);
if (T.substr(0, 2) != "20" || ResponseCode != 200) { if (T.substr(0, 2) != "20" || ResponseCode != 200) {
#if DEBUG trace("got " + T + " from backend");
debug("got " + T + " from backend");
#endif // DEBUG
SentryReportError(Application::GetBackendHostname() + Target, ResponseCode); SentryReportError(Application::GetBackendHostname() + Target, ResponseCode);
std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::this_thread::sleep_for(std::chrono::milliseconds(500));
T = Http::POST(Application::GetBackup1Hostname(), Target, {}, Body, false, &ResponseCode); T = Http::POST(Application::GetBackup1Hostname(), Target, {}, Body, false, &ResponseCode);

View File

@ -331,7 +331,6 @@ void TNetwork::Authentication(SOCKET TCPSock) {
} }
debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles()); 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<TClient>& ClientPtr) -> bool { mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
std::shared_ptr<TClient> Cl; std::shared_ptr<TClient> Cl;
{ {
@ -341,10 +340,7 @@ void TNetwork::Authentication(SOCKET TCPSock) {
} else } else
return true; 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()) { 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()); CloseSocketProper(Cl->GetTCPSock());
Cl->SetStatus(-2); Cl->SetStatus(-2);
return false; 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) { bool TNetwork::CheckBytes(TClient& c, int32_t BytesRcv) {
if (BytesRcv == 0) { if (BytesRcv == 0) {
debug("(TCP) Connection closing..."); trace("(TCP) Connection closing...");
if (c.GetStatus() > -1) if (c.GetStatus() > -1)
c.SetStatus(-1); c.SetStatus(-1);
return false; return false;
@ -434,7 +430,6 @@ bool TNetwork::CheckBytes(TClient& c, int32_t BytesRcv) {
#endif // WIN32 #endif // WIN32
if (c.GetStatus() > -1) if (c.GetStatus() > -1)
c.SetStatus(-1); c.SetStatus(-1);
info(("Closing socket in CheckBytes, BytesRcv < 0"));
CloseSocketProper(c.GetTCPSock()); CloseSocketProper(c.GetTCPSock());
return false; return false;
} }
@ -450,22 +445,13 @@ std::string TNetwork::TCPRcv(TClient& c) {
do { do {
Temp = recv(c.GetTCPSock(), &Data[BytesRcv], 4 - BytesRcv, 0); Temp = recv(c.GetTCPSock(), &Data[BytesRcv], 4 - BytesRcv, 0);
if (!CheckBytes(c, Temp)) { if (!CheckBytes(c, Temp)) {
#ifdef DEBUG
warn(std::string(__func__) + (": failed on CheckBytes in while(BytesRcv < 4)"));
#endif // DEBUG
return ""; return "";
} }
BytesRcv += Temp; BytesRcv += Temp;
} while (size_t(BytesRcv) < sizeof(Header)); } while (size_t(BytesRcv) < sizeof(Header));
memcpy(&Header, &Data[0], 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)) { if (!CheckBytes(c, BytesRcv)) {
#ifdef DEBUG
warn(std::string(__func__) + (": failed on CheckBytes"));
#endif // DEBUG
return ""; return "";
} }
if (Header < 100 * MB) { if (Header < 100 * MB) {
@ -479,29 +465,15 @@ std::string TNetwork::TCPRcv(TClient& c) {
do { do {
Temp = recv(c.GetTCPSock(), &Data[BytesRcv], Header - BytesRcv, 0); Temp = recv(c.GetTCPSock(), &Data[BytesRcv], Header - BytesRcv, 0);
if (!CheckBytes(c, Temp)) { if (!CheckBytes(c, Temp)) {
#ifdef DEBUG
warn(std::string(__func__) + (": failed on CheckBytes in while(BytesRcv < Header)"));
#endif // DEBUG
return ""; return "";
} }
#ifdef DEBUG
//debug(std::string(__func__) + (": Temp: ") + std::to_string(Temp) + (", BytesRcv: ") + std::to_string(BytesRcv));
#endif // DEBUG
BytesRcv += Temp; BytesRcv += Temp;
} while (BytesRcv < Header); } 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); std::string Ret(Data.data(), Header);
if (Ret.substr(0, 4) == "ABG:") { if (Ret.substr(0, 4) == "ABG:") {
Ret = DeComp(Ret.substr(4)); Ret = DeComp(Ret.substr(4));
} }
#ifdef DEBUG
//debug("Parsing from " + c->GetName() + " -> " +std::to_string(Ret.size()));
#endif
return Ret; return Ret;
} }

View File

@ -90,9 +90,7 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
} }
switch (Code) { switch (Code) {
case 'H': // initial connection case 'H': // initial connection
#ifdef DEBUG trace(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")");
debug(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")");
#endif
if (!Network.SyncClient(Client)) { if (!Network.SyncClient(Client)) {
// TODO handle // TODO handle
} }
@ -114,15 +112,11 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
ParseVehicle(*LockedClient, Packet, Network); ParseVehicle(*LockedClient, Packet, Network);
return; return;
case 'J': case 'J':
#ifdef DEBUG trace(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
debug(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
#endif
Network.SendToAll(LockedClient.get(), Packet, false, true); Network.SendToAll(LockedClient.get(), Packet, false, true);
return; return;
case 'C': case 'C':
#ifdef DEBUG trace(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
debug(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
#endif
if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos) if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos)
break; break;
Res = TriggerLuaEvent("onChatMessage", false, nullptr, std::make_unique<TLuaArg>(TLuaArg { { LockedClient->GetID(), LockedClient->GetName(), Packet.substr(Packet.find(':', 3) + 1) } }), true); Res = TriggerLuaEvent("onChatMessage", false, nullptr, std::make_unique<TLuaArg>(TLuaArg { { LockedClient->GetID(), LockedClient->GetName(), Packet.substr(Packet.find(':', 3) + 1) } }), true);
@ -132,14 +126,13 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
Network.SendToAll(nullptr, Packet, true, true); Network.SendToAll(nullptr, Packet, true, true);
return; return;
case 'E': case 'E':
#ifdef DEBUG trace(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
debug(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
#endif
HandleEvent(*LockedClient, Packet); HandleEvent(*LockedClient, Packet);
return; return;
case 'N': 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); Network.SendToAll(LockedClient.get(), Packet, false, true);
return;
default: default:
return; return;
} }
@ -199,9 +192,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
std::string Data = Packet.substr(3), pid, vid; std::string Data = Packet.substr(3), pid, vid;
switch (Code) { //Spawned Destroyed Switched/Moved NotFound Reset switch (Code) { //Spawned Destroyed Switched/Moved NotFound Reset
case 's': case 's':
#ifdef DEBUG trace(std::string(("got 'Os' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
debug(std::string(("got 'Os' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
#endif
if (Data.at(0) == '0') { if (Data.at(0) == '0') {
int CarID = c.GetOpenCarID(); int CarID = c.GetOpenCarID();
debug(c.GetName() + (" created a car with ID ") + std::to_string(CarID)); 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; return;
case 'c': case 'c':
#ifdef DEBUG trace(std::string(("got 'Oc' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
debug(std::string(("got 'Oc' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
#endif
pid = Data.substr(0, Data.find('-')); pid = Data.substr(0, Data.find('-'));
vid = Data.substr(Data.find('-') + 1, Data.find(':', 1) - Data.find('-') - 1); 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) { 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; return;
case 'd': case 'd':
#ifdef DEBUG trace(std::string(("got 'Od' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
debug(std::string(("got 'Od' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
#endif
pid = Data.substr(0, Data.find('-')); pid = Data.substr(0, Data.find('-'));
vid = Data.substr(Data.find('-') + 1); 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) { 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; return;
case 'r': case 'r':
#ifdef DEBUG trace(std::string(("got 'Or' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
debug(std::string(("got 'Or' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
#endif
Pos = int(Data.find('-')); Pos = int(Data.find('-'));
pid = Data.substr(0, Pos++); pid = Data.substr(0, Pos++);
vid = Data.substr(Pos, Data.find(':') - Pos); vid = Data.substr(Pos, Data.find(':') - Pos);
@ -301,15 +286,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
} }
return; return;
case 't': case 't':
#ifdef DEBUG trace(std::string(("got 'Ot' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
debug(std::string(("got 'Ot' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
#endif
Network.SendToAll(&c, Packet, false, true); Network.SendToAll(&c, Packet, false, true);
return; return;
default: default:
#ifdef DEBUG trace(std::string(("possibly not implemented: '") + Packet + ("' (") + std::to_string(Packet.size()) + (")")));
warn(std::string(("possibly not implemented: '") + Packet + ("' (") + std::to_string(Packet.size()) + (")")));
#endif // DEBUG
return; return;
} }
} }

View File

@ -1,18 +1,14 @@
#include "VehicleData.h" #include "VehicleData.h"
#include <utility>
#include "Common.h" #include "Common.h"
#include <utility>
TVehicleData::TVehicleData(int ID, std::string Data) TVehicleData::TVehicleData(int ID, std::string Data)
: mID(ID) : mID(ID)
, mData(std::move(Data)) { , mData(std::move(Data)) {
#ifdef DEBUG trace("vehicle " + std::to_string(mID) + " constructed");
debug("vehicle " + std::to_string(mID) + " constructed");
#endif
} }
TVehicleData::~TVehicleData() { TVehicleData::~TVehicleData() {
#ifdef DEBUG trace("vehicle " + std::to_string(mID) + " destroyed");
debug("vehicle " + std::to_string(mID) + " destroyed");
#endif
} }

View File

@ -48,9 +48,7 @@ TSentry Sentry { SECRET_SENTRY_URL };
int main(int argc, char** argv) try { int main(int argc, char** argv) try {
#ifdef __unix #ifdef __unix
#if DEBUG trace("registering handlers for SIGINT, SIGTERM, SIGPIPE");
info("registering handlers for SIGINT, SIGTERM, SIGPIPE");
#endif // DEBUG
signal(SIGPIPE, UnixSignalHandler); signal(SIGPIPE, UnixSignalHandler);
signal(SIGTERM, UnixSignalHandler); signal(SIGTERM, UnixSignalHandler);
#ifndef DEBUG #ifndef DEBUG
@ -85,6 +83,8 @@ int main(int argc, char** argv) try {
RegisterThread("Main"); RegisterThread("Main");
trace("Running in debug mode on a debug build");
Sentry.SetupUser(); Sentry.SetupUser();
Sentry.PrintWelcome(); Sentry.PrintWelcome();
TResourceManager ResourceManager; TResourceManager ResourceManager;