From f66b58cd419190aaf42124e8407d4dda7139e13e Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 13 Dec 2025 11:43:06 +0200 Subject: [PATCH] utils: rework logging functions Use a general mu_log which does the formatting, and specific mu_debug, mu_info etc., to call it. --- lib/utils/mu-utils.hh | 59 +++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index 11c0d9a6..c4656890 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -56,6 +56,15 @@ constexpr const auto SepaChar2 = '\xff'; * system. We wrap so perhaps at some point (C++23?) we can use std:: instead. */ + +/* + * Fprmatting + */ +template +std::string mu_format(fmt::format_string frm, T&&... args) noexcept { + return fmt::format(frm, std::forward(args)...); +} + /* * Debug/error/warning logging * @@ -63,37 +72,41 @@ constexpr const auto SepaChar2 = '\xff'; * when the formatting fails (ie. a bug) */ +template +void mu_log(GLogLevelFlags level, fmt::format_string frm, T&&... args) noexcept { + g_log("mu", level, "%s", mu_format(frm, std::forward(args)...).c_str()); +} + +template +void mu_none(fmt::format_string, T&&...) noexcept { + // ignore +} + template void mu_debug(fmt::format_string frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_DEBUG, "%s", - fmt::format(frm, std::forward(args)...).c_str()); + mu_log(G_LOG_LEVEL_DEBUG, frm, std::forward(args)...); } template void mu_info(fmt::format_string frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_INFO, "%s", - fmt::format(frm, std::forward(args)...).c_str()); + mu_log(G_LOG_LEVEL_INFO, frm, std::forward(args)...); } template void mu_message(fmt::format_string frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_MESSAGE, "%s", - fmt::format(frm, std::forward(args)...).c_str()); + mu_log(G_LOG_LEVEL_MESSAGE, frm, std::forward(args)...); } template void mu_warning(fmt::format_string frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_WARNING, "%s", - fmt::format(frm, std::forward(args)...).c_str()); + mu_log(G_LOG_LEVEL_WARNING, frm, std::forward(args)...); } -/* LCOV_EXCL_START*/ template void mu_critical(fmt::format_string frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_CRITICAL, "%s", - fmt::format(frm, std::forward(args)...).c_str()); + mu_log(G_LOG_LEVEL_CRITICAL, frm, std::forward(args)...); } template void mu_error(fmt::format_string frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_ERROR, "%s", - fmt::format(frm, std::forward(args)...).c_str()); + mu_log(G_LOG_LEVEL_ERROR, frm, std::forward(args)...); } + /* LCOV_EXCL_STOP*/ /* @@ -118,8 +131,18 @@ void mu_printerrln(fmt::format_string frm, T&&... args) noexcept { fmt::println(stderr, frm, std::forward(args)...); } +// null-stream +class NullStream : public std::ostream { +public: + NullStream() : std::ostream(&buf_) {} +private: + struct NullBuffer : public std::streambuf { + int overflow(int c) override { return c; } + }; + NullBuffer buf_; +}; -/* stream */ +/* stream print */ template void mu_print(std::ostream& os, fmt::format_string frm, T&&... args) noexcept { fmt::print(os, frm, std::forward(args)...); @@ -129,14 +152,6 @@ void mu_println(std::ostream& os, fmt::format_string frm, T&&... args) noe fmt::println(os, frm, std::forward(args)...); } -/* - * Fprmatting - */ -template -std::string mu_format(fmt::format_string frm, T&&... args) noexcept { - return fmt::format(frm, std::forward(args)...); -} - template auto mu_join(Range&& range, std::string_view sepa) { return fmt::join(std::forward(range), sepa);