From ff3cbebac085c531c89a64eaa3b90ac346ce6aba Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 12 Aug 2021 01:24:48 +0200 Subject: [PATCH] Sentry: more macro replacements --- include/Compat.h | 1 - include/TSentry.h | 14 ++++++++------ src/THeartbeatThread.cpp | 2 +- src/TNetwork.cpp | 4 ++-- src/TSentry.cpp | 25 +++++++++++++++++-------- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/include/Compat.h b/include/Compat.h index 27430bf..dfd6262 100644 --- a/include/Compat.h +++ b/include/Compat.h @@ -21,7 +21,6 @@ inline void CloseSocketProper(int TheSocket) { // ======================= WIN32 ======================= #ifdef WIN32 -#include "TSentry.h" inline void CloseSocketProper(SOCKET TheSocket) { shutdown(TheSocket, 2); // 2 == SD_BOTH closesocket(TheSocket); diff --git a/include/TSentry.h b/include/TSentry.h index 36593ba..0db5885 100644 --- a/include/TSentry.h +++ b/include/TSentry.h @@ -1,13 +1,16 @@ #ifndef SENTRY_H #define SENTRY_H -#include - #include #include #include - -// TODO possibly use attach_stacktrace +enum class SentryLevel { + Debug = -1, + Info = 0, + Warning = 1, + Error = 2, + Fatal = 3, +}; // singleton, dont make this twice class TSentry final { @@ -17,7 +20,7 @@ public: void PrintWelcome(); void SetupUser(); - void Log(sentry_level_t level, const std::string& logger, const std::string& text); + 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); @@ -30,7 +33,6 @@ public: private: bool mValid { true }; std::mutex mMutex; - sentry_value_t mContext; }; #endif // SENTRY_H diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 3c66a08..96ce49c 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -47,7 +47,7 @@ void THeartbeatThread::operator()() { { { "response-body", T }, { "request-body", Body } }); Sentry.SetTransaction(transaction); - Sentry.Log(SENTRY_LEVEL_ERROR, "default", "unexpected backend response (" + std::to_string(status) + ")"); + Sentry.Log(SentryLevel::Error, "default", "unexpected backend response (" + std::to_string(status) + ")"); } }; SentryReportError(Application::GetBackendHostname() + Target, ResponseCode); diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index 02a5a4d..c711bc4 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -303,7 +303,7 @@ void TNetwork::Authentication(SOCKET TCPSock) { { { "response-body", Rc }, { "key", RequestString } }); Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); - Sentry.Log(SENTRY_LEVEL_ERROR, "default", "unexpected backend response (" + std::to_string(ResponseCode) + ")"); + Sentry.Log(SentryLevel::Error, "default", "unexpected backend response (" + std::to_string(ResponseCode) + ")"); return; } else if (Rc == "0") { auto Lock = Sentry.CreateExclusiveContext(); @@ -311,7 +311,7 @@ void TNetwork::Authentication(SOCKET TCPSock) { { { "response-body", Rc }, { "key", RequestString } }); Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); - Sentry.Log(SENTRY_LEVEL_INFO, "default", "backend returned 0 instead of json (" + std::to_string(ResponseCode) + ")"); + Sentry.Log(SentryLevel::Info, "default", "backend returned 0 instead of json (" + std::to_string(ResponseCode) + ")"); } if (AuthResponse["username"].IsString() && AuthResponse["roles"].IsString() diff --git a/src/TSentry.cpp b/src/TSentry.cpp index dc84b09..2ea874b 100644 --- a/src/TSentry.cpp +++ b/src/TSentry.cpp @@ -1,6 +1,9 @@ #include "TSentry.h" #include "Common.h" +#include +#include + TSentry::TSentry(const std::string& SentryUrl) { if (SentryUrl.empty()) { mValid = false; @@ -30,40 +33,46 @@ void TSentry::PrintWelcome() { } void TSentry::SetupUser() { + if (!mValid) { + return; + } sentry_value_t user = sentry_value_new_object(); sentry_value_set_by_key(user, "id", sentry_value_new_string(Application::Settings.Key.c_str())); sentry_set_user(user); } -void TSentry::Log(sentry_level_t level, const std::string& logger, const std::string& text) { +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(level, logger.c_str(), text.c_str()); + auto Msg = sentry_value_new_message_event(sentry_level_t(level), logger.c_str(), text.c_str()); sentry_capture_event(Msg); sentry_remove_transaction(); } void TSentry::LogError(const std::string& text, const std::string& file, const std::string& line) { + if (!mValid) { + return; + } SetTransaction(file + ":" + line); - Log(SENTRY_LEVEL_ERROR, "default", file + ": " + text); + Log(SentryLevel::Error, "default", file + ": " + text); } void TSentry::SetContext(const std::string& context_name, const std::unordered_map& map) { if (!mValid) { return; } - mContext = sentry_value_new_object(); + 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(mContext, key.c_str(), sentry_value_new_string(pair.second.c_str())); + sentry_value_set_by_key(ctx, key.c_str(), sentry_value_new_string(pair.second.c_str())); } - sentry_set_context(context_name.c_str(), mContext); + sentry_set_context(context_name.c_str(), ctx); } void TSentry::LogException(const std::exception& e, const std::string& file, const std::string& line) { @@ -71,7 +80,7 @@ void TSentry::LogException(const std::exception& e, const std::string& file, con return; } SetTransaction(file + ":" + line); - Log(SENTRY_LEVEL_FATAL, "exceptions", std::string(e.what()) + " @ " + 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) { @@ -81,7 +90,7 @@ void TSentry::LogAssert(const std::string& condition_string, const std::string& SetTransaction(file + ":" + line + ":" + function); std::stringstream ss; ss << "\"" << condition_string << "\" failed @ " << file << ":" << line; - Log(SENTRY_LEVEL_FATAL, "asserts", ss.str()); + Log(SentryLevel::Fatal, "asserts", ss.str()); } void TSentry::AddErrorBreadcrumb(const std::string& msg, const std::string& file, const std::string& line) {