From 76e404154058f36fc4f5ebf95ca4dc67638c3ce2 Mon Sep 17 00:00:00 2001 From: FailCake Date: Tue, 2 Jul 2024 11:31:41 +0200 Subject: [PATCH] Add RAWRBOX_TRACE_EXCEPTIONS --- CMakeLists.txt | 5 ++++ README.md | 2 ++ .../include/rawrbox/bass/sound/instance.hpp | 1 + rawrbox.engine/CMakeLists.txt | 1 + rawrbox.engine/src/engine.cpp | 10 ++++--- rawrbox.utils/CMakeLists.txt | 17 +++++++---- .../include/rawrbox/utils/logger.hpp | 30 ++++++++++++++----- .../include/rawrbox/utils/threading.hpp | 2 +- rawrbox.utils/src/threading.cpp | 12 ++++++-- 9 files changed, 60 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 154c2007..fbc75bf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ list(PREPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR}) # OPTIONS --- option(RAWRBOX_USE_WAYLAND "Use Wayland for linux" OFF) +# Build ----- option(RAWRBOX_BUILD_TESTING "Build tests" OFF) option(RAWRBOX_BUILD_SAMPLES "Build samples" OFF) @@ -42,13 +43,17 @@ option(RAWRBOX_BUILD_RAWRBOX_SCRIPTING_UNSAFE "Enable unsafe scripting (io / etc option(RAWRBOX_BUILD_RAWRBOX_SCRIPTING_EXCEPTION "Enables scripting throwing exceptions instead of catching them" OFF) option(RAWRBOX_BUILD_MSVC_MULTITHREADED_RUNTIME "Build with msvc multithreading" OFF) option(RAWRBOX_BUILD_QHULL "Include QHull on utils" OFF) +# --------------- # Supports ----- option(RAWRBOX_DISABLE_SUPPORT_DX12 "Disable dx12 support" OFF) option(RAWRBOX_DISABLE_SUPPORT_VULKAN "Disable vulkan support" OFF) # --------------- +# Other ----- option(RAWRBOX_DEV_MODE "Builds all modules, used for developing rawrbox" OFF) +option(RAWRBOX_TRACE_EXCEPTIONS "Enables exception tracing" ON) +# --------------- # ----- # Uncomment for package.lock generation diff --git a/README.md b/README.md index 1c2de456..6da6cf99 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,8 @@ This engine started as a C++ training project, with hopes of being applied in up | `RAWRBOX_DISABLE_SUPPORT_VULKAN` | Disable vulkan support | | -- | -- | | `RAWRBOX_DEV_MODE` | Enables all the modules, used for rawrbox development | +| -- | -- | +| `RAWRBOX_TRACE_EXCEPTIONS` | Enables exception tracing |

