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 @@ -72,7 +72,7 @@ SetupEnvironment(); // don't want to write to debug.log file - GetLogger().fPrintToDebugLog = false; + 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); diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -291,7 +291,7 @@ } void HandleSIGHUP(int) { - GetLogger().fReopenDebugLog = true; + GetLogger().m_reopen_file = true; } void OnRPCStarted() { @@ -1255,10 +1255,10 @@ void InitLogging() { BCLog::Logger &logger = GetLogger(); - logger.fPrintToConsole = gArgs.GetBoolArg("-printtoconsole", false); - logger.fLogTimestamps = + logger.m_print_to_console = gArgs.GetBoolArg("-printtoconsole", false); + logger.m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS); - logger.fLogTimeMicros = + logger.m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS); fLogIPs = gArgs.GetBoolArg("-logips", DEFAULT_LOGIPS); @@ -1765,11 +1765,11 @@ logger.ShrinkDebugFile(); } - if (logger.fPrintToDebugLog) { + if (logger.m_print_to_file) { logger.OpenDebugLog(); } - if (!logger.fLogTimestamps) { + if (!logger.m_log_timestamps) { LogPrintf("Startup time: %s\n", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime())); } diff --git a/src/logging.h b/src/logging.h --- a/src/logging.h +++ b/src/logging.h @@ -7,6 +7,9 @@ #ifndef BITCOIN_LOGGING_H #define BITCOIN_LOGGING_H +#include "fs.h" +#include "tinyformat.h" + #include #include #include @@ -49,32 +52,32 @@ class Logger { private: - FILE *fileout = nullptr; - std::mutex mutexDebugLog; - std::list vMsgsBeforeOpenLog; + FILE *m_fileout = nullptr; + std::mutex m_file_mutex; + std::list m_msgs_before_open; /** - * fStartedNewLine is a state variable that will suppress printing of the + * m_started_new_line is a state variable that will suppress printing of the * timestamp when multiple calls are made that don't end in a newline. */ - std::atomic_bool fStartedNewLine{true}; + std::atomic_bool m_started_new_line{true}; /** * Log categories bitfield. Leveldb/libevent need special handling if their * flags are changed at runtime. */ - std::atomic logCategories{0}; + std::atomic m_categories{0}; std::string LogTimestampStr(const std::string &str); public: - bool fPrintToConsole = false; - bool fPrintToDebugLog = true; + bool m_print_to_console = false; + bool m_print_to_file = true; - bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS; - bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS; + bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS; + bool m_log_time_micros = DEFAULT_LOGTIMEMICROS; - std::atomic fReopenDebugLog{false}; + std::atomic m_reopen_file{false}; ~Logger(); @@ -85,7 +88,9 @@ void ShrinkDebugFile(); void EnableCategory(LogFlags category); + bool EnableCategory(const std::string &str); void DisableCategory(LogFlags category); + bool DisableCategory(const std::string &str); /** Return true if log accepts specified category */ bool WillLogCategory(LogFlags category) const; diff --git a/src/logging.cpp b/src/logging.cpp --- a/src/logging.cpp +++ b/src/logging.cpp @@ -33,18 +33,18 @@ } void BCLog::Logger::OpenDebugLog() { - std::lock_guard scoped_lock(mutexDebugLog); + std::lock_guard scoped_lock(m_file_mutex); - assert(fileout == nullptr); + assert(m_fileout == nullptr); fs::path pathDebug = GetDataDir() / "debug.log"; - fileout = fsbridge::fopen(pathDebug, "a"); - if (fileout) { + m_fileout = fsbridge::fopen(pathDebug, "a"); + if (m_fileout) { // Unbuffered. - setbuf(fileout, nullptr); + setbuf(m_fileout, nullptr); // Dump buffered messages from before we opened the log. - while (!vMsgsBeforeOpenLog.empty()) { - FileWriteStr(vMsgsBeforeOpenLog.front(), fileout); - vMsgsBeforeOpenLog.pop_front(); + while (!m_msgs_before_open.empty()) { + FileWriteStr(m_msgs_before_open.front(), m_fileout); + m_msgs_before_open.pop_front(); } } } @@ -56,6 +56,7 @@ const CLogCategoryDesc LogCategories[] = { {BCLog::NONE, "0"}, + {BCLog::NONE, "none"}, {BCLog::NET, "net"}, {BCLog::TOR, "tor"}, {BCLog::MEMPOOL, "mempool"}, @@ -111,30 +112,30 @@ } BCLog::Logger::~Logger() { - if (fileout) { - fclose(fileout); + if (m_fileout) { + fclose(m_fileout); } } std::string BCLog::Logger::LogTimestampStr(const std::string &str) { std::string strStamped; - if (!fLogTimestamps) return str; + if (!m_log_timestamps) return str; - if (fStartedNewLine) { + if (m_started_new_line) { int64_t nTimeMicros = GetLogTimeMicros(); strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros / 1000000); - if (fLogTimeMicros) + if (m_log_time_micros) strStamped += strprintf(".%06d", nTimeMicros % 1000000); strStamped += ' ' + str; } else strStamped = str; if (!str.empty() && str[str.size() - 1] == '\n') - fStartedNewLine = true; + m_started_new_line = true; else - fStartedNewLine = false; + m_started_new_line = false; return strStamped; } @@ -145,29 +146,29 @@ std::string strTimestamped = LogTimestampStr(str); - if (fPrintToConsole) { + if (m_print_to_console) { // Print to console. ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); fflush(stdout); - } else if (fPrintToDebugLog) { - std::lock_guard scoped_lock(mutexDebugLog); + } else if (m_print_to_file) { + std::lock_guard scoped_lock(m_file_mutex); // Buffer if we haven't opened the log yet. - if (fileout == nullptr) { + if (m_fileout == nullptr) { ret = strTimestamped.length(); - vMsgsBeforeOpenLog.push_back(strTimestamped); + m_msgs_before_open.push_back(strTimestamped); } else { // Reopen the log file, if requested. - if (fReopenDebugLog) { - fReopenDebugLog = false; + if (m_reopen_file) { + m_reopen_file = false; fs::path pathDebug = GetDataDir() / "debug.log"; - if (fsbridge::freopen(pathDebug, "a", fileout) != nullptr) { + if (fsbridge::freopen(pathDebug, "a", m_fileout) != nullptr) { // unbuffered. - setbuf(fileout, nullptr); + setbuf(m_fileout, nullptr); } } - ret = FileWriteStr(strTimestamped, fileout); + ret = FileWriteStr(strTimestamped, m_fileout); } } return ret; @@ -199,11 +200,25 @@ } void BCLog::Logger::EnableCategory(LogFlags category) { - logCategories |= category; + m_categories |= category; +} + +bool BCLog::Logger::EnableCategory(const std::string &str) { + BCLog::LogFlags flag; + if (!GetLogCategory(flag, str)) return false; + EnableCategory(flag); + return true; } void BCLog::Logger::DisableCategory(LogFlags category) { - logCategories &= ~category; + m_categories &= ~category; +} + +bool BCLog::Logger::DisableCategory(const std::string &str) { + BCLog::LogFlags flag; + if (!GetLogCategory(flag, str)) return false; + DisableCategory(flag); + return true; } bool BCLog::Logger::WillLogCategory(LogFlags category) const { @@ -215,9 +230,9 @@ return true; } - return (logCategories.load(std::memory_order_relaxed) & category) != 0; + return (m_categories.load(std::memory_order_relaxed) & category) != 0; } bool BCLog::Logger::DefaultShrinkDebugFile() const { - return logCategories != BCLog::NONE; + return m_categories != BCLog::NONE; } 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 @@ -61,7 +61,7 @@ InitScriptExecutionCache(); // Don't want to write to debug.log file. - GetLogger().fPrintToDebugLog = false; + GetLogger().m_print_to_file = false; fCheckBlockIndex = true; SelectParams(chainName);