diff options
| author | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2025-12-13 11:43:06 +0200 |
|---|---|---|
| committer | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2025-12-13 13:21:47 +0200 |
| commit | d5fc87547450aa929f0292e32bb4453ee5b4b25a (patch) | |
| tree | b1356d9c2541e7b9b34c55dd0edcb5bd69bb4652 | |
| parent | f4136723f00d7ebba1e7ea48f4708e1bf5133d76 (diff) | |
utils: rework logging functions
Use a general mu_log which does the formatting, and specific mu_debug, mu_info
etc., to call it.
| -rw-r--r-- | lib/utils/mu-utils.hh | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index 11c0d9a..c465689 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<typename...T> +std::string mu_format(fmt::format_string<T...> frm, T&&... args) noexcept { + return fmt::format(frm, std::forward<T>(args)...); +} + /* * Debug/error/warning logging * @@ -64,36 +73,40 @@ constexpr const auto SepaChar2 = '\xff'; */ template<typename...T> +void mu_log(GLogLevelFlags level, fmt::format_string<T...> frm, T&&... args) noexcept { + g_log("mu", level, "%s", mu_format(frm, std::forward<T>(args)...).c_str()); +} + +template<typename...T> +void mu_none(fmt::format_string<T...>, T&&...) noexcept { + // ignore +} + +template<typename...T> void mu_debug(fmt::format_string<T...> frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_DEBUG, "%s", - fmt::format(frm, std::forward<T>(args)...).c_str()); + mu_log(G_LOG_LEVEL_DEBUG, frm, std::forward<T>(args)...); } template<typename...T> void mu_info(fmt::format_string<T...> frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_INFO, "%s", - fmt::format(frm, std::forward<T>(args)...).c_str()); + mu_log(G_LOG_LEVEL_INFO, frm, std::forward<T>(args)...); } template<typename...T> void mu_message(fmt::format_string<T...> frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_MESSAGE, "%s", - fmt::format(frm, std::forward<T>(args)...).c_str()); + mu_log(G_LOG_LEVEL_MESSAGE, frm, std::forward<T>(args)...); } template<typename...T> void mu_warning(fmt::format_string<T...> frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_WARNING, "%s", - fmt::format(frm, std::forward<T>(args)...).c_str()); + mu_log(G_LOG_LEVEL_WARNING, frm, std::forward<T>(args)...); } -/* LCOV_EXCL_START*/ template<typename...T> void mu_critical(fmt::format_string<T...> frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_CRITICAL, "%s", - fmt::format(frm, std::forward<T>(args)...).c_str()); + mu_log(G_LOG_LEVEL_CRITICAL, frm, std::forward<T>(args)...); } template<typename...T> void mu_error(fmt::format_string<T...> frm, T&&... args) noexcept { - g_log("mu", G_LOG_LEVEL_ERROR, "%s", - fmt::format(frm, std::forward<T>(args)...).c_str()); + mu_log(G_LOG_LEVEL_ERROR, frm, std::forward<T>(args)...); } + /* LCOV_EXCL_STOP*/ /* @@ -118,8 +131,18 @@ void mu_printerrln(fmt::format_string<T...> frm, T&&... args) noexcept { fmt::println(stderr, frm, std::forward<T>(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<typename...T> void mu_print(std::ostream& os, fmt::format_string<T...> frm, T&&... args) noexcept { fmt::print(os, frm, std::forward<T>(args)...); @@ -129,14 +152,6 @@ void mu_println(std::ostream& os, fmt::format_string<T...> frm, T&&... args) noe fmt::println(os, frm, std::forward<T>(args)...); } -/* - * Fprmatting - */ -template<typename...T> -std::string mu_format(fmt::format_string<T...> frm, T&&... args) noexcept { - return fmt::format(frm, std::forward<T>(args)...); -} - template<typename Range> auto mu_join(Range&& range, std::string_view sepa) { return fmt::join(std::forward<Range>(range), sepa); |
