Skip to content

Commit

Permalink
Refactor sentry environment handling and logging level
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jellespijker committed Dec 15, 2023
1 parent 3ad538a commit 2f60a59
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/utils/sentry_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SentryBreadcrumbSink : public spdlog::sinks::base_sink<Mutex>
{
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<std::chrono::seconds>(duration).count();
Expand Down
20 changes: 10 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <filesystem>
#include <semver.hpp>
#include <sentry.h>
#include <spdlog/details/os.h>
#include <string>

#include <range/v3/algorithm/contains.hpp>
Expand Down Expand Up @@ -53,15 +54,7 @@ int main(int argc, char** argv)

// Want to set the sentry URL? Use '-c user.curaengine:sentry_url=<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();
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 2f60a59

Please sign in to comment.