Sentry: more macro replacements

This commit is contained in:
Lion Kortlepel 2021-08-12 01:24:48 +02:00 committed by Lion
parent 0f9a994c10
commit ff3cbebac0
5 changed files with 28 additions and 18 deletions

View File

@ -21,7 +21,6 @@ inline void CloseSocketProper(int TheSocket) {
// ======================= WIN32 ======================= // ======================= WIN32 =======================
#ifdef WIN32 #ifdef WIN32
#include "TSentry.h"
inline void CloseSocketProper(SOCKET TheSocket) { inline void CloseSocketProper(SOCKET TheSocket) {
shutdown(TheSocket, 2); // 2 == SD_BOTH shutdown(TheSocket, 2); // 2 == SD_BOTH
closesocket(TheSocket); closesocket(TheSocket);

View File

@ -1,13 +1,16 @@
#ifndef SENTRY_H #ifndef SENTRY_H
#define SENTRY_H #define SENTRY_H
#include <sentry.h>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
enum class SentryLevel {
// TODO possibly use attach_stacktrace Debug = -1,
Info = 0,
Warning = 1,
Error = 2,
Fatal = 3,
};
// singleton, dont make this twice // singleton, dont make this twice
class TSentry final { class TSentry final {
@ -17,7 +20,7 @@ public:
void PrintWelcome(); void PrintWelcome();
void SetupUser(); 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 LogError(const std::string& text, const std::string& file, const std::string& line);
void SetContext(const std::string& context_name, const std::unordered_map<std::string, std::string>& map); void SetContext(const std::string& context_name, const std::unordered_map<std::string, std::string>& map);
void LogException(const std::exception& e, const std::string& file, const std::string& line); void LogException(const std::exception& e, const std::string& file, const std::string& line);
@ -30,7 +33,6 @@ public:
private: private:
bool mValid { true }; bool mValid { true };
std::mutex mMutex; std::mutex mMutex;
sentry_value_t mContext;
}; };
#endif // SENTRY_H #endif // SENTRY_H

View File

@ -47,7 +47,7 @@ void THeartbeatThread::operator()() {
{ { "response-body", T }, { { "response-body", T },
{ "request-body", Body } }); { "request-body", Body } });
Sentry.SetTransaction(transaction); 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); SentryReportError(Application::GetBackendHostname() + Target, ResponseCode);

View File

@ -303,7 +303,7 @@ void TNetwork::Authentication(SOCKET TCPSock) {
{ { "response-body", Rc }, { { "response-body", Rc },
{ "key", RequestString } }); { "key", RequestString } });
Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); 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; return;
} else if (Rc == "0") { } else if (Rc == "0") {
auto Lock = Sentry.CreateExclusiveContext(); auto Lock = Sentry.CreateExclusiveContext();
@ -311,7 +311,7 @@ void TNetwork::Authentication(SOCKET TCPSock) {
{ { "response-body", Rc }, { { "response-body", Rc },
{ "key", RequestString } }); { "key", RequestString } });
Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); 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() if (AuthResponse["username"].IsString() && AuthResponse["roles"].IsString()

View File

@ -1,6 +1,9 @@
#include "TSentry.h" #include "TSentry.h"
#include "Common.h" #include "Common.h"
#include <sentry.h>
#include <sstream>
TSentry::TSentry(const std::string& SentryUrl) { TSentry::TSentry(const std::string& SentryUrl) {
if (SentryUrl.empty()) { if (SentryUrl.empty()) {
mValid = false; mValid = false;
@ -30,40 +33,46 @@ void TSentry::PrintWelcome() {
} }
void TSentry::SetupUser() { void TSentry::SetupUser() {
if (!mValid) {
return;
}
sentry_value_t user = sentry_value_new_object(); 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_value_set_by_key(user, "id", sentry_value_new_string(Application::Settings.Key.c_str()));
sentry_set_user(user); 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) { if (!mValid) {
return; return;
} }
SetContext("threads", { { "thread-name", ThreadName(true) } }); 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_capture_event(Msg);
sentry_remove_transaction(); sentry_remove_transaction();
} }
void TSentry::LogError(const std::string& text, const std::string& file, const std::string& line) { void TSentry::LogError(const std::string& text, const std::string& file, const std::string& line) {
if (!mValid) {
return;
}
SetTransaction(file + ":" + line); 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<std::string, std::string>& map) { void TSentry::SetContext(const std::string& context_name, const std::unordered_map<std::string, std::string>& map) {
if (!mValid) { if (!mValid) {
return; return;
} }
mContext = sentry_value_new_object(); auto ctx = sentry_value_new_object();
for (const auto& pair : map) { for (const auto& pair : map) {
std::string key = pair.first; std::string key = pair.first;
if (key == "type") { if (key == "type") {
// `type` is reserved // `type` is reserved
key = "_type"; 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) { 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; return;
} }
SetTransaction(file + ":" + line); 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) { 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); SetTransaction(file + ":" + line + ":" + function);
std::stringstream ss; std::stringstream ss;
ss << "\"" << condition_string << "\" failed @ " << file << ":" << line; 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) { void TSentry::AddErrorBreadcrumb(const std::string& msg, const std::string& file, const std::string& line) {