Repository: bitcoin/bitcoin
File: src/logging.cpp (C++)

Code before:
    }

    str.insert(0, LogTimestampStr(now, mocktime));
}

void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
{
    StdLockGuard scoped_lock(m_cs);
    return LogPrintStr_(str, logging_function, source_file, source_line, category, level);
}

void BCLog::Logger::LogPrintStr_(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
{
    std::string str_prefixed = LogEscapeMessage(str);

    const bool starts_new_line = m_started_new_line;
    m_started_new_line = !str.empty() && str[str.size()-1] == '\n';

    if (m_buffering) {
        if (!starts_new_line) {
            if (!m_msgs_before_open.empty()) {
                m_msgs_before_open.back().str += str_prefixed;
                m_cur_buffer_memusage += str_prefixed.size();
                return;
            } else {
                // unlikely edge case; add a marker that something was trimmed
                str_prefixed.insert(0, "[...] ");
            }
        }

        {
            BufferedLog buf{
                .now=SystemClock::now(),
                .mocktime=GetMockTime(),
                .str=str_prefixed,
                .logging_function=logging_function,
                .source_file=source_file,
                .threadname=util::ThreadGetInternalName(),
                .source_line=source_line,
                .category=category,
                .level=level,
            };
            m_cur_buffer_memusage += MemUsage(buf);
            m_msgs_before_open.push_back(std::move(buf));
        }

        while (m_cur_buffer_memusage > m_max_buffer_memusage) {
            if (m_msgs_before_open.empty()) {
                m_cur_buffer_memusage = 0;
                break;
            }

Review comment (bug):
In commit "logging: Apply formatting to early log messages" (558df5c733d31456faf856d44f7037f41981d797)

Probably better to leave alone since this affects an edge case of an edge case, but just observing that this code could increment m_cur_buffer_memusage to count the usage more accurately.

Code after:

    str.insert(0, LogTimestampStr(now, mocktime));
}

void BCLog::Logger::LogPrintStr(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
{
    StdLockGuard scoped_lock(m_cs);
    return LogPrintStr_(str, logging_function, source_file, source_line, category, level);
}

void BCLog::Logger::LogPrintStr_(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
{
    std::string str_prefixed = LogEscapeMessage(str);

    const bool starts_new_line = m_started_new_line;
    m_started_new_line = !str.empty() && str[str.size()-1] == '\n';

    if (m_buffering) {
        if (!starts_new_line) {
            if (!m_msgs_before_open.empty()) {
                m_msgs_before_open.back().str += str_prefixed;
                m_cur_buffer_memusage += str_prefixed.size();
                return;
            } else {
                // unlikely edge case; add a marker that something was trimmed
                str_prefixed.insert(0, "[...] ");
            }
        }

        {
            BufferedLog buf{
                .now=SystemClock::now(),
                .mocktime=GetMockTime(),
                .str=str_prefixed,
                .logging_function=std::string(logging_function),
                .source_file=std::string(source_file),
                .threadname=util::ThreadGetInternalName(),
                .source_line=source_line,
                .category=category,
                .level=level,
            };
            m_cur_buffer_memusage += MemUsage(buf);
            m_msgs_before_open.push_back(std::move(buf));
        }

        while (m_cur_buffer_memusage > m_max_buffer_memusage) {
            if (m_msgs_before_open.empty()) {
                m_cur_buffer_memusage = 0;
                break;
            }
            m_cur_buffer_memusage -= MemUsage(m_msgs_before_open.front());