From e1fdeb269205664b905f6adc301a37c8d7cf612e Mon Sep 17 00:00:00 2001 From: light7734 Date: Fri, 11 Jul 2025 02:09:02 +0330 Subject: [PATCH] refactor: convert lt_assert from macro to variadic template function --- modules/debug/include/debug/assertions.hpp | 26 ++++++++++++++----- .../engine/include/engine/core/entrypoint.hpp | 4 +-- modules/logger/include/logger/logger.hpp | 6 +++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/modules/debug/include/debug/assertions.hpp b/modules/debug/include/debug/assertions.hpp index 4356306..c81d3b3 100644 --- a/modules/debug/include/debug/assertions.hpp +++ b/modules/debug/include/debug/assertions.hpp @@ -12,13 +12,25 @@ struct FailedAssertion: std::exception } }; -#define lt_assert(x, ...) \ - { \ - if (!(x)) \ - { \ - log_crt(__VA_ARGS__); \ - throw ::lt::FailedAssertion(__FILE__, __LINE__); \ - } \ + +template +constexpr void lt_assert(Expression_T &&expression, std::format_string fmt, Args &&...args) +{ + if (!static_cast(expression)) + { + Logger::log(LogLvl::critical, fmt, std::forward(args)...); + throw ::lt::FailedAssertion(__FILE__, __LINE__); } +} + +template +constexpr void lt_assert(Expression_T &&expression, const char *message) +{ + if (!static_cast(expression)) + { + Logger::log(LogLvl::critical, message); + throw ::lt::FailedAssertion(__FILE__, __LINE__); + } +} } // namespace lt diff --git a/modules/engine/include/engine/core/entrypoint.hpp b/modules/engine/include/engine/core/entrypoint.hpp index fb02c0a..1d21612 100644 --- a/modules/engine/include/engine/core/entrypoint.hpp +++ b/modules/engine/include/engine/core/entrypoint.hpp @@ -12,8 +12,8 @@ try application = lt::create_application(); - lt_assert(application, "Failed to create application"); - lt_assert(application->sanity_check(), "Failed to verify the sanity of the application"); + lt::lt_assert(application, "Failed to create application"); + lt::lt_assert(application->sanity_check(), "Failed to verify the sanity of the application"); application->game_loop(); diff --git a/modules/logger/include/logger/logger.hpp b/modules/logger/include/logger/logger.hpp index 776de55..9bd9bf3 100644 --- a/modules/logger/include/logger/logger.hpp +++ b/modules/logger/include/logger/logger.hpp @@ -53,6 +53,12 @@ public: ); } + void static log(LogLvl lvl, const char *message) + { + instance().spd_logger->log((spdlog::level::level_enum)lvl, message); + } + + private: Logger();