From 3bd67d959fb27ec325a47325ec718e035cc066ba Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 10 Nov 2022 19:13:43 +0100 Subject: [PATCH] handle all exceptions which happen inside a logging macro and print them ;) --- include/Common.h | 105 ++++++++++++++++++++++++++++++++++------------- src/TServer.cpp | 5 ++- src/main.cpp | 2 +- 3 files changed, 80 insertions(+), 32 deletions(-) diff --git a/include/Common.h b/include/Common.h index 9a6be61..dfc0861 100644 --- a/include/Common.h +++ b/include/Common.h @@ -150,7 +150,7 @@ public: // Keeps track of how many packets we dropped on UDP due to fundamentally being malformed static inline std::atomic_size_t MalformedUdpPackets { 0 }; - // Keeps track of how many packets we dropped on UDP due to + // Keeps track of how many packets we dropped on UDP due to // 1) not having a valid (known) player id // 2) player disconnecting // 3) packet failing to parse @@ -219,41 +219,88 @@ void RegisterThread(const std::string& str); #endif // defined(DEBUG) - #define beammp_warn(x) Application::Console().Write(_this_location + std::string("[WARN] ") + (x)) - #define beammp_info(x) Application::Console().Write(_this_location + std::string("[INFO] ") + (x)) - #define beammp_error(x) \ - do { \ - Application::Console().Write(_this_location + std::string("[ERROR] ") + (x)); \ - Sentry.AddErrorBreadcrumb((x), _file_basename, _line); \ + #define beammp_internal_error(x) Application::Console().Write(_this_location + std::string("[INTERNAL ERROR] ") + (x)) + + #define beammp_warn(x) \ + do { \ + try{ \ + Application::Console().Write(_this_location + std::string("[WARN] ") + (x)); \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\ + } \ } while (false) - #define beammp_lua_error(x) \ - do { \ - Application::Console().Write(_this_location + std::string("[LUA ERROR] ") + (x)); \ + #define beammp_info(x) \ + do { \ + try{ \ + Application::Console().Write(_this_location + std::string("[INFO] ") + (x)); \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\ + } \ } while (false) - #define beammp_lua_warn(x) \ - do { \ - Application::Console().Write(_this_location + std::string("[LUA WARN] ") + (x)); \ + #define beammp_error(x) \ + do { \ + try{ \ + Application::Console().Write(_this_location + std::string("[ERROR] ") + (x)); \ + Sentry.AddErrorBreadcrumb((x), _file_basename, _line); \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\ + } \ } while (false) - #define luaprint(x) Application::Console().Write(_this_location + std::string("[LUA] ") + (x)) - #define beammp_debug(x) \ - do { \ - if (Application::GetSettingBool("Debug")) { \ - Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \ - } \ + #define beammp_lua_error(x) \ + do { \ + try{ \ + Application::Console().Write(_this_location + std::string("[LUA ERROR] ") + (x)); \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\ + } \ } while (false) - #define beammp_event(x) \ - do { \ - if (Application::GetSettingBool("Debug")) { \ - Application::Console().Write(_this_location + std::string("[EVENT] ") + (x)); \ - } \ + #define beammp_lua_warn(x) \ + do { \ + try{ \ + Application::Console().Write(_this_location + std::string("[LUA WARN] ") + (x)); \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\ + } \ + } while (false) + #define luaprint(x) \ + do { \ + try{ \ + Application::Console().Write(_this_location + std::string("[LUA] ") + (x)); \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\ + } \ + } while (false) + #define beammp_debug(x) \ + do { \ + try{ \ + if (Application::GetSettingBool("Debug")) { \ + Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \ + } \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\ + } \ + } while (false) + #define beammp_event(x) \ + do { \ + try{ \ + if (Application::GetSettingBool("Debug")) { \ + Application::Console().Write(_this_location + std::string("[EVENT] ") + (x)); \ + } \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\ + } \ } while (false) // trace() is a debug-build debug() #if defined(DEBUG) - #define beammp_trace(x) \ - do { \ - if (Application::GetSettingBool("Debug")) { \ - Application::Console().Write(_this_location + std::string("[TRACE] ") + (x)); \ - } \ + #define beammp_trace(x) \ + do { \ + try{ \ + if (Application::GetSettingBool("Debug")) { \ + Application::Console().Write(_this_location + std::string("[TRACE] ") + (x)); \ + } \ + } catch (const std::exception& e) { \ + beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}'", #x)); \ + } \ } while (false) #else #define beammp_trace(x) diff --git a/src/TServer.cpp b/src/TServer.cpp index 3106de6..badd95d 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -57,11 +57,11 @@ static std::optional ParseVehiclePacket(const std::string& Packet auto NameDataSep = withoutCode.find(':', 3); if (NameDataSep == std::string::npos) { - // invalid packet + beammp_debugf("Invalid packet from {} in ParseVehiclePacket: No ':' separator, assuming corrupted packet. Packet: '{}'", playerID, Packet); return std::nullopt; } if (NameDataSep + 1 > withoutCode.size()) { - // invalid packet + beammp_debugf("Invalid packet from {} in ParseVehiclePacket: Separator in unexpected place, assuming corrupted packet. Packet: '{}'", playerID, Packet); return std::nullopt; } @@ -79,6 +79,7 @@ static std::optional ParseVehiclePacket(const std::string& Packet return { { Data, PID, VID } }; // std::vector(Data.begin(), Data.end()) } } + beammp_debugf("Failed to parse packet from player {}", playerID); return std::nullopt; } diff --git a/src/main.cpp b/src/main.cpp index bd6c55a..65b4fd6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -111,7 +111,7 @@ int BeamMPServerMain(MainArguments Arguments) { } } } - + Application::Console().Internal().set_prompt("> "); Application::SetSubsystemStatus("Main", Application::Status::Starting);