From d58f8994aadeaff12db3c9c809ad1760f3cd7421 Mon Sep 17 00:00:00 2001 From: light7734 Date: Thu, 31 Jul 2025 08:42:42 +0330 Subject: [PATCH] refactor(debug): fix & improve ensure --- modules/debug/public/assertions.hpp | 32 ++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/modules/debug/public/assertions.hpp b/modules/debug/public/assertions.hpp index 9d6f195..bb55f79 100644 --- a/modules/debug/public/assertions.hpp +++ b/modules/debug/public/assertions.hpp @@ -6,23 +6,31 @@ namespace lt { -struct FailedAssertion: std::exception +template +struct ensure { - FailedAssertion(const char *file, int line) + ensure( + Expression_T expression, + std::format_string fmt, + Args_T &&...args, + const std::source_location &location = std::source_location::current() + ) { - log_crt("Assertion failed in: {} (line {})", file, line); + if (!static_cast(expression)) + { + throw std::runtime_error { std::format( + "exception: {}\nlocation: {}:{}", + std::format(fmt, std::forward(args)...), + location.file_name(), + location.line() + ) }; + } } }; -template -constexpr void ensure(Expression_T &&expression, const char *message) -{ - if (!static_cast(expression)) - { - Logger::log(LogLvl::critical, message); - throw ::lt::FailedAssertion(__FILE__, __LINE__); - } -} +template +ensure(Expression_T, std::format_string, Args_T &&...) + -> ensure; } // namespace lt