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

Code before:
        fclose(file);
}

void BCLog::LogRateLimiter::Reset()
{
    decltype(m_source_locations) source_locations;
    {
        StdLockGuard scoped_lock(m_mutex);
        source_locations.swap(m_source_locations);
        m_suppression_active = false;
    }
    for (const auto& [source_loc, stats] : source_locations) {
        if (stats.dropped_bytes == 0) continue;
        LogPrintLevel_(
            LogFlags::ALL, Level::Info, /*should_ratelimit=*/false,
            "Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.\n",
            source_loc.file_name(), source_loc.line(), source_loc.function_name(),
            stats.dropped_bytes, Ticks<std::chrono::seconds>(m_reset_window));
    }
}

bool BCLog::LogRateLimiter::Stats::Consume(uint64_t bytes)
{
    if (bytes > available_bytes) {
        dropped_bytes += bytes;
        available_bytes = 0;
        return false;
    }

    available_bytes -= bytes;
    return true;
}

bool BCLog::Logger::SetLogLevel(std::string_view level_str)
{
    const auto level = GetLogLevel(level_str);
    if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
    m_log_level = level.value();
    return true;
}

bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)
{
    BCLog::LogFlags flag;
    if (!GetLogCategory(flag, category_str)) return false;

    const auto level = GetLogLevel(level_str);
    if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;

    StdLockGuard scoped_lock(m_cs);
    m_category_log_levels[flag] = level.value();

Review comment (suggestion):
nit in 0716f0a69f37d611ead459eadf35a8c670e511f4 (doesn't matter much): Generally, when a function exists in a class, it is better to use the `m_` prefix. This would also make the diff smaller.  

Code after:

        file = fsbridge::fopen(m_file_path, "w");
        if (file)
        {
            fwrite(vch.data(), 1, nBytes, file);
            fclose(file);
        }
    }
    else if (file != nullptr)
        fclose(file);
}

void BCLog::LogRateLimiter::Reset()
{
    decltype(m_source_locations) source_locations;
    {
        StdLockGuard scoped_lock(m_mutex);
        source_locations.swap(m_source_locations);
        m_suppression_active = false;
    }
    for (const auto& [source_loc, stats] : source_locations) {
        if (stats.m_dropped_bytes == 0) continue;
        LogPrintLevel_(
            LogFlags::ALL, Level::Warning, /*should_ratelimit=*/false,
            "Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.",
            source_loc.file_name(), source_loc.line(), source_loc.function_name(),
            stats.m_dropped_bytes, Ticks<std::chrono::seconds>(m_reset_window));
    }
}

bool BCLog::LogRateLimiter::Stats::Consume(uint64_t bytes)
{
    if (bytes > m_available_bytes) {
        m_dropped_bytes += bytes;
        m_available_bytes = 0;
        return false;
    }

    m_available_bytes -= bytes;
    return true;
}

bool BCLog::Logger::SetLogLevel(std::string_view level_str)
{
    const auto level = GetLogLevel(level_str);
    if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
    m_log_level = level.value();
    return true;
}

bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)