From e8f9c37a3b4c9c88baddb556c4b33a4cbba1f614 Mon Sep 17 00:00:00 2001 From: Eugene Siegel Date: Fri, 18 Jul 2025 10:39:45 -0400 Subject: [PATCH] log: clean up LogPrintStr_ and Reset, prefix all logs with "[*]" when there are suppressions In LogPrintStr_: - remove an unnecessary BCLog since we are in the BCLog namespace. - remove an unnecessary \n when rate limiting is triggered since FormatLogStrInPlace will add it. - move the ratelimit bool into an else if block. - prefix all log lines with [*] when suppressions exist. Previously this was only done if should_ratelimit was true. In Reset: - remove an unnecessary \n since FormatLogStrInPlace will add it. - Change Level::Info to Level::Warning. --- src/logging.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index befac8a03f8..0cad2905047 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -455,24 +455,26 @@ void BCLog::Logger::LogPrintStr_(std::string_view str, std::source_location&& so bool ratelimit{false}; if (should_ratelimit && m_limiter) { auto status{m_limiter->Consume(source_loc, str_prefixed)}; - if (status == BCLog::LogRateLimiter::Status::NEWLY_SUPPRESSED) { + if (status == LogRateLimiter::Status::NEWLY_SUPPRESSED) { // NOLINTNEXTLINE(misc-no-recursion) LogPrintStr_(strprintf( "Excessive logging detected from %s:%d (%s): >%d bytes logged during " "the last time window of %is. Suppressing logging to disk from this " "source location until time window resets. Console logging " - "unaffected. Last log entry.\n", + "unaffected. Last log entry.", source_loc.file_name(), source_loc.line(), source_loc.function_name(), m_limiter->m_max_bytes, Ticks(m_limiter->m_reset_window)), std::source_location::current(), LogFlags::ALL, Level::Warning, /*should_ratelimit=*/false); // with should_ratelimit=false, this cannot lead to infinite recursion + } else if (status == LogRateLimiter::Status::STILL_SUPPRESSED) { + ratelimit = true; } - ratelimit = status == BCLog::LogRateLimiter::Status::STILL_SUPPRESSED; - // To avoid confusion caused by dropped log messages when debugging an issue, - // we prefix log lines with "[*]" when there are any suppressed source locations. - if (m_limiter->SuppressionsActive()) { - str_prefixed.insert(0, "[*] "); - } + } + + // To avoid confusion caused by dropped log messages when debugging an issue, + // we prefix log lines with "[*]" when there are any suppressed source locations. + if (m_limiter && m_limiter->SuppressionsActive()) { + str_prefixed.insert(0, "[*] "); } if (m_print_to_console) { @@ -552,8 +554,8 @@ void BCLog::LogRateLimiter::Reset() for (const auto& [source_loc, stats] : source_locations) { if (stats.m_dropped_bytes == 0) continue; LogPrintLevel_( - LogFlags::ALL, Level::Info, /*should_ratelimit=*/false, - "Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.\n", + LogFlags::ALL, Level::Warning, /*should_ratelimit=*/false, + "Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.", source_loc.file_name(), source_loc.line(), source_loc.function_name(), stats.m_dropped_bytes, Ticks(m_reset_window)); }