logging: replace FormatLogStrInPlace with Format
What changed, and why it matters
This commit is a straightforward internal code cleanup in Bitcoin Core's logging system. It replaces a function that modified a string by repeatedly inserting text at the front with a function that builds the same string left-to-right and returns it. There is no security-relevant change here.
No security action required. Treat as normal refactoring/code-quality improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors BCLog::Logger::FormatLogStrInPlace into BCLog::Logger::Format. The old implementation used std::string::insert(0, …) repeatedly to prepend timestamp, thread name, source location, and log prefix to an already-escaped message. The new implementation constructs the result left-to-right starting with the timestamp, then optionally thread name, source location, prefix, escaped message, and trailing newline. Call sites are updated accordingly. The behavior is functionally equivalent; no input validation, escaping, or trust boundary changes are introduced.
Changed components
src/logging.cppsrc/logging.hInspect captured patch +13 / −14
diff --git a/src/logging.cpp b/src/logging.cpp
index e95375d8..141ffab6 100644
--- a/src/logging.cpp
+++ b/src/logging.cpp
@@ -85,8 +85,7 @@ bool BCLog::Logger::StartLogging()
}
while (!m_msgs_before_open.empty()) {
const auto& buflog = m_msgs_before_open.front();
- 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);
+ std::string s{Format(buflog)};
m_msgs_before_open.pop_front();
if (m_print_to_file) FileWriteStr(s, m_fileout);
@@ -410,21 +409,23 @@ BCLog::LogRateLimiter::Status BCLog::LogRateLimiter::Consume(
return status;
}
-void BCLog::Logger::FormatLogStrInPlace(std::string& str, BCLog::LogFlags category, BCLog::Level level, const SourceLocation& source_loc, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const
+std::string BCLog::Logger::Format(const util::log::Entry& entry) const
{
- if (!str.ends_with('\n')) str.push_back('\n');
+ std::string result{LogTimestampStr(entry.timestamp, entry.mocktime)};
- str.insert(0, GetLogPrefix(category, level));
+ if (m_log_threadnames) {
+ result += strprintf("[%s] ", (entry.thread_name.empty() ? "unknown" : entry.thread_name));
+ }
if (m_log_sourcelocations) {
- str.insert(0, strprintf("[%s:%d] [%s] ", RemovePrefixView(source_loc.file_name(), "./"), source_loc.line(), source_loc.function_name_short()));
+ result += strprintf("[%s:%d] [%s] ", RemovePrefixView(entry.source_loc.file_name(), "./"), entry.source_loc.line(), entry.source_loc.function_name_short());
}
- if (m_log_threadnames) {
- str.insert(0, strprintf("[%s] ", (threadname.empty() ? "unknown" : threadname)));
- }
+ result += GetLogPrefix(static_cast<LogFlags>(entry.category), entry.level);
+ result += LogEscapeMessage(entry.message);
- str.insert(0, LogTimestampStr(now, mocktime));
+ if (!result.ends_with('\n')) result += '\n';
+ return result;
}
void BCLog::Logger::LogPrint(util::log::Entry entry)
@@ -436,8 +437,6 @@ void BCLog::Logger::LogPrint(util::log::Entry entry)
// NOLINTNEXTLINE(misc-no-recursion)
void BCLog::Logger::LogPrint_(util::log::Entry entry)
{
- std::string str_prefixed = LogEscapeMessage(entry.message);
-
if (m_buffering) {
{
m_cur_buffer_memusage += MemUsage(entry);
@@ -457,7 +456,7 @@ void BCLog::Logger::LogPrint_(util::log::Entry entry)
return;
}
- FormatLogStrInPlace(str_prefixed, static_cast<LogFlags>(entry.category), entry.level, entry.source_loc, entry.thread_name, entry.timestamp, GetMockTime());
+ std::string str_prefixed{Format(entry)};
bool ratelimit{false};
if (entry.should_ratelimit && m_limiter) {
auto status{m_limiter->Consume(entry.source_loc, str_prefixed)};
diff --git a/src/logging.h b/src/logging.h
index 1d3f8900..952bb95a 100644
--- a/src/logging.h
+++ b/src/logging.h
@@ -149,7 +149,7 @@ namespace BCLog {
/** Log categories bitfield. */
std::atomic<CategoryMask> m_categories{BCLog::NONE};
- void FormatLogStrInPlace(std::string& str, LogFlags category, Level level, const SourceLocation& source_loc, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const;
+ std::string Format(const util::log::Entry& entry) const;
std::string LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const;
Why this scored 15/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.