From 55ce59b4fb9d8ecb44cb759281c523e596e29b20 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 22 Feb 2025 03:40:43 -0600 Subject: [PATCH] updated build system --- scripts/env.sh | 1 + support/include/utils/error.hpp | 31 ++++ support/src/utils/error.cpp | 79 --------- support/src/utils/error_handler.cpp | 241 ++++++++++++++++++++++++++ support/test/src/utils/error_test.cpp | 7 + 5 files changed, 280 insertions(+), 79 deletions(-) create mode 100644 support/src/utils/error_handler.cpp diff --git a/scripts/env.sh b/scripts/env.sh index cd0c8df3..ce8873f1 100755 --- a/scripts/env.sh +++ b/scripts/env.sh @@ -358,6 +358,7 @@ echo " Company name: ${PROJECT_COMPANY_NAME}" echo " Copyright: ${PROJECT_COPYRIGHT}" echo " Description: ${PROJECT_DESC}" echo " Dist dir: ${PROJECT_DIST_DIR}" +echo " Enable v2 errors: ${PROJECT_ENABLE_V2_ERRORS}" echo " External build root: ${PROJECT_EXTERNAL_BUILD_ROOT}" echo " File part: ${PROJECT_FILE_PART}" echo " Is ARM64: ${PROJECT_IS_ARM64}" diff --git a/support/include/utils/error.hpp b/support/include/utils/error.hpp index 19db8e82..a78a02aa 100644 --- a/support/include/utils/error.hpp +++ b/support/include/utils/error.hpp @@ -98,7 +98,38 @@ struct iostream_exception_handler final : public i_exception_handler { #endif // defined(PROJECT_ENABLE_V2_ERRORS) }; +#if defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) +struct spdlog_exception_handler final : public i_exception_handler { + void handle_debug(std::string_view function_name, + std::string_view msg) const override; + + void handle_error(std::string_view function_name, + std::string_view msg) const override; + + void handle_exception(std::string_view function_name) const override; + + void handle_exception(std::string_view function_name, + const std::exception &ex) const override; + + void handle_info(std::string_view function_name, + std::string_view msg) const override; + + void handle_trace(std::string_view function_name, + std::string_view msg) const override; + + void handle_warn(std::string_view function_name, + std::string_view msg) const override; + +private: + iostream_exception_handler fallback{}; +}; +#endif // defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) + +#if defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) +inline const spdlog_exception_handler default_exception_handler{}; +#else // !defined(PROJECT_ENABLE_SPDLOG) || !defined(PROJECT_ENABLE_V2_ERRORS) inline const iostream_exception_handler default_exception_handler{}; +#endif // defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) #if defined(PROJECT_ENABLE_TESTING) extern std::atomic exception_handler; diff --git a/support/src/utils/error.cpp b/support/src/utils/error.cpp index 350a5fe2..feebdbcd 100644 --- a/support/src/utils/error.cpp +++ b/support/src/utils/error.cpp @@ -22,85 +22,6 @@ #include "utils/error.hpp" namespace repertory::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}; - auto create_error_message(std::vector items) -> std::string { std::stringstream stream{}; for (std::size_t idx = 0U; idx < items.size(); ++idx) { diff --git a/support/src/utils/error_handler.cpp b/support/src/utils/error_handler.cpp new file mode 100644 index 00000000..827ce2b1 --- /dev/null +++ b/support/src/utils/error_handler.cpp @@ -0,0 +1,241 @@ +/* + Copyright <2018-2025> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ +#include "utils/config.hpp" +#include "utils/error.hpp" + +namespace repertory::utils::error { +std::atomic exception_handler{ + &default_exception_handler}; + +#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) + +#if defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) +void spdlog_exception_handler::handle_debug(std::string_view function_name, + std::string_view msg) const { + auto console = spdlog::get("console"); + if (console) { + console->debug(utils::error::create_error_message(function_name, {msg})); + } else { + fallback.handle_debug(function_name, msg); + } + + auto file = spdlog::get("file"); + if (not file) { + return; + } + + file->debug(utils::error::create_error_message(function_name, {msg})); +} + +void spdlog_exception_handler::handle_error(std::string_view function_name, + std::string_view msg) const { + auto console = spdlog::get("console"); + if (console) { + console->error(utils::error::create_error_message(function_name, {msg})); + } else { + fallback.handle_error(function_name, msg); + } + + auto file = spdlog::get("file"); + if (not file) { + return; + } + + file->error(utils::error::create_error_message(function_name, {msg})); +} + +void spdlog_exception_handler::handle_exception( + std::string_view function_name) const { + auto console = spdlog::get("console"); + if (console) { + console->error(utils::error::create_error_message(function_name, + { + "exception", + "unknown exception", + })); + } else { + fallback.handle_exception(function_name); + } + + auto file = spdlog::get("file"); + if (not file) { + return; + } + + file->error( + utils::error::create_error_message(function_name, { + "exception", + "unknown exception", + })); +} + +void spdlog_exception_handler::handle_exception( + std::string_view function_name, const std::exception &ex) const { + auto console = spdlog::get("console"); + if (console) { + console->error(utils::error::create_error_message( + function_name, { + "exception", + (ex.what() == nullptr ? "unknown" : ex.what()), + })); + } else { + fallback.handle_exception(function_name, ex); + } + + auto file = spdlog::get("file"); + if (not file) { + return; + } + + file->error(utils::error::create_error_message( + function_name, { + "exception", + (ex.what() == nullptr ? "unknown" : ex.what()), + })); +} + +void spdlog_exception_handler::handle_info(std::string_view function_name, + std::string_view msg) const { + auto console = spdlog::get("console"); + if (console) { + console->info(utils::error::create_error_message(function_name, {msg})); + } else { + fallback.handle_info(function_name, msg); + } + + auto file = spdlog::get("file"); + if (not file) { + return; + } + + file->info(utils::error::create_error_message(function_name, {msg})); +} + +void spdlog_exception_handler::handle_trace(std::string_view function_name, + std::string_view msg) const { + auto console = spdlog::get("console"); + if (console) { + console->trace(utils::error::create_error_message(function_name, {msg})); + } else { + fallback.handle_trace(function_name, msg); + } + + auto file = spdlog::get("file"); + if (not file) { + return; + } + + file->trace(utils::error::create_error_message(function_name, {msg})); +} + +void spdlog_exception_handler::handle_warn(std::string_view function_name, + std::string_view msg) const { + auto console = spdlog::get("console"); + if (console) { + console->warn(utils::error::create_error_message(function_name, {msg})); + } else { + fallback.handle_warn(function_name, msg); + } + + auto file = spdlog::get("file"); + if (not file) { + return; + } + + file->warn(utils::error::create_error_message(function_name, {msg})); +} +#endif // defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) +} // namespace repertory::utils::error diff --git a/support/test/src/utils/error_test.cpp b/support/test/src/utils/error_test.cpp index f2cba571..540322a5 100644 --- a/support/test/src/utils/error_test.cpp +++ b/support/test/src/utils/error_test.cpp @@ -29,10 +29,17 @@ TEST(utils_error, check_default_exception_handler) { EXPECT_TRUE(utils::error::get_exception_handler() != nullptr); if (&utils::error::default_exception_handler == utils::error::get_exception_handler()) { +#if defined(PROJECT_ENABLE_SPDLOG) && defined(PROJECT_ENABLE_V2_ERRORS) + auto default_handler_is_spdlog = + is_decay_equ; + EXPECT_TRUE(default_handler_is_spdlog); +#else // !defined(PROJECT_ENABLE_SPDLOG) || !defined(PROJECT_ENABLE_V2_ERRORS) auto default_handler_is_iostream = is_decay_equ; EXPECT_TRUE(default_handler_is_iostream); +#endif } }