From f01756f296ce6ad1f803e233414f0f2ce9bdfb3a Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 4 Dec 2023 15:46:39 +0100 Subject: [PATCH] fix compile errors & remove sentry --- include/Common.h | 4 -- include/CustomAssert.h | 2 - include/TConfig.h | 2 +- include/TLuaEngine.h | 2 +- include/TSentry.h | 38 ----------- src/Common.cpp | 6 -- src/THeartbeatThread.cpp | 18 +---- src/TSentry.cpp | 138 --------------------------------------- src/TServer.cpp | 12 +--- src/main.cpp | 5 -- 10 files changed, 4 insertions(+), 223 deletions(-) delete mode 100644 include/TSentry.h delete mode 100644 src/TSentry.cpp diff --git a/include/Common.h b/include/Common.h index 42c67ca..1ae3c24 100644 --- a/include/Common.h +++ b/include/Common.h @@ -1,8 +1,5 @@ #pragma once -#include "TSentry.h" -extern TSentry Sentry; - #include #include #include @@ -192,7 +189,6 @@ void RegisterThread(const std::string& str); #define beammp_error(x) \ do { \ Application::Console().Write(_this_location + std::string("[ERROR] ") + (x)); \ - Sentry.AddErrorBreadcrumb((x), _file_basename, _line); \ } while (false) #define beammp_lua_error(x) \ do { \ diff --git a/include/CustomAssert.h b/include/CustomAssert.h index df3c4c6..2eeac20 100644 --- a/include/CustomAssert.h +++ b/include/CustomAssert.h @@ -63,12 +63,10 @@ inline void _assert([[maybe_unused]] const char* file, [[maybe_unused]] const ch bool result = (cond); \ if (!result) { \ beammp_errorf("Assertion failed in '{}:{}': {}.", __func__, _line, #cond); \ - Sentry.LogAssert(#cond, _file_basename, _line, __func__); \ } \ } while (false) #define beammp_assert_not_reachable() \ do { \ beammp_errorf("Assertion failed in '{}:{}': Unreachable code reached. This may result in a crash or undefined state of the program.", __func__, _line); \ - Sentry.LogAssert("code is unreachable", _file_basename, _line, __func__); \ } while (false) #endif // DEBUG diff --git a/include/TConfig.h b/include/TConfig.h index d9d6045..634fcb8 100644 --- a/include/TConfig.h +++ b/include/TConfig.h @@ -6,7 +6,7 @@ #include #define TOML11_PRESERVE_COMMENTS_BY_DEFAULT -#include // header-only version of TOML++ +#include // header-only version of TOML++ namespace fs = std::filesystem; diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 21db6d2..36923a2 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include diff --git a/include/TSentry.h b/include/TSentry.h deleted file mode 100644 index 0c68745..0000000 --- a/include/TSentry.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef SENTRY_H -#define SENTRY_H - -#include -#include -#include -enum class SentryLevel { - Debug = -1, - Info = 0, - Warning = 1, - Error = 2, - Fatal = 3, -}; - -// singleton, dont make this twice -class TSentry final { -public: - TSentry(); - ~TSentry(); - - void PrintWelcome(); - void SetupUser(); - void Log(SentryLevel level, const std::string& logger, const std::string& text); - void LogError(const std::string& text, const std::string& file, const std::string& line); - void SetContext(const std::string& context_name, const std::unordered_map& map); - void LogException(const std::exception& e, const std::string& file, const std::string& line); - void LogAssert(const std::string& condition_string, const std::string& file, const std::string& line, const std::string& function); - void AddErrorBreadcrumb(const std::string& msg, const std::string& file, const std::string& line); - // cleared when Logged - void SetTransaction(const std::string& id); - [[nodiscard]] std::unique_lock CreateExclusiveContext(); - -private: - bool mValid { true }; - std::mutex mMutex; -}; - -#endif // SENTRY_H diff --git a/src/Common.cpp b/src/Common.cpp index 5b153ca..af345ad 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -13,9 +13,6 @@ #include "CustomAssert.h" #include "Http.h" -// global, yes, this is ugly, no, it cant be done another way -TSentry Sentry {}; - Application::TSettings Application::Settings = {}; void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) { @@ -217,9 +214,6 @@ void Application::CheckForUpdates() { if (FirstTime) { beammp_debug("Failed to fetch version from: " + url); beammp_trace("got " + Response); - auto Lock = Sentry.CreateExclusiveContext(); - Sentry.SetContext("get-response", { { "response", Response } }); - Sentry.LogError("failed to get server version", _file_basename, _line); Application::SetSubsystemStatus("UpdateCheck", Application::Status::Bad); } } diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 873825a..2f496c1 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -40,16 +40,6 @@ void THeartbeatThread::operator()() { Body += "&ip=" + Application::Settings.CustomIP; } - auto SentryReportError = [&](const std::string& transaction, int status) { - auto Lock = Sentry.CreateExclusiveContext(); - Sentry.SetContext("heartbeat", - { { "response-body", T }, - { "request-body", Body } }); - Sentry.SetTransaction(transaction); - beammp_trace("sending log to sentry: " + std::to_string(status) + " for " + transaction); - Sentry.Log(SentryLevel::Error, "default", Http::Status::ToString(status) + " (" + std::to_string(status) + ")"); - }; - auto Target = "/heartbeat"; unsigned int ResponseCode = 0; @@ -63,10 +53,8 @@ void THeartbeatThread::operator()() { beammp_trace("Backend response failed to parse as valid json"); beammp_trace("Response was: `" + T + "`"); } - Sentry.SetContext("JSON Response", { { "reponse", T } }); - SentryReportError(Url + Target, ResponseCode); } else if (ResponseCode != 200) { - SentryReportError(Url + Target, ResponseCode); + beammp_errorf("Response code from the heartbeat: {}", ResponseCode); } else { // all ok Ok = true; @@ -85,24 +73,20 @@ void THeartbeatThread::operator()() { if (Doc.HasMember(StatusKey) && Doc[StatusKey].IsString()) { Status = Doc[StatusKey].GetString(); } else { - Sentry.SetContext("JSON Response", { { StatusKey, "invalid string / missing" } }); Ok = false; } if (Doc.HasMember(CodeKey) && Doc[CodeKey].IsString()) { Code = Doc[CodeKey].GetString(); } else { - Sentry.SetContext("JSON Response", { { CodeKey, "invalid string / missing" } }); Ok = false; } if (Doc.HasMember(MessageKey) && Doc[MessageKey].IsString()) { Message = Doc[MessageKey].GetString(); } else { - Sentry.SetContext("JSON Response", { { MessageKey, "invalid string / missing" } }); Ok = false; } if (!Ok) { beammp_error("Missing/invalid json members in backend response"); - Sentry.LogError("Missing/invalid json members in backend response", __FILE__, std::to_string(__LINE__)); } } else { if (!Application::Settings.Private) { diff --git a/src/TSentry.cpp b/src/TSentry.cpp deleted file mode 100644 index ceab947..0000000 --- a/src/TSentry.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include "TSentry.h" -#include "Common.h" - -#include -#include -#include - -TSentry::TSentry() { - if (std::strlen(S_DSN) == /* DISABLES CODE */ (0)) { - mValid = false; - } else { - mValid = true; - sentry_options_t* options = sentry_options_new(); - sentry_options_set_dsn(options, S_DSN); - auto ReleaseString = "BeamMP-Server@" + Application::ServerVersionString(); - sentry_options_set_symbolize_stacktraces(options, true); - sentry_options_set_release(options, ReleaseString.c_str()); - sentry_options_set_max_breadcrumbs(options, 10); - sentry_init(options); - } -} - -TSentry::~TSentry() { - if (mValid) { - sentry_close(); - } -} - -void TSentry::PrintWelcome() { - if (mValid) { - if (!Application::Settings.SendErrors) { - mValid = false; - if (Application::Settings.SendErrorsMessageEnabled) { - beammp_info("Opted out of error reporting (SendErrors), Sentry disabled."); - } else { - beammp_info("Sentry disabled"); - } - } else { - if (Application::Settings.SendErrorsMessageEnabled) { - beammp_info("Sentry started! Reporting errors automatically. This sends data to the developers in case of errors and crashes. You can learn more, turn this message off or opt-out of this in the ServerConfig.toml."); - } else { - beammp_info("Sentry started"); - } - } - } else { - if (Application::Settings.SendErrorsMessageEnabled) { - beammp_info("Sentry disabled in unofficial build. Automatic error reporting disabled."); - } else { - beammp_info("Sentry disabled in unofficial build"); - } - } -} - -void TSentry::SetupUser() { - if (!mValid) { - return; - } - Application::SetSubsystemStatus("Sentry", Application::Status::Good); - sentry_value_t user = sentry_value_new_object(); - if (Application::Settings.Key.size() == 36) { - sentry_value_set_by_key(user, "id", sentry_value_new_string(Application::Settings.Key.c_str())); - } else { - sentry_value_set_by_key(user, "id", sentry_value_new_string("unauthenticated")); - } - sentry_set_user(user); -} - -void TSentry::Log(SentryLevel level, const std::string& logger, const std::string& text) { - if (!mValid) { - return; - } - SetContext("threads", { { "thread-name", ThreadName(true) } }); - auto Msg = sentry_value_new_message_event(sentry_level_t(level), logger.c_str(), text.c_str()); - sentry_capture_event(Msg); - sentry_set_transaction(nullptr); -} - -void TSentry::LogError(const std::string& text, const std::string& file, const std::string& line) { - if (!mValid) { - return; - } - SetTransaction(file + ":" + line); - Log(SentryLevel::Error, "default", file + ": " + text); -} - -void TSentry::SetContext(const std::string& context_name, const std::unordered_map& map) { - if (!mValid) { - return; - } - auto ctx = sentry_value_new_object(); - for (const auto& pair : map) { - std::string key = pair.first; - if (key == "type") { - // `type` is reserved - key = "_type"; - } - sentry_value_set_by_key(ctx, key.c_str(), sentry_value_new_string(pair.second.c_str())); - } - sentry_set_context(context_name.c_str(), ctx); -} - -void TSentry::LogException(const std::exception& e, const std::string& file, const std::string& line) { - if (!mValid) { - return; - } - SetTransaction(file + ":" + line); - Log(SentryLevel::Fatal, "exceptions", std::string(e.what()) + " @ " + file + ":" + line); -} - -void TSentry::LogAssert(const std::string& condition_string, const std::string& file, const std::string& line, const std::string& function) { - if (!mValid) { - return; - } - SetTransaction(file + ":" + line + ":" + function); - std::stringstream ss; - ss << "\"" << condition_string << "\" failed @ " << file << ":" << line; - Log(SentryLevel::Fatal, "asserts", ss.str()); -} - -void TSentry::AddErrorBreadcrumb(const std::string& msg, const std::string& file, const std::string& line) { - if (!mValid) { - return; - } - auto crumb = sentry_value_new_breadcrumb("default", (msg + " @ " + file + ":" + line).c_str()); - sentry_value_set_by_key(crumb, "level", sentry_value_new_string("error")); - sentry_add_breadcrumb(crumb); -} - -void TSentry::SetTransaction(const std::string& id) { - if (!mValid) { - return; - } - sentry_set_transaction(id.c_str()); -} - -std::unique_lock TSentry::CreateExclusiveContext() { - return std::unique_lock(mMutex); -} diff --git a/src/TServer.cpp b/src/TServer.cpp index e1f115e..9a64189 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -382,23 +382,13 @@ void TServer::Apply(TClient& c, int VID, const std::string& pckt) { std::string VD = c.GetCarData(VID); if (VD.empty()) { beammp_error("Tried to apply change to vehicle that does not exist"); - auto Lock = Sentry.CreateExclusiveContext(); - Sentry.SetContext("vehicle-change", - { { "packet", Packet }, - { "vehicle-id", std::to_string(VID) }, - { "client-car-count", std::to_string(c.GetCarCount()) } }); - Sentry.LogError("attempt to apply change to nonexistent vehicle", _file_basename, _line); return; } std::string Header = VD.substr(0, VD.find('{')); FoundPos = VD.find('{'); if (FoundPos == std::string::npos) { - auto Lock = Sentry.CreateExclusiveContext(); - Sentry.SetContext("vehicle-change-packet", - { { "packet", VD } }); - Sentry.LogError("malformed packet", _file_basename, _line); - return; + return; } VD = VD.substr(FoundPos); rapidjson::Document Veh, Pack; diff --git a/src/main.cpp b/src/main.cpp index 54f0f62..4a027f1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,3 @@ -#include "TSentry.h" - #include "ArgsParser.h" #include "Common.h" #include "Http.h" @@ -64,7 +62,6 @@ int main(int argc, char** argv) { } catch (const std::exception& e) { beammp_error("A fatal exception has occurred and the server is forcefully shutting down."); beammp_error(e.what()); - Sentry.LogException(e, _file_basename, _line); MainRet = -1; } std::exit(MainRet); @@ -148,8 +145,6 @@ int BeamMPServerMain(MainArguments Arguments) { RegisterThread("Main"); beammp_trace("Running in debug mode on a debug build"); - Sentry.SetupUser(); - Sentry.PrintWelcome(); TResourceManager ResourceManager; TPPSMonitor PPSMonitor(Server); THeartbeatThread Heartbeat(ResourceManager, Server);