From 2f60a5927b409a7ec6714ae6b51be4988f4c4ec8 Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Fri, 15 Dec 2023 17:19:17 +0100 Subject: [PATCH] Refactor sentry environment handling and logging level The environment variable checking for enabling sentry has been changed to use spdlog's OS GETENV method, reducing the code complexity. Introduced fetching of "CURAENGINE_SENTRY_USER" to set it as a user in Sentry. Also improved visibility by utilizing spdlog's TO_STRING_VIEW method in sentry_sink's logging level. Contribute to CURA-11482 --- include/utils/sentry_sink.h | 2 +- src/main.cpp | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/utils/sentry_sink.h b/include/utils/sentry_sink.h index 1a283c9e4b..9757eee5a7 100644 --- a/include/utils/sentry_sink.h +++ b/include/utils/sentry_sink.h @@ -17,7 +17,7 @@ class SentryBreadcrumbSink : public spdlog::sinks::base_sink { sentry_value_t crumb = sentry_value_new_breadcrumb("debug", msg.payload.data()); sentry_value_set_by_key(crumb, "category", sentry_value_new_string(msg.logger_name.data())); - sentry_value_set_by_key(crumb, "level", sentry_value_new_string(spdlog::level::to_short_c_str(msg.level))); + sentry_value_set_by_key(crumb, "level", sentry_value_new_string(spdlog::level::to_string_view(msg.level).data())); auto duration = msg.time.time_since_epoch(); auto timestamp = std::chrono::duration_cast(duration).count(); diff --git a/src/main.cpp b/src/main.cpp index b372ee39a2..4f04690850 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -53,15 +54,7 @@ int main(int argc, char** argv) // Want to set the sentry URL? Use '-c user.curaengine:sentry_url= -o curaengine:enable_sentry=True' with conan install #ifdef SENTRY_URL - bool use_sentry = true; - if (const char* use_sentry_env = std::getenv("use_sentry")) - { - if (std::strcmp(use_sentry_env, "0") == 0) - { - use_sentry = false; - } - } - if (use_sentry) + if (const auto use_sentry = spdlog::details::os::getenv("use_sentry"); ! use_sentry.empty() && use_sentry == "1") { // Setup sentry error handling. sentry_options_t* options = sentry_options_new(); @@ -99,13 +92,20 @@ int main(int argc, char** argv) ? "" : fmt::format("-{}.{}", version.prerelease_type == semver::prerelease::alpha ? "alpha" : "beta", version.prerelease_number); sentry_set_tag("cura.version", fmt::format("{}.{}.{}{}", version.major, version.minor, version.patch, prerelease).c_str()); + + if (const auto sentry_user = spdlog::details::os::getenv("CURAENGINE_SENTRY_USER"); ! sentry_user.empty()) + { + sentry_value_t user = sentry_value_new_object(); + sentry_value_set_by_key(user, "email", sentry_value_new_string(sentry_user.c_str())); + sentry_set_user(user); + } } #endif cura::Application::getInstance().run(argc, argv); #ifdef SENTRY_URL - if (use_sentry) + if (const auto use_sentry = spdlog::details::os::getenv("use_sentry"); ! use_sentry.empty() && use_sentry == "1") { sentry_close(); }