From 6ce51f1a1d6fee1a3e35a174c09b9acc648ebbff Mon Sep 17 00:00:00 2001 From: mehulmathur001 <160306774+mehulmathur001@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:27:42 +0000 Subject: [PATCH] refactor: driving sentry from env variables --- .github/workflows/build-mudlet.yml | 1 - CMakeLists.txt | 24 +++-------- src/CMakeLists.txt | 10 +++-- src/main.cpp | 68 +++++++++++++----------------- src/mudlet.pro | 9 ++++ 5 files changed, 52 insertions(+), 60 deletions(-) diff --git a/.github/workflows/build-mudlet.yml b/.github/workflows/build-mudlet.yml index 6c0a2d3d299..f656cd444d7 100644 --- a/.github/workflows/build-mudlet.yml +++ b/.github/workflows/build-mudlet.yml @@ -49,7 +49,6 @@ jobs: env: BOOST_ROOT: ${{github.workspace}}/3rdparty/boost BOOST_URL: https://sourceforge.net/projects/boost/files/boost/1.83.0/boost_1_83_0.tar.bz2/download - ENABLE_SENTRY: false SENTRY_ORG: mehul-mathur SENTRY_PROJECT: native-qt-mac SENTRY_AUTH_TOKEN: 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 diff --git a/CMakeLists.txt b/CMakeLists.txt index fecec482abb..8513be420a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,20 +102,6 @@ else() message(STATUS "Git SHA1 used: ${GIT_SHA1}") endif() -# This is used for enabling / disabling sentry QT -if(DEFINED ENV{ENABLE_SENTRY}) - if($ENV{ENABLE_SENTRY} STREQUAL "true") - set(ENABLE_SENTRY_FLAG TRUE) - message(STATUS "Sentry integration is enabled") - else() - set(ENABLE_SENTRY_FLAG FALSE) - message(STATUS "Sentry integration is disabled") - endif() -else() - set(ENABLE_SENTRY_FLAG FALSE) - message(STATUS "ENABLE_SENTRY environment variable is not defined") -endif() - # APP_BUILD should only be empty/null in official "release" builds, developers # may like to set the MUDLET_VERSION_BUILD environment variable to their user # and branch names to make it easier to tell different builds apart! @@ -189,6 +175,10 @@ include_optional_module(ENVIRONMENT_VARIABLE WITH_MAIN_BUILD_SYSTEM include_optional_module(ENVIRONMENT_VARIABLE WITH_QT6 OPTION_VARIABLE USE_QT6 READABLE_NAME "build with Qt6 (if installed)") +include_optional_module(ENVIRONMENT_VARIABLE WITH_SENTRY + OPTION_VARIABLE USE_SENTRY + READABLE_NAME "sentry integration" + SUPPORTED_SYSTEMS "Darwin") if(USE_QT6) find_package( @@ -216,7 +206,7 @@ git_submodule_init( CHECK_FILE "3rdparty/edbee-lib/CMakeLists.txt" SUBMODULE_PATH "3rdparty/edbee-lib" READABLE_NAME "edbee-lib editor widget") -if(APPLE AND ENABLE_SENTRY_FLAG) +if(APPLE AND USE_SENTRY) git_submodule_init( CHECK_FILE "3rdparty/sentry-native/CMakeLists.txt" SUBMODULE_PATH "3rdparty/sentry-native" READABLE_NAME "sentry-native") @@ -275,14 +265,14 @@ if(CCACHE_FOUND) set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) endif(CCACHE_FOUND) -if(APPLE AND ENABLE_SENTRY_FLAG) +if(APPLE AND USE_SENTRY) set(SENTRY_BACKEND "crashpad") set(SENTRY_INTEGRATION_QT "ON") set(SENTRY_INCLUDE_DIR "3rdparty/sentry-native/include") set(SENTRY_LIBRARIES "3rdparty/sentry-native") endif() -if(APPLE AND ENABLE_SENTRY_FLAG) +if(APPLE AND USE_SENTRY) add_subdirectory(3rdparty/sentry-native) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7b0e650056f..43323c27724 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -446,7 +446,7 @@ endif() message(STATUS "Using ${CMAKE_CXX_COMPILER_ID} compiler") -if(APPLE AND ${ENABLE_SENTRY_FLAG}) +if(APPLE AND USE_SENTRY) find_package(sentry REQUIRED) endif() @@ -513,6 +513,10 @@ if(USE_FONTS) target_compile_definitions(mudlet PRIVATE INCLUDE_FONTS) endif() +if(USE_SENTRY) + target_compile_definitions(mudlet PRIVATE INCLUDE_SENTRY) +endif() + if(UNIX) set(LUA_DEFAULT_DIR "${CMAKE_INSTALL_PREFIX}/share/mudlet/lua") endif(UNIX) @@ -561,7 +565,7 @@ if(USE_UPDATER) target_compile_definitions(mudlet PRIVATE INCLUDE_UPDATER) endif(USE_UPDATER) -if(APPLE AND ${ENABLE_SENTRY_FLAG}) +if(APPLE AND USE_SENTRY) target_link_libraries(mudlet sentry::sentry) endif() @@ -694,7 +698,7 @@ if(UNIX AND NOT APPLE) WORLD_EXECUTE) endif() -if(APPLE AND ${ENABLE_SENTRY_FLAG}) +if(APPLE AND USE_SENTRY) message(STATUS "mudlet_SOURCE_DIR : ${mudlet_SOURCE_DIR}") message(STATUS "mudlet_BINARY_DIR : ${mudlet_BINARY_DIR}") diff --git a/src/main.cpp b/src/main.cpp index 1e7f626db05..3ab9d86472b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,7 +46,7 @@ #include "Announcer.h" #include "FileOpenHandler.h" -#if defined(Q_OS_MACOS) +#if defined(Q_OS_MACOS) && defined(INCLUDE_SENTRY) extern "C" { #include "sentry.h" } @@ -132,12 +132,6 @@ void removeOldNoteColorEmojiFonts() #endif // defined(Q_OS_LINUX) #endif // defined(INCLUDE_FONTS) -bool isSentryEnabled() { - QProcessEnvironment systemEnvironment = QProcessEnvironment::systemEnvironment(); - QString enableSentry = systemEnvironment.value(qsl("ENABLE_SENTRY"), QString()); - return (enableSentry.toLower() == "true"); -} - QTranslator* loadTranslationsForCommandLine() { const QSettings settings_new(QLatin1String("mudlet"), QLatin1String("Mudlet")); @@ -164,34 +158,32 @@ QTranslator* loadTranslationsForCommandLine() } void initSentry() { -#ifdef Q_OS_MACOS - if (isSentryEnabled()) { - QProcessEnvironment systemEnvironment = QProcessEnvironment::systemEnvironment(); - QString sentryDsn = systemEnvironment.value(qsl("SENTRY_DSN"), QString()); - const char* sentryDsnCStr = sentryDsn.toUtf8().constData(); - - sentry_options_t *options = sentry_options_new(); - sentry_options_set_dsn(options, sentryDsnCStr); - sentry_options_set_handler_path(options, "./crashpad_handler"); - sentry_options_set_debug(options, 1); - sentry_init(options); - - sentry_value_t mudlet_info = sentry_value_new_object(); - sentry_value_set_by_key(mudlet_info, "Version", sentry_value_new_string(APP_VERSION)); - sentry_set_context("Mudlet", mudlet_info); - - sentry_set_tag("mudlet-version", APP_VERSION); - - /* - sentry_capture_event(sentry_value_new_message_event( - SENTRY_LEVEL_INFO, - "Testing", - "Working as expected!" - )); - */ - - throw std::runtime_error("This is a test runtime error!"); - } +#if defined(Q_OS_MACOS) && defined(INCLUDE_SENTRY) + QProcessEnvironment systemEnvironment = QProcessEnvironment::systemEnvironment(); + QString sentryDsn = systemEnvironment.value(qsl("SENTRY_DSN"), QString()); + const char* sentryDsnCStr = sentryDsn.toUtf8().constData(); + + sentry_options_t *options = sentry_options_new(); + sentry_options_set_dsn(options, sentryDsnCStr); + sentry_options_set_handler_path(options, "./crashpad_handler"); + sentry_options_set_debug(options, 1); + sentry_init(options); + + sentry_value_t mudlet_info = sentry_value_new_object(); + sentry_value_set_by_key(mudlet_info, "Version", sentry_value_new_string(APP_VERSION)); + sentry_set_context("Mudlet", mudlet_info); + + sentry_set_tag("mudlet-version", APP_VERSION); + + /* + sentry_capture_event(sentry_value_new_message_event( + SENTRY_LEVEL_INFO, + "Testing", + "Working as expected!" + )); + */ + + throw std::runtime_error("This is a test runtime error!"); #endif } @@ -200,10 +192,8 @@ int main(int argc, char* argv[]) // print stdout to console if Mudlet is started in a console in Windows // credit to https://stackoverflow.com/a/41701133 for the workaround -#ifdef Q_OS_MACOS - if(isSentryEnabled()) { - initSentry(); - } +#if defined(Q_OS_MACOS) && defined(INCLUDE_SENTRY) + initSentry(); #endif #ifdef Q_OS_WIN32 if (AttachConsole(ATTACH_PARENT_PROCESS)) { diff --git a/src/mudlet.pro b/src/mudlet.pro index 83ef5c46ac9..8a06427bb7d 100644 --- a/src/mudlet.pro +++ b/src/mudlet.pro @@ -239,6 +239,15 @@ isEmpty( 3DMAPPER_TEST ) | !equals(3DMAPPER_TEST, "NO" ) { DEFINES += INCLUDE_3DMAPPER } +############################### Sentry QT toggle ############################### +# To remove the sentry, set the environment WITH_SENTRY variable to "NO" +# ie: export WITH_SENTRY="NO" qmake +# +SENTRY_TEST = $$upper($$(WITH_SENTRY)) +isEmpty( SENTRY_TEST ) | !equals(SENTRY_TEST, "NO" ) { + DEFINES += INCLUDE_SENTRY +} + ######################## System QtKeyChain library ############################# # To use a system provided QtKeyChain library set the environmental variable # WITH_OWN_QTKEYCHAIN variable to "NO". Note that this is only likely to be