diff --git a/modules/logger/logger.cppm b/modules/logger/logger.cppm index 95860da..826b0b4 100644 --- a/modules/logger/logger.cppm +++ b/modules/logger/logger.cppm @@ -2,7 +2,7 @@ export module logger; import preliminary; -namespace lt::log { +export namespace lt::log { /** Severity of a log message. */ enum class Level : u8 @@ -25,19 +25,25 @@ enum class Level : u8 /** Unrecoverable errors */ critical = 5, + /** + * Logs from the testing-framework. + * Highest so we still get them while turning off all logs from the code under test. + * + * @note: log::test does NOT include source_location + */ + test = 6, + /** No logging */ - off = 6, + off = 7, }; -namespace details { +auto min_severity = Level::trace; -inline auto thread_hash_id() noexcept -> u64 +auto set_min_severity(Level severity) { - return static_cast(std::hash {}(std::this_thread::get_id())); + min_severity = severity; } -} // namespace details - template struct [[maybe_unused]] print { @@ -48,6 +54,11 @@ struct [[maybe_unused]] print Args &&...arguments ) noexcept { + if (std::to_underlying(level) < std::to_underlying(min_severity)) + { + return; + } + constexpr auto to_string = [](Level level) { // clang-format off switch (level) @@ -75,13 +86,45 @@ struct [[maybe_unused]] print std::format(format, std::forward(arguments)...) ); } + + [[maybe_unused]] print( + Level level, + std::format_string format, + Args &&...arguments + ) noexcept + { + constexpr auto to_string = [](Level level) { + // clang-format off + switch (level) + { + using enum ::lt::log::Level; + case trace : return "\033[1;37m| trc |\033[0m"; + case debug : return "\033[1;36m| dbg |\033[0m"; + case info : return "\033[1;32m| inf |\033[0m"; + case warn : return "\033[1;33m| wrn |\033[0m"; + case error : return "\033[1;31m| err |\033[0m"; + case critical: return "\033[1;41m| crt |\033[0m"; + case test : return "\033[1;33m| test |\033[0m"; + case off : return ""; + } + // clang-format on + + std::unreachable(); + }; + + std::println( + "{} {}", + to_string(level), + std::format(format, std::forward(arguments)...) + ); + } }; template print(Level, const std::source_location &, std::format_string, Args &&...) noexcept -> print; -export template +template struct [[maybe_unused]] trace { [[maybe_unused]] trace( @@ -94,10 +137,10 @@ struct [[maybe_unused]] trace } }; -export template +template trace(std::format_string, Args &&...) noexcept -> trace; -export template +template struct [[maybe_unused]] debug { [[maybe_unused]] debug( @@ -110,10 +153,11 @@ struct [[maybe_unused]] debug } }; -export template +template debug(std::format_string, Args &&...) noexcept -> debug; -export template + +template struct [[maybe_unused]] info { [[maybe_unused]] info( @@ -126,10 +170,10 @@ struct [[maybe_unused]] info } }; -export template +template info(std::format_string, Args &&...) noexcept -> info; -export template +template struct [[maybe_unused]] warn { [[maybe_unused]] warn( @@ -142,10 +186,10 @@ struct [[maybe_unused]] warn } }; -export template +template warn(std::format_string, Args &&...) noexcept -> warn; -export template +template struct [[maybe_unused]] error { [[maybe_unused]] error( @@ -158,10 +202,10 @@ struct [[maybe_unused]] error } }; -export template +template error(std::format_string, Args &&...) noexcept -> error; -export template +template struct [[maybe_unused]] critical { [[maybe_unused]] critical( @@ -174,7 +218,13 @@ struct [[maybe_unused]] critical } }; -export template +template critical(std::format_string, Args &&...) noexcept -> critical; +template +void test(std::format_string format, Args &&...arguments) noexcept +{ + print(Level::test, format, std::forward(arguments)...); +} + } // namespace lt::log