From 95e8db0c5db8542c4105230e12b9fec30fa50e52 Mon Sep 17 00:00:00 2001 From: Barry Deeney Date: Tue, 7 May 2019 21:45:43 +0800 Subject: [PATCH] Replaced the prefix checking code with dedicated function calls from error( to insert in error.log --- src/qt/rpcconsole.cpp | 1 + src/util.cpp | 115 ++++++++++++++++++++++++++++++++---------- src/util.h | 15 +++--- 3 files changed, 97 insertions(+), 34 deletions(-) diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index b18926827..2d31d90f3 100755 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -321,6 +321,7 @@ RPCConsole::~RPCConsole() void RPCConsole::errorLogInitPos() { + error("LOADING errorLogInitPos"); // Check if we already have the file if (errorLogFile != NULL) { // Get a QFile instance diff --git a/src/util.cpp b/src/util.cpp index 5a8d0a1f0..fc21a9775 100755 --- a/src/util.cpp +++ b/src/util.cpp @@ -188,57 +188,66 @@ instance_of_cinit; */ static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT; +static boost::once_flag errorPrintInitFlag = BOOST_ONCE_INIT; /** * We use boost::call_once() to make sure mutexDebugLog and - * vMsgsBeforeOpenLog are initialized in a thread-safe manner. + * vMsgsBeforeOpenDebugLog are initialized in a thread-safe manner. * * NOTE: fileoutDebugLog, fileoutErrorLog, mutexDebugLog and sometimes - * vMsgsBeforeOpenLog are leaked on exit. This is ugly, but will be cleaned + * vMsgsBeforeOpenDebugLog are leaked on exit. This is ugly, but will be cleaned * up by the OS/libc. When the shutdown sequence is fully audited and * tested, explicit destruction of these objects can be implemented. */ static FILE* fileoutDebugLog = NULL; static FILE* fileoutErrorLog = NULL; static boost::mutex* mutexDebugLog = NULL; -static list *vMsgsBeforeOpenLog; +static boost::mutex* mutexErrorLog = NULL; +static list *vMsgsBeforeOpenDebugLog; +static list *vMsgsBeforeOpenErrorLog; static int FileWriteStr(const std::string &str, FILE *fp) { return fwrite(str.data(), 1, str.size(), fp); } -static int LogWriteStr(const std::string &str) +static int DebugLogWriteStr(const std::string &str) { - // Save the size of written data - int size = FileWriteStr(str, fileoutDebugLog); // write to debug log - - // What is our prefix - std::string prefix = "ERROR"; - - // Check if we need to write to error log - if (str.find(prefix) != string::npos) { - FileWriteStr(str, fileoutErrorLog); // write to error log - } + // Return the int from the write size + return FileWriteStr(str, fileoutDebugLog); // write to debug log +} +static int ErrorLogWriteStr(const std::string &str) +{ // Return the int from the write size - return size; + return FileWriteStr(str, fileoutErrorLog); // write to error log } static void DebugPrintInit() { assert(mutexDebugLog == NULL); mutexDebugLog = new boost::mutex(); - vMsgsBeforeOpenLog = new list; + vMsgsBeforeOpenDebugLog = new list; +} + +static void ErrorPrintInit() +{ + assert(mutexErrorLog == NULL); + mutexErrorLog = new boost::mutex(); + vMsgsBeforeOpenErrorLog = new list; } void OpenDebugLog() { boost::call_once(&DebugPrintInit, debugPrintInitFlag); - boost::mutex::scoped_lock scoped_lock(*mutexDebugLog); + boost::call_once(&ErrorPrintInit, errorPrintInitFlag); + boost::mutex::scoped_lock scoped_lock_debug(*mutexDebugLog); + boost::mutex::scoped_lock scoped_lock_error(*mutexErrorLog); assert(fileoutDebugLog == NULL); - assert(vMsgsBeforeOpenLog); + assert(fileoutErrorLog == NULL); + assert(vMsgsBeforeOpenDebugLog); + assert(vMsgsBeforeOpenErrorLog); // Open the debug log fileoutDebugLog = fopen(GetDebugLogPath().string().c_str(), "a"); @@ -249,13 +258,21 @@ void OpenDebugLog() if (fileoutErrorLog) setbuf(fileoutErrorLog, NULL); // unbuffered // dump buffered messages from before we opened the log - while (!vMsgsBeforeOpenLog->empty()) { - LogWriteStr(vMsgsBeforeOpenLog->front()); // write to log - vMsgsBeforeOpenLog->pop_front(); + while (!vMsgsBeforeOpenDebugLog->empty()) { + DebugLogWriteStr(vMsgsBeforeOpenDebugLog->front()); // write to log + vMsgsBeforeOpenDebugLog->pop_front(); + } + + // dump buffered messages from before we opened the log + while (!vMsgsBeforeOpenErrorLog->empty()) { + ErrorLogWriteStr(vMsgsBeforeOpenErrorLog->front()); // write to log + vMsgsBeforeOpenErrorLog->pop_front(); } - delete vMsgsBeforeOpenLog; - vMsgsBeforeOpenLog = NULL; + delete vMsgsBeforeOpenDebugLog; + delete vMsgsBeforeOpenErrorLog; + vMsgsBeforeOpenDebugLog = NULL; + vMsgsBeforeOpenErrorLog = NULL; } bool LogAcceptCategory(const char* category) @@ -284,6 +301,7 @@ bool LogAcceptCategory(const char* category) setCategories.count(string(category)) == 0) return false; } + return true; } @@ -326,7 +344,7 @@ boost::filesystem::path GetErrorLogPath() return GetDataDir() / "error.log"; } -int LogPrintStr(const std::string &str) +int DebugLogPrintStr(const std::string &str) { int ret = 0; // Returns total number of characters written static bool fStartedNewLine = true; @@ -342,13 +360,54 @@ int LogPrintStr(const std::string &str) else if (fPrintToDebugLog) { boost::call_once(&DebugPrintInit, debugPrintInitFlag); - boost::mutex::scoped_lock scoped_lock(*mutexDebugLog); + boost::mutex::scoped_lock scoped_lock_debug(*mutexDebugLog); // buffer if we haven't opened the log yet if (fileoutDebugLog == NULL) { - assert(vMsgsBeforeOpenLog); + assert(vMsgsBeforeOpenDebugLog); + ret = strTimestamped.length(); + vMsgsBeforeOpenDebugLog->push_back(strTimestamped); + } + else + { + // reopen the log file, if requested + if (fReopenLogFiles) { + fReopenLogFiles = false; + + // Open the log files + OpenDebugLog(); + } + + ret = DebugLogWriteStr(strTimestamped); + } + } + + return ret; +} + +int ErrorLogPrintStr(const std::string &str) +{ + int ret = 0; // Returns total number of characters written + static bool fStartedNewLine = true; + + string strTimestamped = LogTimestampStr(str, &fStartedNewLine); + + if (fPrintToConsole) + { + // print to console + ret = fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); + fflush(stdout); + } + else if (fPrintToDebugLog) + { + boost::call_once(&ErrorPrintInit, errorPrintInitFlag); + boost::mutex::scoped_lock scoped_lock_error(*mutexErrorLog); + + // buffer if we haven't opened the log yet + if (fileoutErrorLog == NULL) { + assert(vMsgsBeforeOpenErrorLog); ret = strTimestamped.length(); - vMsgsBeforeOpenLog->push_back(strTimestamped); + vMsgsBeforeOpenErrorLog->push_back(strTimestamped); } else { @@ -360,7 +419,7 @@ int LogPrintStr(const std::string &str) OpenDebugLog(); } - ret = LogWriteStr(strTimestamped); + ret = ErrorLogWriteStr(strTimestamped); } } diff --git a/src/util.h b/src/util.h index c9e6d8efc..0a6473538 100755 --- a/src/util.h +++ b/src/util.h @@ -79,8 +79,11 @@ boost::filesystem::path GetDebugLogPath(); /** Returns the path to the error.log file */ boost::filesystem::path GetErrorLogPath(); -/** Send a string to the log output */ -int LogPrintStr(const std::string &str); +/** Send a string to the debug log output */ +int DebugLogPrintStr(const std::string &str); + +/** Send a string to the error log output */ +int ErrorLogPrintStr(const std::string &str); #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__) @@ -94,14 +97,14 @@ static inline int LogPrint(const char* category, const char* fmt, const T1& v1, } catch (std::runtime_error &e) { _log_msg_ = "Error \"" + std::string(e.what()) + "\" while formatting log message: " + fmt; } - return LogPrintStr(_log_msg_); + return DebugLogPrintStr(_log_msg_); } template bool error(const char* fmt, const T1& v1, const Args&... args) { - LogPrintStr("ERROR: " + tfm::format(fmt, v1, args...) + "\n"); + ErrorLogPrintStr("ERROR: " + tfm::format(fmt, v1, args...) + "\n"); return false; } @@ -113,11 +116,11 @@ bool error(const char* fmt, const T1& v1, const Args&... args) static inline int LogPrint(const char* category, const char* s) { if(!LogAcceptCategory(category)) return 0; - return LogPrintStr(s); + return DebugLogPrintStr(s); } static inline bool error(const char* s) { - LogPrintStr(std::string("ERROR: ") + s + "\n"); + ErrorLogPrintStr(std::string("ERROR: ") + s + "\n"); return false; } static inline bool error(std::string s)