Skip to content

Commit

Permalink
refactor: driving sentry from env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulmathur001 committed Apr 17, 2024
1 parent 7315397 commit 6ce51f1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 60 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build-mudlet.yml
Expand Up @@ -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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
Expand Down
24 changes: 7 additions & 17 deletions CMakeLists.txt
Expand Up @@ -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!
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()

Expand Down
10 changes: 7 additions & 3 deletions src/CMakeLists.txt
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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}")

Expand Down
68 changes: 29 additions & 39 deletions src/main.cpp
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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"));
Expand All @@ -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
}

Expand All @@ -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)) {
Expand Down
9 changes: 9 additions & 0 deletions src/mudlet.pro
Expand Up @@ -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
Expand Down

0 comments on commit 6ce51f1

Please sign in to comment.