logging: replace FormatLogStrInPlace with Format

Return a new string built left-to-right instead of mutating one
with repeated insert(0, ...). Take a const log::Entry ref since
both call sites now have an Entry available. Also move
LogEscapeMessage inside so callers don't need to pre-escape.
This commit is contained in:
stickies-v
2026-03-19 12:27:06 +00:00
parent 3b92ec2036
commit c5ec2d5313
2 changed files with 15 additions and 16 deletions

View File

@@ -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');
str.insert(0, GetLogPrefix(category, level));
if (m_log_sourcelocations) {
str.insert(0, strprintf("[%s:%d] [%s] ", RemovePrefixView(source_loc.file_name(), "./"), source_loc.line(), source_loc.function_name_short()));
}
std::string result{LogTimestampStr(entry.timestamp, entry.mocktime)};
if (m_log_threadnames) {
str.insert(0, strprintf("[%s] ", (threadname.empty() ? "unknown" : threadname)));
result += strprintf("[%s] ", (entry.thread_name.empty() ? "unknown" : entry.thread_name));
}
str.insert(0, LogTimestampStr(now, mocktime));
if (m_log_sourcelocations) {
result += strprintf("[%s:%d] [%s] ", RemovePrefixView(entry.source_loc.file_name(), "./"), entry.source_loc.line(), entry.source_loc.function_name_short());
}
result += GetLogPrefix(static_cast<LogFlags>(entry.category), entry.level);
result += LogEscapeMessage(entry.message);
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)};

View File

@@ -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;