diff --git a/support/include/utils/error.hpp b/support/include/utils/error.hpp index 8fa36f6..f5ccb5c 100644 --- a/support/include/utils/error.hpp +++ b/support/include/utils/error.hpp @@ -72,89 +72,37 @@ protected: i_exception_handler() = default; }; -struct iostream_exception_handler final : i_exception_handler { +struct iostream_exception_handler final : public i_exception_handler { #if defined(PROJECT_ENABLE_V2_ERRORS) void handle_debug(std::string_view function_name, - std::string_view msg) const override { - std::cout << create_error_message(function_name, - { - "debug", - msg, - }) - << std::endl; - } + std::string_view msg) const override; #endif // defined(PROJECT_ENABLE_V2_ERRORS) void handle_error(std::string_view function_name, - std::string_view msg) const override { - std::cerr << create_error_message(function_name, - { - "error", - msg, - }) - << std::endl; - } + std::string_view msg) const override; - void handle_exception(std::string_view function_name) const override { - std::cerr << create_error_message(function_name, - { - "error", - "exception", - "unknown", - }) - << std::endl; - } + void handle_exception(std::string_view function_name) const override; void handle_exception(std::string_view function_name, - const std::exception &ex) const override { - std::cerr << create_error_message( - function_name, - { - "error", - "exception", - (ex.what() == nullptr ? "unknown" : ex.what()), - }) - << std::endl; - } + const std::exception &ex) const override; #if defined(PROJECT_ENABLE_V2_ERRORS) void handle_info(std::string_view function_name, - std::string_view msg) const override { - std::cout << create_error_message(function_name, - { - "info", - msg, - }) - << std::endl; - } + std::string_view msg) const override; void handle_trace(std::string_view function_name, - std::string_view msg) const override { - std::cout << create_error_message(function_name, - { - "trace", - msg, - }) - << std::endl; - } + std::string_view msg) const override; void handle_warn(std::string_view function_name, - std::string_view msg) const override { - std::cout << create_error_message(function_name, - { - "warn", - msg, - }) - << std::endl; - } + std::string_view msg) const override; #endif // defined(PROJECT_ENABLE_V2_ERRORS) }; inline const iostream_exception_handler default_exception_handler{}; +#if defined(PROJECT_ENABLE_TESTING) extern std::atomic exception_handler; -#if defined(PROJECT_ENABLE_TESTING) [[nodiscard]] inline auto get_exception_handler() -> const i_exception_handler * { return exception_handler; diff --git a/support/src/utils/error.cpp b/support/src/utils/error.cpp index 68c3c99..02b68ff 100644 --- a/support/src/utils/error.cpp +++ b/support/src/utils/error.cpp @@ -22,6 +22,82 @@ #include "utils/error.hpp" namespace monitarr::utils::error { +#if defined(PROJECT_ENABLE_V2_ERRORS) +void iostream_exception_handler::handle_debug(std::string_view function_name, + std::string_view msg) const { + std::cout << create_error_message({ + "debug", + function_name, + msg, + }) + << std::endl; +} +#endif // defined(PROJECT_ENABLE_V2_ERRORS) + +void iostream_exception_handler::handle_error(std::string_view function_name, + std::string_view msg) const { + std::cerr << create_error_message({ + "error", + function_name, + msg, + }) + << std::endl; +} + +void iostream_exception_handler::handle_exception( + std::string_view function_name) const { + std::cerr << create_error_message({ + "error", + function_name, + "exception", + "unknown", + }) + << std::endl; +} + +void iostream_exception_handler::handle_exception( + std::string_view function_name, const std::exception &ex) const { + std::cerr << create_error_message({ + "error", + function_name, + "exception", + (ex.what() == nullptr ? "unknown" : ex.what()), + }) + << std::endl; +} + +#if defined(PROJECT_ENABLE_V2_ERRORS) +void iostream_exception_handler::handle_info(std::string_view function_name, + std::string_view msg) const { + std::cout << create_error_message({ + "info", + function_name, + msg, + }) + << std::endl; +} + +void iostream_exception_handler::handle_trace(std::string_view function_name, + std::string_view msg) const { + std::cout << create_error_message({ + "trace", + function_name, + msg, + }) + << std::endl; +} + +void iostream_exception_handler::handle_warn(std::string_view function_name, + std::string_view msg) const { + std::cout << create_error_message({ + "warn", + function_name, + msg, + }) + << std::endl; +} +#endif // defined(PROJECT_ENABLE_V2_ERRORS) + std::atomic exception_handler{ &default_exception_handler}; @@ -40,7 +116,7 @@ auto create_error_message(std::vector items) -> std::string { auto create_error_message(std::string_view function_name, std::vector items) -> std::string { - items.insert(items.begin(), function_name); + items.insert(std::next(items.begin()), function_name); return create_error_message(items); }