From 3b92ec20362b0a109b39da55289e41229001c9d4 Mon Sep 17 00:00:00 2001 From: stickies-v Date: Fri, 13 Feb 2026 13:04:15 +0000 Subject: [PATCH] logging: replace BufferedLog with log::Entry Avoids duplication and ensures timestamp and mocktime are captured at the same time. --- src/logging.cpp | 26 ++++++++------------------ src/logging.h | 12 +----------- src/util/log.h | 1 + 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 78b34f38d1b..e95375d8706 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -85,8 +85,8 @@ bool BCLog::Logger::StartLogging() } while (!m_msgs_before_open.empty()) { const auto& buflog = m_msgs_before_open.front(); - std::string s{buflog.str}; - FormatLogStrInPlace(s, buflog.category, buflog.level, buflog.source_loc, buflog.threadname, buflog.now, buflog.mocktime); + std::string s{buflog.message}; + FormatLogStrInPlace(s, static_cast(buflog.category), buflog.level, buflog.source_loc, buflog.thread_name, buflog.timestamp, buflog.mocktime); m_msgs_before_open.pop_front(); if (m_print_to_file) FileWriteStr(s, m_fileout); @@ -372,11 +372,11 @@ std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level l return s; } -static size_t MemUsage(const BCLog::Logger::BufferedLog& buflog) +static size_t MemUsage(const util::log::Entry& log) { - return memusage::DynamicUsage(buflog.str) + - memusage::DynamicUsage(buflog.threadname) + - memusage::MallocUsage(sizeof(memusage::list_node)); + return memusage::DynamicUsage(log.message) + + memusage::DynamicUsage(log.thread_name) + + memusage::MallocUsage(sizeof(memusage::list_node)); } BCLog::LogRateLimiter::LogRateLimiter(uint64_t max_bytes, std::chrono::seconds reset_window) @@ -440,18 +440,8 @@ void BCLog::Logger::LogPrint_(util::log::Entry entry) if (m_buffering) { { - BufferedLog buf{ - .now = entry.timestamp, - .mocktime = GetMockTime(), - .str = std::move(str_prefixed), - .threadname = std::move(entry.thread_name), - .source_loc = entry.source_loc, - .category = static_cast(entry.category), - .level = entry.level, - }; - (void)std::move(entry); - m_cur_buffer_memusage += MemUsage(buf); - m_msgs_before_open.push_back(std::move(buf)); + m_cur_buffer_memusage += MemUsage(entry); + m_msgs_before_open.push_back(std::move(entry)); } while (m_cur_buffer_memusage > m_max_buffer_memusage) { diff --git a/src/logging.h b/src/logging.h index c871a9d86a5..1d3f89008ac 100644 --- a/src/logging.h +++ b/src/logging.h @@ -126,21 +126,11 @@ namespace BCLog { class Logger { - public: - struct BufferedLog { - SystemClock::time_point now; - std::chrono::seconds mocktime; - std::string str, threadname; - SourceLocation source_loc; - LogFlags category; - Level level; - }; - private: mutable StdMutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected FILE* m_fileout GUARDED_BY(m_cs) = nullptr; - std::list m_msgs_before_open GUARDED_BY(m_cs); + std::list m_msgs_before_open GUARDED_BY(m_cs); bool m_buffering GUARDED_BY(m_cs) = true; //!< Buffer messages before logging can be started. size_t m_max_buffer_memusage GUARDED_BY(m_cs){DEFAULT_MAX_LOG_BUFFER}; size_t m_cur_buffer_memusage GUARDED_BY(m_cs){0}; diff --git a/src/util/log.h b/src/util/log.h index 38214f967cc..9394161f61d 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -56,6 +56,7 @@ struct Entry { Level level; bool should_ratelimit{false}; //!< Hint for consumers if this entry should be ratelimited SystemClock::time_point timestamp{SystemClock::now()}; + std::chrono::seconds mocktime{GetMockTime()}; std::string thread_name{util::ThreadGetInternalName()}; SourceLocation source_loc; std::string message;