diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp --- a/src/bench/bench_bitcoin.cpp +++ b/src/bench/bench_bitcoin.cpp @@ -87,9 +87,6 @@ ECC_Start(); SetupEnvironment(); - // don't want to write to debug.log file - GetLogger().m_print_to_file = false; - int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS); std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER); std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING); diff --git a/src/httpserver.h b/src/httpserver.h --- a/src/httpserver.h +++ b/src/httpserver.h @@ -42,7 +42,7 @@ /** * Change logging level for libevent. Removes BCLog::LIBEVENT from - * logCategories if libevent doesn't support debug logging. + * log categories if libevent doesn't support debug logging. */ bool UpdateHTTPServerLogging(bool enable); diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -1359,15 +1359,18 @@ * careful about what global state you rely on here. */ void InitLogging() { + BCLog::Logger &logger = GetLogger(); + logger.m_print_to_file = !gArgs.IsArgNegated("-debuglogfile"); + logger.m_file_path = AbsPathForConfigVal( + gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)); + // Add newlines to the logfile to distinguish this execution from the last // one; called before console logging is set up, so this is only sent to // debug.log. LogPrintf("\n\n\n\n\n"); - BCLog::Logger &logger = GetLogger(); logger.m_print_to_console = gArgs.GetBoolArg( "-printtoconsole", !gArgs.GetBoolArg("-daemon", false)); - logger.m_print_to_file = !gArgs.IsArgNegated("-debuglogfile"); logger.m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS); logger.m_log_time_micros = @@ -1525,27 +1528,21 @@ categories.begin(), categories.end(), [](std::string cat) { return cat == "0" || cat == "none"; })) { for (const auto &cat : categories) { - BCLog::LogFlags flag = BCLog::NONE; - if (!GetLogCategory(flag, cat)) { + if (!GetLogger().EnableCategory(cat)) { InitWarning( strprintf(_("Unsupported logging category %s=%s."), "-debug", cat)); - continue; } - GetLogger().EnableCategory(flag); } } } // Now remove the logging categories which were explicitly excluded for (const std::string &cat : gArgs.GetArgs("-debugexclude")) { - BCLog::LogFlags flag = BCLog::NONE; - if (!GetLogCategory(flag, cat)) { + if (!GetLogger().DisableCategory(cat)) { InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat)); - continue; } - GetLogger().DisableCategory(flag); } // Check for -debugnet @@ -1844,8 +1841,8 @@ BCLog::Logger &logger = GetLogger(); if (logger.m_print_to_file) { - bool default_shrinkdebugfile = logger.DefaultShrinkDebugFile(); - if (gArgs.GetBoolArg("-shrinkdebugfile", default_shrinkdebugfile)) { + if (gArgs.GetBoolArg("-shrinkdebugfile", + logger.DefaultShrinkDebugFile())) { // Do this first since it both loads a bunch of debug.log into // memory, and because this needs to happen before any other // debug.log printing. @@ -1854,7 +1851,7 @@ if (!logger.OpenDebugLog()) { return InitError(strprintf("Could not open debug log file %s", - logger.GetDebugLogPath().string())); + logger.m_file_path.string())); } } diff --git a/src/logging.h b/src/logging.h --- a/src/logging.h +++ b/src/logging.h @@ -77,11 +77,12 @@ public: bool m_print_to_console = false; - bool m_print_to_file = true; + bool m_print_to_file = false; bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS; bool m_log_time_micros = DEFAULT_LOGTIMEMICROS; + fs::path m_file_path; std::atomic m_reopen_file{false}; ~Logger(); @@ -89,7 +90,6 @@ /** Send a string to the log output */ void LogPrintStr(const std::string &str); - fs::path GetDebugLogPath(); bool OpenDebugLog(); void ShrinkDebugFile(); diff --git a/src/logging.cpp b/src/logging.cpp --- a/src/logging.cpp +++ b/src/logging.cpp @@ -6,7 +6,6 @@ #include -#include #include bool fLogIPs = DEFAULT_LOGIPS; @@ -34,18 +33,13 @@ return fwrite(str.data(), 1, str.size(), fp); } -fs::path BCLog::Logger::GetDebugLogPath() { - fs::path logfile(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)); - return AbsPathForConfigVal(logfile); -} - bool BCLog::Logger::OpenDebugLog() { std::lock_guard scoped_lock(m_file_mutex); assert(m_fileout == nullptr); - fs::path pathDebug = GetDebugLogPath(); + assert(!m_file_path.empty()); - m_fileout = fsbridge::fopen(pathDebug, "a"); + m_fileout = fsbridge::fopen(m_file_path, "a"); if (!m_fileout) { return false; } @@ -197,8 +191,7 @@ // Reopen the log file, if requested. if (m_reopen_file) { m_reopen_file = false; - fs::path pathDebug = GetDebugLogPath(); - FILE *new_fileout = fsbridge::fopen(pathDebug, "a"); + FILE *new_fileout = fsbridge::fopen(m_file_path, "a"); if (new_fileout) { // unbuffered. setbuf(m_fileout, nullptr); @@ -214,14 +207,16 @@ void BCLog::Logger::ShrinkDebugFile() { // Amount of debug.log to save at end when shrinking (must fit in memory) constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; + + assert(!m_file_path.empty()); + // Scroll debug.log if it's getting too big. - fs::path pathLog = GetDebugLogPath(); - FILE *file = fsbridge::fopen(pathLog, "r"); + FILE *file = fsbridge::fopen(m_file_path, "r"); // Special files (e.g. device nodes) may not have a size. size_t log_size = 0; try { - log_size = fs::file_size(pathLog); + log_size = fs::file_size(m_file_path); } catch (boost::filesystem::filesystem_error &) { } @@ -238,7 +233,7 @@ int nBytes = fread(vch.data(), 1, vch.size(), file); fclose(file); - file = fsbridge::fopen(pathLog, "w"); + file = fsbridge::fopen(m_file_path, "w"); if (file) { fwrite(vch.data(), 1, nBytes, file); fclose(file); diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -51,9 +51,6 @@ InitSignatureCache(); InitScriptExecutionCache(); - // Don't want to write to debug.log file. - GetLogger().m_print_to_file = false; - fCheckBlockIndex = true; SelectParams(chainName); noui_connect();