From b932d37c5986659dc62a6824f02a47e41b339729 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Mon, 20 Nov 2023 13:16:28 +0100 Subject: [PATCH 01/19] Add sentry to CuraEngine CURA-11364 --- conanfile.py | 1 + src/main.cpp | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/conanfile.py b/conanfile.py index 6221b6c38a..1d51628fa3 100644 --- a/conanfile.py +++ b/conanfile.py @@ -98,6 +98,7 @@ def requirements(self): self.requires("protobuf/3.21.9") self.requires("zlib/1.2.12") self.requires("openssl/1.1.1l") + self.requires("sentry-native/0.6.5") def generate(self): deps = CMakeDeps(self) diff --git a/src/main.cpp b/src/main.cpp index bcfd39d9f0..e568d0e248 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,7 @@ #endif #include - +#include #include "Application.h" namespace cura @@ -37,7 +37,21 @@ int main(int argc, char** argv) #endif std::cerr << std::boolalpha; + // Setup sentry error handling. + sentry_options_t *options = sentry_options_new(); + // TODO: Right now we just hardcode the key. We should probably get that from some kind of secret for release builds + sentry_options_set_dsn(options, "https://734f9ec9024f73e53701d59c3ffddfe3@o323038.ingest.sentry.io/4506257745510401"); + // This is also the default-path. For further information and recommendations: + // https://docs.sentry.io/platforms/native/configuration/options/#database-path + sentry_options_set_database_path(options, ".sentry-native"); + // TODO: Hardcoded the version number, we should also get that from somewhere. Can't be bothered to figure that out now + sentry_options_set_release(options, "curaengine@1.0.0"); + sentry_options_set_debug(options, 1); + sentry_init(options); + cura::Application::getInstance().run(argc, argv); + sentry_close(); + return 0; } From 7d6dc2cdf8243dcdfe4cf44be89836f86944a9b9 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Mon, 20 Nov 2023 13:41:50 +0100 Subject: [PATCH 02/19] Also add sentry to CMakeLists CURA-11364 --- CMakeLists.txt | 2 ++ src/main.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 27fcdf5bd8..cb1b3cc708 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -201,6 +201,7 @@ find_package(fmt REQUIRED) find_package(range-v3 REQUIRED) find_package(scripta REQUIRED) find_package(neargye-semver REQUIRED) +find_package(sentry REQUIRED) if (ENABLE_TESTING) find_package(GTest REQUIRED) @@ -221,6 +222,7 @@ target_link_libraries(_CuraEngine asio-grpc::asio-grpc grpc::grpc protobuf::libprotobuf + sentry::sentry $<$:GTest::gtest>) if (NOT WIN32) diff --git a/src/main.cpp b/src/main.cpp index e568d0e248..9a7f06c3ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,7 +48,7 @@ int main(int argc, char** argv) sentry_options_set_release(options, "curaengine@1.0.0"); sentry_options_set_debug(options, 1); sentry_init(options); - + cura::Application::getInstance().run(argc, argv); sentry_close(); From 67b924d1d2fedb92aa82c7793f6f2dd94b7e141d Mon Sep 17 00:00:00 2001 From: nallath Date: Mon, 20 Nov 2023 12:42:57 +0000 Subject: [PATCH 03/19] Applied clang-format. --- src/main.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9a7f06c3ae..aa3c634f6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,8 +7,10 @@ #include //For setpriority. #endif -#include #include + +#include + #include "Application.h" namespace cura @@ -38,7 +40,7 @@ int main(int argc, char** argv) std::cerr << std::boolalpha; // Setup sentry error handling. - sentry_options_t *options = sentry_options_new(); + sentry_options_t* options = sentry_options_new(); // TODO: Right now we just hardcode the key. We should probably get that from some kind of secret for release builds sentry_options_set_dsn(options, "https://734f9ec9024f73e53701d59c3ffddfe3@o323038.ingest.sentry.io/4506257745510401"); // This is also the default-path. For further information and recommendations: @@ -48,7 +50,7 @@ int main(int argc, char** argv) sentry_options_set_release(options, "curaengine@1.0.0"); sentry_options_set_debug(options, 1); sentry_init(options); - + cura::Application::getInstance().run(argc, argv); sentry_close(); From 5e22539dace1f19000b98d9bc7a5dad3f5570ed7 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Mon, 20 Nov 2023 14:53:01 +0100 Subject: [PATCH 04/19] Correctly set the version number when sending to sentry CURA-13364 --- src/main.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9a7f06c3ae..d6f2cb9640 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include #include +#include #include "Application.h" namespace cura @@ -44,11 +45,11 @@ int main(int argc, char** argv) // This is also the default-path. For further information and recommendations: // https://docs.sentry.io/platforms/native/configuration/options/#database-path sentry_options_set_database_path(options, ".sentry-native"); - // TODO: Hardcoded the version number, we should also get that from somewhere. Can't be bothered to figure that out now - sentry_options_set_release(options, "curaengine@1.0.0"); - sentry_options_set_debug(options, 1); + std::string version = "curaengine@"; + version += std::string(CURA_ENGINE_VERSION); + sentry_options_set_release(options, version.c_str()); sentry_init(options); - + cura::Application::getInstance().run(argc, argv); sentry_close(); From 785490c1f76f177691546ba2e77a3b2c5c9df61c Mon Sep 17 00:00:00 2001 From: nallath Date: Mon, 20 Nov 2023 14:02:50 +0000 Subject: [PATCH 05/19] Applied clang-format. --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 84920c5006..9acb8d97d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,9 +8,10 @@ #endif #include +#include #include -#include + #include "Application.h" namespace cura From 02cb17c831d17702caddaaa7c0d8b60336024f6e Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Tue, 21 Nov 2023 10:50:43 +0100 Subject: [PATCH 06/19] Store sentry logs in correct place By default it stores them in the cwd, which won't work for actual instals. It now places them in the root Cura folder CURA-11364 --- src/main.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 84920c5006..c185c6d163 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,7 +45,19 @@ int main(int argc, char** argv) sentry_options_set_dsn(options, "https://734f9ec9024f73e53701d59c3ffddfe3@o323038.ingest.sentry.io/4506257745510401"); // This is also the default-path. For further information and recommendations: // https://docs.sentry.io/platforms/native/configuration/options/#database-path - sentry_options_set_database_path(options, ".sentry-native"); + std::string config_path = ""; + +#if defined(__linux__) + config_path += getenv("HOME"); + config_path += "/.local/share/cura/"; +#elif defined(__APPLE__) && defined(__MACH__) + config_path += getenv("HOME"); + config_path += "/Library/Application Support/cura/"; +#elif defined(_WIN64) + config_path = "%APPDATA%\\cura\\"; +#endif + config_path += ".sentry-native"; + sentry_options_set_database_path(options, config_path.c_str()); std::string version = "curaengine@"; version += std::string(CURA_ENGINE_VERSION); sentry_options_set_release(options, version.c_str()); From c6047b7c1379121478344b8eb7245abcb87769a5 Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Tue, 21 Nov 2023 12:17:59 +0100 Subject: [PATCH 07/19] Add optional Sentry integration The update adds an option to enable or disable Sentry integration in the code. Sentry is configured to capture and report crashes only if the appropriate option is enabled and a valid DSN (data source name) is provided. The suitable changes have been made in the Conan, CMake configurations and source code files. To compile locally with Sentry support run the conan install command with: ``` -c user.curaengine:sentry_url= -o curaengine:enable_sentry=True ``` For the runners the conf `user.curaengine:sentry_url` has been added to the relevant runner profiles. Once the new workflows have been merged that url will move out of the profiles and passed along as a repository variable in the GH runner. Contributes to CURA-11364 --- CMakeLists.txt | 9 +++++++-- conanfile.py | 10 +++++++++- src/main.cpp | 10 ++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cb1b3cc708..8dbd0534a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ option(ENABLE_TESTING "Build with unit tests" OFF) option(EXTENSIVE_WARNINGS "Build with all warnings" ON) option(ENABLE_PLUGINS "Build with plugins" ON) option(ENABLE_REMOTE_PLUGINS "Build with all warnings" OFF) +option(ENABLE_SENTRY "Send crash data via Sentry" OFF) option(ENABLE_MORE_COMPILER_OPTIMIZATION_FLAGS "Enable more optimization flags" ON) option(USE_SYSTEM_LIBS "Use the system libraries if available" OFF) option(OLDER_APPLE_CLANG "Apple Clang <= 13 used" OFF) @@ -173,6 +174,7 @@ target_compile_definitions(_CuraEngine CURA_ENGINE_VERSION=\"${CURA_ENGINE_VERSION}\" $<$:BUILD_TESTS> PRIVATE + $<$:SENTRY_URL=\"${SENTRY_URL}\"> $<$:NOMINMAX> $<$:ASSERT_INSANE_OUTPUT> $<$:USE_CPU_TIME> @@ -201,7 +203,10 @@ find_package(fmt REQUIRED) find_package(range-v3 REQUIRED) find_package(scripta REQUIRED) find_package(neargye-semver REQUIRED) -find_package(sentry REQUIRED) + +if (ENABLE_SENTRY) + find_package(sentry REQUIRED) +endif () if (ENABLE_TESTING) find_package(GTest REQUIRED) @@ -222,7 +227,7 @@ target_link_libraries(_CuraEngine asio-grpc::asio-grpc grpc::grpc protobuf::libprotobuf - sentry::sentry + $<$:sentry::sentry> $<$:GTest::gtest>) if (NOT WIN32) diff --git a/conanfile.py b/conanfile.py index 1d51628fa3..9019773558 100644 --- a/conanfile.py +++ b/conanfile.py @@ -28,6 +28,7 @@ class CuraEngineConan(ConanFile): "enable_benchmarks": [True, False], "enable_extensive_warnings": [True, False], "enable_plugins": [True, False], + "enable_sentry": [True, False], "enable_remote_plugins": [True, False], } default_options = { @@ -35,6 +36,7 @@ class CuraEngineConan(ConanFile): "enable_benchmarks": False, "enable_extensive_warnings": False, "enable_plugins": True, + "enable_sentry": False, "enable_remote_plugins": False, } @@ -56,6 +58,8 @@ def export_sources(self): def config_options(self): if not self.options.enable_plugins: del self.options.enable_remote_plugins + if self.conf.get("user.curaengine:sentry_url", "", check_type=str) == "": + del self.options.enable_sentry def configure(self): self.options["boost"].header_only = True @@ -83,6 +87,8 @@ def build_requirements(self): def requirements(self): if self.options.enable_arcus: self.requires("arcus/5.3.0") + if self.options.get_safe("enable_sentry", False): + self.requires("sentry-native/0.6.5") self.requires("asio-grpc/2.6.0") self.requires("grpc/1.50.1") self.requires("curaengine_grpc_definitions/(latest)@ultimaker/testing") @@ -98,7 +104,6 @@ def requirements(self): self.requires("protobuf/3.21.9") self.requires("zlib/1.2.12") self.requires("openssl/1.1.1l") - self.requires("sentry-native/0.6.5") def generate(self): deps = CMakeDeps(self) @@ -111,6 +116,9 @@ def generate(self): tc.variables["ENABLE_BENCHMARKS"] = self.options.enable_benchmarks tc.variables["EXTENSIVE_WARNINGS"] = self.options.enable_extensive_warnings tc.variables["OLDER_APPLE_CLANG"] = self.settings.compiler == "apple-clang" and Version(self.settings.compiler.version) < "14" + if self.options.get_safe("enable_sentry", False): + tc.variables["ENABLE_SENTRY"] = True + tc.variables["SENTRY_URL"] = self.conf.get("user.curaengine:sentry_url", "", check_type=str) if self.options.enable_plugins: tc.variables["ENABLE_PLUGINS"] = True tc.variables["ENABLE_REMOTE_PLUGINS"] = self.options.enable_remote_plugins diff --git a/src/main.cpp b/src/main.cpp index 8614af364c..09d59403a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,10 @@ #include //For setpriority. #endif +#ifdef SENTRY_URL #include +#endif + #include #include @@ -40,10 +43,10 @@ int main(int argc, char** argv) #endif std::cerr << std::boolalpha; +#ifdef SENTRY_URL // Setup sentry error handling. sentry_options_t* options = sentry_options_new(); - // TODO: Right now we just hardcode the key. We should probably get that from some kind of secret for release builds - sentry_options_set_dsn(options, "https://734f9ec9024f73e53701d59c3ffddfe3@o323038.ingest.sentry.io/4506257745510401"); + sentry_options_set_dsn(options, std::string(SENTRY_URL)); // This is also the default-path. For further information and recommendations: // https://docs.sentry.io/platforms/native/configuration/options/#database-path std::string config_path = ""; @@ -63,10 +66,13 @@ int main(int argc, char** argv) version += std::string(CURA_ENGINE_VERSION); sentry_options_set_release(options, version.c_str()); sentry_init(options); +#endif cura::Application::getInstance().run(argc, argv); +#ifdef SENTRY_URL sentry_close(); +#endif return 0; } From 17466d01528c197957879a1d9a92632f70006b22 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Tue, 21 Nov 2023 12:46:28 +0100 Subject: [PATCH 08/19] Use format to create the versionstring --- src/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 09d59403a6..e8c8245084 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #endif #ifdef SENTRY_URL +#include #include #endif @@ -62,8 +63,7 @@ int main(int argc, char** argv) #endif config_path += ".sentry-native"; sentry_options_set_database_path(options, config_path.c_str()); - std::string version = "curaengine@"; - version += std::string(CURA_ENGINE_VERSION); + std::string version = fmt::format("curaengine@{}", CURA_ENGINE_VERSION); sentry_options_set_release(options, version.c_str()); sentry_init(options); #endif From adfa140c710b080f53e075f9f95ed794c165fdd5 Mon Sep 17 00:00:00 2001 From: nallath Date: Tue, 21 Nov 2023 11:47:12 +0000 Subject: [PATCH 09/19] Applied clang-format. --- src/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index e8c8245084..f60f6139f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,8 +8,9 @@ #endif #ifdef SENTRY_URL -#include #include + +#include #endif #include From 7af9a1a46f0147c9dd8f58d160fd3a4c79bd9028 Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Tue, 21 Nov 2023 13:16:13 +0100 Subject: [PATCH 10/19] Refactor sentry options and improve code readability The revision consists of refactoring of the sentry options in the main.cpp file, including setting the database path and release version. The filesystem library and fmt library have been used to improve the code readability and simplicity. Also, some changes have been made in the CMakeLists.txt file, specifically for enabling sentry and its related configurations. Contributes to CURA-11364 --- CMakeLists.txt | 12 ++++++++---- src/main.cpp | 22 ++++++++-------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8dbd0534a1..b2d88be329 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -174,7 +174,6 @@ target_compile_definitions(_CuraEngine CURA_ENGINE_VERSION=\"${CURA_ENGINE_VERSION}\" $<$:BUILD_TESTS> PRIVATE - $<$:SENTRY_URL=\"${SENTRY_URL}\"> $<$:NOMINMAX> $<$:ASSERT_INSANE_OUTPUT> $<$:USE_CPU_TIME> @@ -227,7 +226,6 @@ target_link_libraries(_CuraEngine asio-grpc::asio-grpc grpc::grpc protobuf::libprotobuf - $<$:sentry::sentry> $<$:GTest::gtest>) if (NOT WIN32) @@ -246,8 +244,14 @@ else () endif (NOT WIN32) use_threads(CuraEngine) -target_link_libraries(CuraEngine PRIVATE _CuraEngine) -target_compile_definitions(CuraEngine PRIVATE VERSION=\"${CURA_ENGINE_VERSION}\") +target_link_libraries(CuraEngine PRIVATE + _CuraEngine + $<$:sentry::sentry> +) +target_compile_definitions(CuraEngine PRIVATE + $<$:SENTRY_URL=\"${SENTRY_URL}\"> + VERSION=\"${CURA_ENGINE_VERSION}\" +) # Compiling the test environment. if (ENABLE_TESTING OR ENABLE_BENCHMARKS) diff --git a/src/main.cpp b/src/main.cpp index f60f6139f8..f44dac6a6b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Ultimaker B.V. +// Copyright (c) 2023 UltiMaker // CuraEngine is released under the terms of the AGPLv3 or higher #include //To change the formatting of std::cerr. @@ -8,9 +8,9 @@ #endif #ifdef SENTRY_URL -#include - +#include #include +#include #endif #include @@ -48,24 +48,18 @@ int main(int argc, char** argv) #ifdef SENTRY_URL // Setup sentry error handling. sentry_options_t* options = sentry_options_new(); - sentry_options_set_dsn(options, std::string(SENTRY_URL)); + sentry_options_set_dsn(options, std::string(SENTRY_URL).c_str()); // This is also the default-path. For further information and recommendations: // https://docs.sentry.io/platforms/native/configuration/options/#database-path - std::string config_path = ""; - #if defined(__linux__) - config_path += getenv("HOME"); - config_path += "/.local/share/cura/"; + const auto config_path = std::filesystem::path(std::getenv("HOME")).append( "/.local/share/cura/.sentry-native" ); #elif defined(__APPLE__) && defined(__MACH__) - config_path += getenv("HOME"); - config_path += "/Library/Application Support/cura/"; + const auto config_path = std::filesystem::path(std::getenv("HOME")).append( "/Library/Application Support/cura/.sentry-native" ); #elif defined(_WIN64) - config_path = "%APPDATA%\\cura\\"; + const auto config_path = std::filesystem::path(std::getenv("APPDATA")).append( "/cura/.sentry-native" ); #endif - config_path += ".sentry-native"; sentry_options_set_database_path(options, config_path.c_str()); - std::string version = fmt::format("curaengine@{}", CURA_ENGINE_VERSION); - sentry_options_set_release(options, version.c_str()); + sentry_options_set_release(options, fmt::format("curaengine@{}", CURA_ENGINE_VERSION).c_str()); sentry_init(options); #endif From 2ad753641940ec95b10193c22ba60c7d379f5857 Mon Sep 17 00:00:00 2001 From: jellespijker Date: Tue, 21 Nov 2023 12:16:55 +0000 Subject: [PATCH 11/19] Applied clang-format. --- src/main.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f44dac6a6b..be05974400 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,8 +9,9 @@ #ifdef SENTRY_URL #include -#include #include + +#include #endif #include @@ -52,11 +53,11 @@ int main(int argc, char** argv) // This is also the default-path. For further information and recommendations: // https://docs.sentry.io/platforms/native/configuration/options/#database-path #if defined(__linux__) - const auto config_path = std::filesystem::path(std::getenv("HOME")).append( "/.local/share/cura/.sentry-native" ); + const auto config_path = std::filesystem::path(std::getenv("HOME")).append("/.local/share/cura/.sentry-native"); #elif defined(__APPLE__) && defined(__MACH__) - const auto config_path = std::filesystem::path(std::getenv("HOME")).append( "/Library/Application Support/cura/.sentry-native" ); + const auto config_path = std::filesystem::path(std::getenv("HOME")).append("/Library/Application Support/cura/.sentry-native"); #elif defined(_WIN64) - const auto config_path = std::filesystem::path(std::getenv("APPDATA")).append( "/cura/.sentry-native" ); + const auto config_path = std::filesystem::path(std::getenv("APPDATA")).append("/cura/.sentry-native"); #endif sentry_options_set_database_path(options, config_path.c_str()); sentry_options_set_release(options, fmt::format("curaengine@{}", CURA_ENGINE_VERSION).c_str()); From 7a6098c614bf71784f870819650b8c7bc030ede7 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Tue, 21 Nov 2023 13:31:44 +0100 Subject: [PATCH 12/19] Add instruction how to set sentry_url in the code CURA-11364 --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index be05974400..ec3891b83f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,7 +45,7 @@ int main(int argc, char** argv) signal(SIGFPE, cura::signal_FPE); #endif std::cerr << std::boolalpha; - +// Want to set the sentry URL? Use '-c user.curaengine:sentry_url= -o curaengine:enable_sentry=True' with conan install #ifdef SENTRY_URL // Setup sentry error handling. sentry_options_t* options = sentry_options_new(); From 14e8ec0d70e82a1b64b0624d929c63d95a340d52 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Tue, 21 Nov 2023 14:01:29 +0100 Subject: [PATCH 13/19] Only send sentry data if use_sentry env is set CURA-11364 --- src/main.cpp | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ec3891b83f..fab26295ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,11 +10,10 @@ #ifdef SENTRY_URL #include #include - +#include #include #endif - -#include +#include #include @@ -45,29 +44,46 @@ int main(int argc, char** argv) signal(SIGFPE, cura::signal_FPE); #endif std::cerr << std::boolalpha; + + + // Want to set the sentry URL? Use '-c user.curaengine:sentry_url= -o curaengine:enable_sentry=True' with conan install #ifdef SENTRY_URL - // Setup sentry error handling. - sentry_options_t* options = sentry_options_new(); - sentry_options_set_dsn(options, std::string(SENTRY_URL).c_str()); - // This is also the default-path. For further information and recommendations: - // https://docs.sentry.io/platforms/native/configuration/options/#database-path + 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) + { + // Setup sentry error handling. + sentry_options_t* options = sentry_options_new(); + sentry_options_set_dsn(options, std::string(SENTRY_URL).c_str()); + // This is also the default-path. For further information and recommendations: + // https://docs.sentry.io/platforms/native/configuration/options/#database-path #if defined(__linux__) - const auto config_path = std::filesystem::path(std::getenv("HOME")).append("/.local/share/cura/.sentry-native"); + const auto config_path = std::filesystem::path(std::getenv("HOME")).append("/.local/share/cura/.sentry-native"); #elif defined(__APPLE__) && defined(__MACH__) - const auto config_path = std::filesystem::path(std::getenv("HOME")).append("/Library/Application Support/cura/.sentry-native"); + const auto config_path = std::filesystem::path(std::getenv("HOME")).append("/Library/Application Support/cura/.sentry-native"); #elif defined(_WIN64) - const auto config_path = std::filesystem::path(std::getenv("APPDATA")).append("/cura/.sentry-native"); + const auto config_path = std::filesystem::path(std::getenv("APPDATA")).append("/cura/.sentry-native"); #endif - sentry_options_set_database_path(options, config_path.c_str()); - sentry_options_set_release(options, fmt::format("curaengine@{}", CURA_ENGINE_VERSION).c_str()); - sentry_init(options); + sentry_options_set_database_path(options, config_path.c_str()); + sentry_options_set_release(options, fmt::format("curaengine@{}", CURA_ENGINE_VERSION).c_str()); + sentry_init(options); + } #endif cura::Application::getInstance().run(argc, argv); #ifdef SENTRY_URL - sentry_close(); + if(use_sentry) + { + sentry_close(); + } #endif return 0; From b9cbe337f86947ca796c670dc3a78c6e23515831 Mon Sep 17 00:00:00 2001 From: nallath Date: Tue, 21 Nov 2023 13:03:10 +0000 Subject: [PATCH 14/19] Applied clang-format. --- src/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fab26295ae..89a3a766ad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include #include #include + #include #endif #include @@ -46,18 +47,17 @@ int main(int argc, char** argv) std::cerr << std::boolalpha; - // 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) + if (std::strcmp(use_sentry_env, "0") == 0) { use_sentry = false; } } - if(use_sentry) + if (use_sentry) { // Setup sentry error handling. sentry_options_t* options = sentry_options_new(); @@ -80,7 +80,7 @@ int main(int argc, char** argv) cura::Application::getInstance().run(argc, argv); #ifdef SENTRY_URL - if(use_sentry) + if (use_sentry) { sentry_close(); } From c05b7766134a3d0c94fdf06884ce4c4472c7fb3c Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Tue, 21 Nov 2023 14:56:10 +0100 Subject: [PATCH 15/19] Refactor sentry config path generation The sentry config path creation is improved by leveraging the fmt library to format the respective path strings. This change streamlines the code and boosts its overall comprehensibility. Also, the sentry options set database path now makes use of the native function for string conversion. Contributes to CURA-11364 --- src/main.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 89a3a766ad..95d2ce1624 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,9 +9,11 @@ #ifdef SENTRY_URL #include -#include #include +#include +#include + #include #endif #include @@ -65,13 +67,13 @@ int main(int argc, char** argv) // This is also the default-path. For further information and recommendations: // https://docs.sentry.io/platforms/native/configuration/options/#database-path #if defined(__linux__) - const auto config_path = std::filesystem::path(std::getenv("HOME")).append("/.local/share/cura/.sentry-native"); + const auto config_path = std::filesystem::path(fmt::format("{}/.local/share/cura/.sentry-native", std::getenv("HOME"))); #elif defined(__APPLE__) && defined(__MACH__) - const auto config_path = std::filesystem::path(std::getenv("HOME")).append("/Library/Application Support/cura/.sentry-native"); + const auto config_path = std::filesystem::path(fmt::format("{}/Library/Application Support/cura/.sentry-native", std::getenv("HOME"))); #elif defined(_WIN64) - const auto config_path = std::filesystem::path(std::getenv("APPDATA")).append("/cura/.sentry-native"); + const auto config_path = std::filesystem::path(fmt::format("{}/cura/.sentry-native", std::getenv("APPDATA"))); #endif - sentry_options_set_database_path(options, config_path.c_str()); + sentry_options_set_database_path(options, config_path.native().c_str()); sentry_options_set_release(options, fmt::format("curaengine@{}", CURA_ENGINE_VERSION).c_str()); sentry_init(options); } From f397fb59586a828e148388efb8ca044a8cf02ace Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Tue, 21 Nov 2023 14:58:41 +0100 Subject: [PATCH 16/19] Log Sentry URL and Path Made sure that user identifiable paths aren't logged with a custom formatter. See example output on Linux ``` [2023-11-21 14:57:13.764] [info] Sentry config path: /home/*******/.local/share/cura/.sentry-native ``` Untested on Windows Contributes to CURA-11364 --- include/utils/format/filesystem_path.h | 47 ++++++++++++++++++++++++++ src/main.cpp | 8 +++-- 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 include/utils/format/filesystem_path.h diff --git a/include/utils/format/filesystem_path.h b/include/utils/format/filesystem_path.h new file mode 100644 index 0000000000..679b69223e --- /dev/null +++ b/include/utils/format/filesystem_path.h @@ -0,0 +1,47 @@ +// Copyright (c) 2023 UltiMaker +// CuraEngine is released under the terms of the AGPLv3 or higher + +#ifndef CURAENGINE_INCLUDE_UTILS_FORMAT_FILESYSTEM_PATH_H +#define CURAENGINE_INCLUDE_UTILS_FORMAT_FILESYSTEM_PATH_H + +#include +#include + +#include + +namespace fmt +{ +template<> +struct formatter : formatter +{ + static std::string USERNAME; + static constexpr std::string_view OBFUSCATED_STRING = "*******"; + + [[nodiscard]] static std::string anonymizePath(const std::string& path) + { + std::string anonymized_path = path; + size_t pos = anonymized_path.find(USERNAME); + while (pos != std::string::npos) + { + anonymized_path.replace(pos, USERNAME.size(), OBFUSCATED_STRING); + pos = anonymized_path.find(USERNAME, pos + OBFUSCATED_STRING.size()); + } + return anonymized_path; + } + + template + auto format(const std::filesystem::path& path, FormatContext& ctx) + { + return formatter::format(anonymizePath(path.native()), ctx); + } +}; + +#ifdef _WIN32 +std::string fmt::formatter::USERNAME = std::getenv("USERNAME") != nullptr ? std::getenv("USERNAME") : ""; +#else +std::string fmt::formatter::USERNAME = std::getenv("USER") != nullptr ? std::getenv("USER") : ""; +#endif + +} // namespace fmt + +#endif // CURAENGINE_INCLUDE_UTILS_FORMAT_FILESYSTEM_PATH_H diff --git a/src/main.cpp b/src/main.cpp index 95d2ce1624..fc11a1a611 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,12 +9,12 @@ #ifdef SENTRY_URL #include +#include #include #include -#include -#include +#include "utils/format/filesystem_path.h" #endif #include @@ -64,15 +64,17 @@ int main(int argc, char** argv) // Setup sentry error handling. sentry_options_t* options = sentry_options_new(); sentry_options_set_dsn(options, std::string(SENTRY_URL).c_str()); + spdlog::info("Sentry url: {}", std::string(SENTRY_URL).c_str()); // This is also the default-path. For further information and recommendations: // https://docs.sentry.io/platforms/native/configuration/options/#database-path #if defined(__linux__) - const auto config_path = std::filesystem::path(fmt::format("{}/.local/share/cura/.sentry-native", std::getenv("HOME"))); + const auto config_path = std::filesystem::path(fmt::format("{}/.local/share/cura/.sentry-native", std::getenv("HOME"))); #elif defined(__APPLE__) && defined(__MACH__) const auto config_path = std::filesystem::path(fmt::format("{}/Library/Application Support/cura/.sentry-native", std::getenv("HOME"))); #elif defined(_WIN64) const auto config_path = std::filesystem::path(fmt::format("{}/cura/.sentry-native", std::getenv("APPDATA"))); #endif + spdlog::info("Sentry config path: {}", config_path); sentry_options_set_database_path(options, config_path.native().c_str()); sentry_options_set_release(options, fmt::format("curaengine@{}", CURA_ENGINE_VERSION).c_str()); sentry_init(options); From 5ef89b543ee7926afe9f2d5ce919c7776baaf120 Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Tue, 21 Nov 2023 16:58:19 +0100 Subject: [PATCH 17/19] Set sentry environment based on version Contributes to CURA-11364 --- conanfile.py | 6 ++++-- src/main.cpp | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/conanfile.py b/conanfile.py index 9019773558..287e93897c 100644 --- a/conanfile.py +++ b/conanfile.py @@ -8,7 +8,8 @@ from conan.tools.files import copy, mkdir from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake, cmake_layout from conan.tools.build import check_min_cppstd -from conan.tools.scm import Version +from conan.tools.scm import Version, Git + required_conan_version = ">=1.58.0 <2.0.0" @@ -42,7 +43,8 @@ class CuraEngineConan(ConanFile): def set_version(self): if not self.version: - self.version = "5.7.0-alpha" + git = Git(self) + self.version = f"5.7.0-alpha+{git.get_commit()[:6]}" def export_sources(self): copy(self, "CMakeLists.txt", self.recipe_folder, self.export_sources_folder) diff --git a/src/main.cpp b/src/main.cpp index fc11a1a611..4f4798a175 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,10 +9,13 @@ #ifdef SENTRY_URL #include +#include #include #include -#include +#include +#include +#include #include "utils/format/filesystem_path.h" #endif @@ -72,11 +75,39 @@ int main(int argc, char** argv) #elif defined(__APPLE__) && defined(__MACH__) const auto config_path = std::filesystem::path(fmt::format("{}/Library/Application Support/cura/.sentry-native", std::getenv("HOME"))); #elif defined(_WIN64) - const auto config_path = std::filesystem::path(fmt::format("{}/cura/.sentry-native", std::getenv("APPDATA"))); + const auto config_path = std::filesystem::path(fmt::format("{}\\cura\\.sentry-native", std::getenv("APPDATA"))); #endif spdlog::info("Sentry config path: {}", config_path); - sentry_options_set_database_path(options, config_path.native().c_str()); - sentry_options_set_release(options, fmt::format("curaengine@{}", CURA_ENGINE_VERSION).c_str()); + sentry_options_set_database_path(options, static_cast(std::filesystem::absolute(config_path).native().c_str())); + constexpr std::string_view cura_engine_version{ CURA_ENGINE_VERSION }; + const auto version = semver::from_string( + cura_engine_version + | ranges::views::take_while( + [](const auto& c) + { + return c != '+'; + }) + | ranges::to); + if (ranges::contains(cura_engine_version, '+') || version.prerelease_type == semver::prerelease::alpha) + { + // Not a production build + sentry_options_set_environment(options, "development"); + sentry_options_set_release( + options, + fmt::format( + "curaengine@{}.{}.{}-{}.{}", + version.major, + version.minor, + version.patch, + version.prerelease_type == semver::prerelease::alpha ? "alpha" : "beta", + version.prerelease_number) + .c_str()); + } + else + { + sentry_options_set_environment(options, "production"); + sentry_options_set_release(options, fmt::format("curaengine@{}", version.to_string()).c_str()); + } sentry_init(options); } #endif From d020d0f17b02859dff0f198a033e1f4cd3ed86c3 Mon Sep 17 00:00:00 2001 From: Remco Burema Date: Wed, 22 Nov 2023 10:18:14 +0100 Subject: [PATCH 18/19] Fix Sentry (crash logging) for Win64. The issue was windows defaults to wide-strings. Sadly sentry doesn't seem to take wide-string paths, so that users with non-ASCII names might be left out again :-/ part of CURA-11364 --- include/utils/format/filesystem_path.h | 2 +- src/main.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/utils/format/filesystem_path.h b/include/utils/format/filesystem_path.h index 679b69223e..f6f8d697a7 100644 --- a/include/utils/format/filesystem_path.h +++ b/include/utils/format/filesystem_path.h @@ -32,7 +32,7 @@ struct formatter : formatter template auto format(const std::filesystem::path& path, FormatContext& ctx) { - return formatter::format(anonymizePath(path.native()), ctx); + return formatter::format(anonymizePath(path.generic_string()), ctx); } }; diff --git a/src/main.cpp b/src/main.cpp index 4f4798a175..9f02aba974 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ #include #include -#include +#include #include #include "utils/format/filesystem_path.h" @@ -78,7 +78,7 @@ int main(int argc, char** argv) const auto config_path = std::filesystem::path(fmt::format("{}\\cura\\.sentry-native", std::getenv("APPDATA"))); #endif spdlog::info("Sentry config path: {}", config_path); - sentry_options_set_database_path(options, static_cast(std::filesystem::absolute(config_path).native().c_str())); + sentry_options_set_database_path(options, std::filesystem::absolute(config_path).generic_string().c_str()); constexpr std::string_view cura_engine_version{ CURA_ENGINE_VERSION }; const auto version = semver::from_string( cura_engine_version From 67eb8e03b6f6fe707b658ccd7f601343aa313231 Mon Sep 17 00:00:00 2001 From: Remco Burema Date: Wed, 22 Nov 2023 10:59:55 +0100 Subject: [PATCH 19/19] 'Fix' weird Win64 issue with ranges-library. The file 'subrange.hpp' of the ranges library wouldn't compile properly on Windows, work around this strange limitation. part of CURA-11364 --- src/main.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9f02aba974..1965a329cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,6 @@ #include #include -#include #include "utils/format/filesystem_path.h" #endif @@ -80,14 +79,7 @@ int main(int argc, char** argv) spdlog::info("Sentry config path: {}", config_path); sentry_options_set_database_path(options, std::filesystem::absolute(config_path).generic_string().c_str()); constexpr std::string_view cura_engine_version{ CURA_ENGINE_VERSION }; - const auto version = semver::from_string( - cura_engine_version - | ranges::views::take_while( - [](const auto& c) - { - return c != '+'; - }) - | ranges::to); + const auto version = semver::from_string(cura_engine_version.substr(0, cura_engine_version.find_first_of('+'))); if (ranges::contains(cura_engine_version, '+') || version.prerelease_type == semver::prerelease::alpha) { // Not a production build