Skip to content

Commit

Permalink
Merge pull request #171 from edunad/improvements/exceptions
Browse files Browse the repository at this point in the history
Add RAWRBOX_TRACE_EXCEPTIONS
  • Loading branch information
edunad authored Jul 2, 2024
2 parents 264a2c9 + 76e4041 commit 1dd9890
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

<br/><br/>

Expand Down
1 change: 1 addition & 0 deletions rawrbox.bass/include/rawrbox/bass/sound/instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <rawrbox/utils/logger.hpp>

#include <cmath>
#include <memory>
#include <vector>

namespace rawrbox {
Expand Down
1 change: 1 addition & 0 deletions rawrbox.engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 6 additions & 4 deletions rawrbox.engine/src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <rawrbox/utils/threading.hpp>
#include <rawrbox/utils/timer.hpp>

#include <cpptrace/cpptrace.hpp>
#ifdef RAWRBOX_TRACE_EXCEPTIONS
#include <cpptrace/cpptrace.hpp>
#endif

#include <fmt/format.h>

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

Expand All @@ -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;
}
Expand Down
17 changes: 12 additions & 5 deletions rawrbox.utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ if(thread-pool_ADDED)
endif()

# DEPS ----
CPMAddPackage("gh:jeremy-rifkin/[email protected]")
if(cpptrace_ADDED)
set_lib_runtime_mt(cpptrace-lib)
if(RAWRBOX_TRACE_EXCEPTIONS)
CPMAddPackage("gh:jeremy-rifkin/[email protected]")
if(cpptrace_ADDED)
set_lib_runtime_mt(cpptrace-lib)
endif()

list(APPEND EXTRA_UTIL_LIBS cpptrace::cpptrace)
endif()
# --------------
# ----
Expand All @@ -90,14 +94,17 @@ 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}

thread-pool
fmt::fmt
glaze::glaze
magic_enum::magic_enum
cpptrace::cpptrace
)

set_lib_runtime_mt(${output_target})
Expand All @@ -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
Expand Down
30 changes: 23 additions & 7 deletions rawrbox.utils/include/rawrbox/utils/logger.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <cpptrace/cpptrace.hpp>
#ifdef RAWRBOX_TRACE_EXCEPTIONS
#include <cpptrace/cpptrace.hpp>
#endif

#include <fmt/color.h>
#include <fmt/format.h>
Expand All @@ -25,12 +27,32 @@ namespace rawrbox {
if (this->_autoNewLine) fmt::print("\n");
}

#ifdef RAWRBOX_TRACE_EXCEPTIONS
template <typename... T>
cpptrace::runtime_error error(fmt::format_string<T...> fmt, T&&... args) {
auto str = fmt::format(fmt, std::forward<T>(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 <typename... T>
static cpptrace::runtime_error err(const std::string& title, fmt::format_string<T...> fmt, T&&... args) {
auto str = fmt::format(fmt, std::forward<T>(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 <typename... T>
std::exception error(fmt::format_string<T...> fmt, T&&... args) {
auto str = fmt::format(fmt, std::forward<T>(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 <typename... T>
static std::exception err(const std::string& title, fmt::format_string<T...> fmt, T&&... args) {
auto str = fmt::format(fmt, std::forward<T>(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 <typename... T>
void printError(fmt::format_string<T...> fmt, T&&... args) {
auto str = fmt::format(fmt, std::forward<T>(args)...);
Expand All @@ -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 <typename... T>
static cpptrace::runtime_error err(const std::string& title, fmt::format_string<T...> fmt, T&&... args) {
auto str = fmt::format(fmt, std::forward<T>(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
2 changes: 1 addition & 1 deletion rawrbox.utils/include/rawrbox/utils/threading.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace rawrbox {
// -------------

public:
static void init();
static void init(uint32_t threads = 5);
static void shutdown();

static std::future<void> run(const std::function<void()>& job);
Expand Down
12 changes: 9 additions & 3 deletions rawrbox.utils/src/threading.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <rawrbox/utils/threading.hpp>

#include <cpptrace/cpptrace.hpp>
#ifdef RAWRBOX_TRACE_EXCEPTIONS
#include <cpptrace/cpptrace.hpp>
#endif

#include <fmt/format.h>

Expand All @@ -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<BS::thread_pool>(5);
_pool = std::make_unique<BS::thread_pool>(threads);
}

void ASYNC::shutdown() {
Expand All @@ -29,11 +31,15 @@ namespace rawrbox {
if (_pool == nullptr) throw _logger->error("ASYNC not initialized!");
std::future<void> 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;
}
Expand Down

0 comments on commit 1dd9890

Please sign in to comment.