diff --git a/src/logging.cpp b/src/logging.cpp index e95375d8706..141ffab6810 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(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(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(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 1d3f89008ac..952bb95a004 100644 --- a/src/logging.h +++ b/src/logging.h @@ -149,7 +149,7 @@ namespace BCLog { /** Log categories bitfield. */ std::atomic 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;