logging: replace BufferedLog with log::Entry
What changed, and why it matters
This is a small internal code cleanup in Bitcoin Core's logging system. It removes a duplicate log-entry structure and makes sure the real timestamp and the simulated 'mocktime' are recorded at the same instant. There is no indication this fixes a security bug or introduces a vulnerability.
No security action required. Treat as normal code-quality refactoring.
Security signals we found
No security-relevant signals present in commit message or diff
Refactoring/cleanup change only
No boundary, input, or cryptographic changes
Evidence from the diff
The commit refactors BCLog::Logger by replacing the local BCLog::Logger::BufferedLog struct with the existing util::log::Entry type. It adds a mocktime field to util::log::Entry and captures both timestamp and mocktime together when the entry is created, rather than capturing mocktime later when the buffered message is flushed. This eliminates duplicated fields and avoids a potential inconsistency between timestamp and mocktime. The change is purely structural and does not alter log output format, network behavior, consensus rules, or access controls.
Changed components
src/logging.cppsrc/logging.hsrc/util/log.hInspect captured patch +10 / −29
diff --git a/src/logging.cpp b/src/logging.cpp
index 78b34f38..e95375d8 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<BCLog::LogFlags>(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<BCLog::Logger::BufferedLog>));
+ return memusage::DynamicUsage(log.message) +
+ memusage::DynamicUsage(log.thread_name) +
+ memusage::MallocUsage(sizeof(memusage::list_node<util::log::Entry>));
}
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<LogFlags>(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 c871a9d8..1d3f8900 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<BufferedLog> m_msgs_before_open GUARDED_BY(m_cs);
+ std::list<util::log::Entry> 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 38214f96..9394161f 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;
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.