diff --git a/.gitmodules b/.gitmodules index c8edf6a..e34e296 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "fmt"] path = fmt url = https://github.com/fmtlib/fmt +[submodule "sentry"] + path = sentry + url = git@github.com:getsentry/sentry-native.git diff --git a/CMakeLists.txt b/CMakeLists.txt index c8a7410..082dd48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,19 @@ add_library(logvisor lib/logvisor.cpp include/logvisor/logvisor.hpp) -target_link_libraries(logvisor PUBLIC fmt) +if ("${SENTRY_DSN}" STREQUAL "") + message(STATUS "SENTRY_DSN not set, not enabling Sentry") + target_compile_definitions(logvisor PUBLIC SENTRY_ENABLED=0) + set(SENTRY_LIB "") +else() + message(STATUS "Enabling Sentry integration") + add_subdirectory(sentry) + target_compile_definitions(logvisor PUBLIC SENTRY_ENABLED=1) + target_compile_definitions(logvisor PRIVATE SENTRY_DSN="${SENTRY_DSN}") + set(SENTRY_LIB sentry) +endif() + +target_link_libraries(logvisor PUBLIC fmt ${SENTRY_LIB}) if(NX) target_link_libraries(logvisor PUBLIC debug nxd optimized nx) else() @@ -33,7 +45,7 @@ set(config_install_dir "lib/cmake/logvisor") # Associate target with export install( - TARGETS logvisor fmt + TARGETS logvisor fmt ${SENTRY_LIB} EXPORT logvisorTargets ARCHIVE DESTINATION "lib" INCLUDES DESTINATION include # This sets the INTERFACE_INCLUDE_DIRECTORIES property of the target. diff --git a/include/logvisor/logvisor.hpp b/include/logvisor/logvisor.hpp index 1e9b18a..7c15dd1 100644 --- a/include/logvisor/logvisor.hpp +++ b/include/logvisor/logvisor.hpp @@ -153,6 +153,16 @@ void RegisterFileLogger(const char* filepath); */ void RegisterStandardExceptions(); +#if SENTRY_ENABLED +/** + * @brief Register Sentry crash reporting & logging. + * @param appName The application name + * @param appVersion The application version + * @param cacheDir Directory for Sentry cache files + */ +void RegisterSentry(const char* appName, const char* appVersion, const char* cacheDir); +#endif + #if _WIN32 /** * @brief Spawn an application-owned cmd.exe window for displaying console output diff --git a/lib/logvisor.cpp b/lib/logvisor.cpp index 40fd302..5b3846d 100644 --- a/lib/logvisor.cpp +++ b/lib/logvisor.cpp @@ -34,6 +34,10 @@ #include #include "logvisor/logvisor.hpp" +#if SENTRY_ENABLED +#include +#endif + /* ANSI sequences */ #define RED "\x1b[1;31m" #define YELLOW "\x1b[1;33m" @@ -737,6 +741,27 @@ void RegisterStandardExceptions() { signal(SIGFPE, AbortHandler); } +#if SENTRY_ENABLED +void RegisterSentry(const char* appName, const char* appVersion, const char* cacheDir) { + sentry_options_t *options = sentry_options_new(); + sentry_options_set_database_path(options, cacheDir); + sentry_options_set_auto_session_tracking(options, true); + sentry_options_set_symbolize_stacktraces(options, true); + sentry_options_set_dsn(options, SENTRY_DSN); + +#ifdef NDEBUG + sentry_options_set_environment(options, "release"); +#else + sentry_options_set_environment(options, "debug"); +#endif + + std::string release = fmt::format(FMT_STRING("{}@{}"), appName, appVersion); + sentry_options_set_release(options, release.c_str()); + + sentry_init(options); +} +#endif + struct FileLogger : public ILogger { FILE* fp = nullptr; FileLogger(uint64_t typeHash) : ILogger(typeHash) {} diff --git a/sentry b/sentry new file mode 160000 index 0000000..aee5dc1 --- /dev/null +++ b/sentry @@ -0,0 +1 @@ +Subproject commit aee5dc1a55dee01477f20016c197084a501db0de