Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
501}502if (m_print_to_file && !ratelimit) {503assert(m_fileout != nullptr);505// reopen the log file, if requested506if (m_reopen_file) {507m_reopen_file = false;508FILE* new_fileout = fsbridge::fopen(m_file_path, "a");509if (new_fileout) {510setbuf(new_fileout, nullptr); // unbuffered511fclose(m_fileout);512m_fileout = new_fileout;513}514}515FileWriteStr(str_prefixed, m_fileout);516}517}519void BCLog::Logger::ShrinkDebugFile()520{521STDLOCK(m_cs);523// Amount of debug.log to save at end when shrinking (must fit in memory)524constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;526assert(!m_file_path.empty());528// Scroll debug.log if it's getting too big529FILE* file = fsbridge::fopen(m_file_path, "r");531// Special files (e.g. device nodes) may not have a size.532size_t log_size = 0;533try {534log_size = fs::file_size(m_file_path);535} catch (const fs::filesystem_error&) {}537// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE538// trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes539if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))540{541// Restart the file with some of the end542std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);543if (fseek(file, -((long)vch.size()), SEEK_END)) {544// LogWarning, except with m_cs held545LogPrint_({546.category = BCLog::ALL,547.level = Level::Warning,548.should_ratelimit = true,549.source_loc = SourceLocation{__func__},550.message = "Failed to shrink debug log file: fseek(...) failed",551});552fclose(file);553return;554}555int nBytes = fread(vch.data(), 1, vch.size(), file);556fclose(file);558file = fsbridge::fopen(m_file_path, "w");559if (file)560{561fwrite(vch.data(), 1, nBytes, file);562fclose(file);563}564}565else if (file != nullptr)566fclose(file);567}569void BCLog::LogRateLimiter::Reset()570{571decltype(m_source_locations) source_locations;572{573STDLOCK(m_mutex);574source_locations.swap(m_source_locations);575m_suppression_active = false;576}577for (const auto& [source_loc, stats] : source_locations) {578if (stats.m_dropped_bytes == 0) continue;579LogWarning(util::log::NO_RATE_LIMIT,580"Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.",581source_loc.file_name(), source_loc.line(), source_loc.function_name_short(),582stats.m_dropped_bytes, Ticks<std::chrono::seconds>(m_reset_window));583}584}586bool BCLog::LogRateLimiter::Stats::Consume(uint64_t bytes)587{588if (bytes > m_available_bytes) {589m_dropped_bytes += bytes;590m_available_bytes = 0;591return false;592}594m_available_bytes -= bytes;595return true;596}598bool BCLog::Logger::SetLogLevel(std::string_view level_str)599{600const auto level = GetLogLevel(level_str);601if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;602m_log_level = level.value();603return true;604}606bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)607{608const auto flag{GetLogCategory(category_str)};609if (!flag) return false;611const auto level = GetLogLevel(level_str);612if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;613if (*flag == BCLog::NONE) return true;615STDLOCK(m_cs);616m_category_log_levels[*flag] = level.value();617return true;618}620bool util::log::ShouldDebugLog(Category category)621{622return LogInstance().WillLogCategoryLevel(static_cast<BCLog::LogFlags>(category), util::log::Level::Debug);623}625bool util::log::ShouldTraceLog(Category category)626{627return LogInstance().WillLogCategoryLevel(static_cast<BCLog::LogFlags>(category), util::log::Level::Trace);628}630void util::log::Log(util::log::Entry entry)631{632BCLog::Logger& logger{LogInstance()};633if (logger.Enabled()) {634logger.LogPrint(std::move(entry));635}636}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.