diff --git a/rawrbox.bass/include/rawrbox/bass/sound/instance.hpp b/rawrbox.bass/include/rawrbox/bass/sound/instance.hpp index f2c08099..f11f8d4a 100644 --- a/rawrbox.bass/include/rawrbox/bass/sound/instance.hpp +++ b/rawrbox.bass/include/rawrbox/bass/sound/instance.hpp @@ -6,6 +6,7 @@ #include #include +#include #include namespace rawrbox { diff --git a/rawrbox.engine/CMakeLists.txt b/rawrbox.engine/CMakeLists.txt index 8394b6af..9cde3c80 100644 --- a/rawrbox.engine/CMakeLists.txt +++ b/rawrbox.engine/CMakeLists.txt @@ -8,6 +8,7 @@ add_library(${output_target} ${RAWRBOX_LIBRARY_TYPE} ${RAWRBOX_ENGINE_IMPORTS}) target_include_directories(${output_target} PUBLIC "include") target_compile_definitions(${output_target} PRIVATE _CRT_SECURE_NO_WARNINGS NOMINMAX) target_compile_definitions(${output_target} PUBLIC RAWRBOX_ENGINE) + target_compile_features(${output_target} PUBLIC cxx_std_${CMAKE_CXX_STANDARD}) target_link_libraries(${output_target} PUBLIC RAWRBOX.UTILS) diff --git a/rawrbox.engine/src/engine.cpp b/rawrbox.engine/src/engine.cpp index 6ce0c16f..5e7072c4 100644 --- a/rawrbox.engine/src/engine.cpp +++ b/rawrbox.engine/src/engine.cpp @@ -4,7 +4,9 @@ #include #include -#include +#ifdef RAWRBOX_TRACE_EXCEPTIONS + #include +#endif #include @@ -59,7 +61,7 @@ namespace rawrbox { // Setup render threading auto renderThread = std::jthread([this]() { -#ifndef _DEBUG +#ifdef RAWRBOX_TRACE_EXCEPTIONS try { #endif rawrbox::RENDER_THREAD_ID = std::this_thread::get_id(); @@ -115,7 +117,7 @@ namespace rawrbox { this->onThreadShutdown(rawrbox::ENGINE_THREADS::THREAD_RENDER); this->_shutdown = rawrbox::ENGINE_THREADS::THREAD_INPUT; // Done killing rendering, now destroy glfw -#ifndef _DEBUG +#ifdef RAWRBOX_TRACE_EXCEPTIONS } catch (const cpptrace::exception_with_message& err) { this->prettyPrintErr(err.message()); @@ -124,7 +126,7 @@ namespace rawrbox { } catch (const std::exception& err) { this->prettyPrintErr(err.what()); - fmt::print("▒▒{}▒▒\n", fmt::format(fmt::bg(fmt::color::dark_red), fmt::format(fmt::fg(fmt::color::white), " If you are a developer, please use the logger error in RAWRBOX.UTILS for a better stack trace "))); + fmt::print("▒▒{}▒▒\n", fmt::styled(" If you are a developer, please use the logger error in RAWRBOX.UTILS for a better stack trace ", fmt::bg(fmt::color::dark_red) | fmt::fg(fmt::color::white))); cpptrace::generate_trace().print(); throw err; } diff --git a/rawrbox.utils/CMakeLists.txt b/rawrbox.utils/CMakeLists.txt index 126c0a7e..73af4fb1 100644 --- a/rawrbox.utils/CMakeLists.txt +++ b/rawrbox.utils/CMakeLists.txt @@ -77,9 +77,13 @@ if(thread-pool_ADDED) endif() # DEPS ---- -CPMAddPackage("gh:jeremy-rifkin/cpptrace@0.6.2") -if(cpptrace_ADDED) - set_lib_runtime_mt(cpptrace-lib) +if(RAWRBOX_TRACE_EXCEPTIONS) + CPMAddPackage("gh:jeremy-rifkin/cpptrace@0.6.2") + if(cpptrace_ADDED) + set_lib_runtime_mt(cpptrace-lib) + endif() + + list(APPEND EXTRA_UTIL_LIBS cpptrace::cpptrace) endif() # -------------- # ---- @@ -90,6 +94,10 @@ target_include_directories(${output_target} PUBLIC "include" ${EXTRA_UTIL_INCLUD target_compile_features(${output_target} PUBLIC cxx_std_${CMAKE_CXX_STANDARD}) target_compile_definitions(${output_target} PRIVATE _CRT_SECURE_NO_WARNINGS NOMINMAX) target_compile_definitions(${output_target} PUBLIC RAWRBOX_UTILS) +if(RAWRBOX_TRACE_EXCEPTIONS) + target_compile_definitions(${output_target} PUBLIC RAWRBOX_TRACE_EXCEPTIONS) +endif() + target_link_libraries(${output_target} PUBLIC ${EXTRA_UTIL_LIBS} @@ -97,7 +105,6 @@ target_link_libraries(${output_target} PUBLIC fmt::fmt glaze::glaze magic_enum::magic_enum - cpptrace::cpptrace ) set_lib_runtime_mt(${output_target}) @@ -107,7 +114,7 @@ set_lib_runtime_mt(${output_target}) include(../cmake/catch2.cmake) # -------------- -if(WIN32) +if(WIN32 AND RAWRBOX_TRACE_EXCEPTIONS) add_custom_command( TARGET ${output_target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different diff --git a/rawrbox.utils/include/rawrbox/utils/logger.hpp b/rawrbox.utils/include/rawrbox/utils/logger.hpp index 5f5545f9..9773b110 100644 --- a/rawrbox.utils/include/rawrbox/utils/logger.hpp +++ b/rawrbox.utils/include/rawrbox/utils/logger.hpp @@ -1,6 +1,8 @@ #pragma once -#include +#ifdef RAWRBOX_TRACE_EXCEPTIONS + #include +#endif #include #include @@ -25,12 +27,32 @@ namespace rawrbox { if (this->_autoNewLine) fmt::print("\n"); } +#ifdef RAWRBOX_TRACE_EXCEPTIONS template cpptrace::runtime_error error(fmt::format_string fmt, T&&... args) { auto str = fmt::format(fmt, std::forward(args)...); return cpptrace::runtime_error(fmt::format("[{} ▓ {}]: {}", fmt::styled("ERROR", fmt::fg(fmt::color::red)), fmt::styled(this->_title, fmt::fg(fmt::color::gold)), str)); } + template + static cpptrace::runtime_error err(const std::string& title, fmt::format_string fmt, T&&... args) { + auto str = fmt::format(fmt, std::forward(args)...); + return cpptrace::runtime_error(fmt::format("[{} ▓ {}]: {}", fmt::styled("ERROR", fmt::fg(fmt::color::red)), fmt::styled(title, fmt::fg(fmt::color::gold)), str)); + } +#else + template + std::exception error(fmt::format_string fmt, T&&... args) { + auto str = fmt::format(fmt, std::forward(args)...); + return std::runtime_error(fmt::format("[{} ▓ {}]: {}", fmt::styled("ERROR", fmt::fg(fmt::color::red)), fmt::styled(this->_title, fmt::fg(fmt::color::gold)), str)); + } + + template + static std::exception err(const std::string& title, fmt::format_string fmt, T&&... args) { + auto str = fmt::format(fmt, std::forward(args)...); + return std::runtime_error(fmt::format("[{} ▓ {}]: {}", fmt::styled("ERROR", fmt::fg(fmt::color::red)), fmt::styled(title, fmt::fg(fmt::color::gold)), str)); + } +#endif + template void printError(fmt::format_string fmt, T&&... args) { auto str = fmt::format(fmt, std::forward(args)...); @@ -54,11 +76,5 @@ namespace rawrbox { fmt::print("[{} ▓ {}]: {}", fmt::styled("SUCCESS", fmt::fg(fmt::color::lime_green)), fmt::styled(this->_title, fmt::fg(fmt::color::gold)), str); if (this->_autoNewLine) fmt::print("\n"); } - - template - static cpptrace::runtime_error err(const std::string& title, fmt::format_string fmt, T&&... args) { - auto str = fmt::format(fmt, std::forward(args)...); - return cpptrace::runtime_error(fmt::format("[{} ▓ {}]: {}", fmt::styled("ERROR", fmt::fg(fmt::color::red)), fmt::styled(title, fmt::fg(fmt::color::gold)), str)); - } }; } // namespace rawrbox diff --git a/rawrbox.utils/include/rawrbox/utils/threading.hpp b/rawrbox.utils/include/rawrbox/utils/threading.hpp index 01bd1d9a..203a55d4 100644 --- a/rawrbox.utils/include/rawrbox/utils/threading.hpp +++ b/rawrbox.utils/include/rawrbox/utils/threading.hpp @@ -16,7 +16,7 @@ namespace rawrbox { // ------------- public: - static void init(); + static void init(uint32_t threads = 5); static void shutdown(); static std::future run(const std::function& job); diff --git a/rawrbox.utils/src/threading.cpp b/rawrbox.utils/src/threading.cpp index 4c147ffc..2d9a985c 100644 --- a/rawrbox.utils/src/threading.cpp +++ b/rawrbox.utils/src/threading.cpp @@ -1,6 +1,8 @@ #include -#include +#ifdef RAWRBOX_TRACE_EXCEPTIONS + #include +#endif #include @@ -13,9 +15,9 @@ namespace rawrbox { // ------------- // ------------- - void ASYNC::init() { + void ASYNC::init(uint32_t threads) { if (_pool != nullptr) throw _logger->error("ASYNC init already called!"); - _pool = std::make_unique(5); + _pool = std::make_unique(threads); } void ASYNC::shutdown() { @@ -29,11 +31,15 @@ namespace rawrbox { if (_pool == nullptr) throw _logger->error("ASYNC not initialized!"); std::future future = _pool->submit_task(job); +#ifdef RAWRBOX_TRACE_EXCEPTIONS try { future.get(); } catch (const cpptrace::exception_with_message& e) { throw _logger->error("Fatal error\n └── {}", e.message()); } +#else + future.get(); +#endif return future; }