diff --git a/src/common/logging/log.cpp b/src/common/logging/log.cpp index a53b526..27bbe48 100644 --- a/src/common/logging/log.cpp +++ b/src/common/logging/log.cpp @@ -178,6 +178,11 @@ Direction GetDirection() { return g_direction; } +bool IsSilent() { + // Before init LOGF must keep writing to stdout, so report non-silent. + return g_initialized && g_direction == Direction::Silent; +} + void Write(std::string_view text) { WriteImpl(text); } diff --git a/src/common/logging/log.h b/src/common/logging/log.h index babfb24..3ce29cf 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -15,6 +15,7 @@ KYTY_SUBSYSTEM_DEFINE(Log); enum class Direction { Silent, Console, File }; Direction GetDirection(); +bool IsSilent(); void Write(std::string_view text); void Write(fmt::text_style style, std::string_view text); void WriteFatal(std::string_view text); @@ -41,8 +42,18 @@ inline constexpr auto BrightWhite = fmt::fg(fmt::terminal_color::bright_white) } // namespace Log // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) -#define LOGF(...) ::Log::Write(::fmt::sprintf(__VA_ARGS__)) +#define LOGF(...) \ + do { \ + if (!::Log::IsSilent()) { \ + ::Log::Write(::fmt::sprintf(__VA_ARGS__)); \ + } \ + } while (false) // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) -#define LOGF_COLOR(style, ...) ::Log::Write((style), ::fmt::sprintf(__VA_ARGS__)) +#define LOGF_COLOR(style, ...) \ + do { \ + if (!::Log::IsSilent()) { \ + ::Log::Write((style), ::fmt::sprintf(__VA_ARGS__)); \ + } \ + } while (false) #endif /* KYTY_COMMON_LOGGING_LOG_H_ */