From 8c2d695c4a45bdd9378c7970b0fcba6e1efc01f9 Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Fri, 20 Apr 2018 01:11:44 -0700 Subject: [PATCH] util: Store debug log file path in BCLog::Logger member. This breaks the cyclic between logging and util. --- src/bench/bench_bitcoin.cpp | 1 - src/init.cpp | 6 ++++-- src/logging.cpp | 25 ++++++++++--------------- src/logging.h | 4 ++-- src/test/test_bitcoin.cpp | 1 - 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp index c1fbeb8d1..c1f333983 100644 --- a/src/bench/bench_bitcoin.cpp +++ b/src/bench/bench_bitcoin.cpp @@ -46,7 +46,6 @@ main(int argc, char** argv) RandomInit(); ECC_Start(); SetupEnvironment(); - g_logger->m_print_to_file = false; // don't want to write to debug.log file int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS); std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER); diff --git a/src/init.cpp b/src/init.cpp index 7bc2f6302..6423d8702 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -826,13 +826,15 @@ static std::string ResolveErrMsg(const char * const optname, const std::string& */ void InitLogging() { + g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile"); + g_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"); g_logger->m_print_to_console = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false)); - g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile"); g_logger->m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS); g_logger->m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS); @@ -1233,7 +1235,7 @@ bool AppInitMain() } if (!g_logger->OpenDebugLog()) { return InitError(strprintf("Could not open debug log file %s", - g_logger->GetDebugLogPath().string())); + g_logger->m_file_path.string())); } } diff --git a/src/logging.cpp b/src/logging.cpp index b7c682c94..10a3b1895 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -4,7 +4,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include const char * const DEFAULT_DEBUGLOGFILE = "debug.log"; @@ -30,20 +30,14 @@ static int FileWriteStr(const std::string &str, FILE *fp) return fwrite(str.data(), 1, str.size(), fp); } -fs::path BCLog::Logger::GetDebugLogPath() const -{ - 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; } @@ -228,8 +222,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str) // reopen the log file, if requested if (m_reopen_file) { m_reopen_file = false; - fs::path pathDebug = GetDebugLogPath(); - if (fsbridge::freopen(pathDebug,"a",m_fileout) != nullptr) + if (fsbridge::freopen(m_file_path,"a",m_fileout) != nullptr) setbuf(m_fileout, nullptr); // unbuffered } @@ -243,14 +236,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 &) {} // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE @@ -263,7 +258,7 @@ void BCLog::Logger::ShrinkDebugFile() 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); diff --git a/src/logging.h b/src/logging.h index 249d5debe..1f2be6016 100644 --- a/src/logging.h +++ b/src/logging.h @@ -77,11 +77,12 @@ namespace BCLog { 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}; /** Send a string to the log output */ @@ -90,7 +91,6 @@ namespace BCLog { /** Returns whether logs will be written to any output */ bool Enabled() const { return m_print_to_console || m_print_to_file; } - fs::path GetDebugLogPath() const; bool OpenDebugLog(); void ShrinkDebugFile(); diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index eea180488..fe816a6f7 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -47,7 +47,6 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName) SetupNetworking(); InitSignatureCache(); InitScriptExecutionCache(); - g_logger->m_print_to_file = false; // don't want to write to debug.log file fCheckBlockIndex = true; SelectParams(chainName); noui_connect();