utils: rework logging functions

Use a general mu_log which does the formatting, and specific mu_debug, mu_info
etc., to call it.
This commit is contained in:
Dirk-Jan C. Binnema
2025-12-13 11:43:06 +02:00
committed by Seth Ladygo
parent 6099af565d
commit f66b58cd41

View File

@ -56,6 +56,15 @@ constexpr const auto SepaChar2 = '\xff';
* system. We wrap so perhaps at some point (C++23?) we can use std:: instead. * 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 * Debug/error/warning logging
* *
@ -63,37 +72,41 @@ constexpr const auto SepaChar2 = '\xff';
* when the formatting fails (ie. a bug) * when the formatting fails (ie. a bug)
*/ */
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> template<typename...T>
void mu_debug(fmt::format_string<T...> frm, T&&... args) noexcept { void mu_debug(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_DEBUG, "%s", mu_log(G_LOG_LEVEL_DEBUG, frm, std::forward<T>(args)...);
fmt::format(frm, std::forward<T>(args)...).c_str());
} }
template<typename...T> template<typename...T>
void mu_info(fmt::format_string<T...> frm, T&&... args) noexcept { void mu_info(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_INFO, "%s", mu_log(G_LOG_LEVEL_INFO, frm, std::forward<T>(args)...);
fmt::format(frm, std::forward<T>(args)...).c_str());
} }
template<typename...T> template<typename...T>
void mu_message(fmt::format_string<T...> frm, T&&... args) noexcept { void mu_message(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_MESSAGE, "%s", mu_log(G_LOG_LEVEL_MESSAGE, frm, std::forward<T>(args)...);
fmt::format(frm, std::forward<T>(args)...).c_str());
} }
template<typename...T> template<typename...T>
void mu_warning(fmt::format_string<T...> frm, T&&... args) noexcept { void mu_warning(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_WARNING, "%s", mu_log(G_LOG_LEVEL_WARNING, frm, std::forward<T>(args)...);
fmt::format(frm, std::forward<T>(args)...).c_str());
} }
/* LCOV_EXCL_START*/
template<typename...T> template<typename...T>
void mu_critical(fmt::format_string<T...> frm, T&&... args) noexcept { void mu_critical(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_CRITICAL, "%s", mu_log(G_LOG_LEVEL_CRITICAL, frm, std::forward<T>(args)...);
fmt::format(frm, std::forward<T>(args)...).c_str());
} }
template<typename...T> template<typename...T>
void mu_error(fmt::format_string<T...> frm, T&&... args) noexcept { void mu_error(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_ERROR, "%s", mu_log(G_LOG_LEVEL_ERROR, frm, std::forward<T>(args)...);
fmt::format(frm, std::forward<T>(args)...).c_str());
} }
/* LCOV_EXCL_STOP*/ /* 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)...); 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> template<typename...T>
void mu_print(std::ostream& os, fmt::format_string<T...> frm, T&&... args) noexcept { void mu_print(std::ostream& os, fmt::format_string<T...> frm, T&&... args) noexcept {
fmt::print(os, frm, std::forward<T>(args)...); 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)...); 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> template<typename Range>
auto mu_join(Range&& range, std::string_view sepa) { auto mu_join(Range&& range, std::string_view sepa) {
return fmt::join(std::forward<Range>(range), sepa); return fmt::join(std::forward<Range>(range), sepa);