From faa2a47cd7bcdbd187035c76f8dbd0442f6818dc Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Tue, 28 May 2019 10:39:29 -0400 Subject: logging: Add threadsafety comments --- src/logging.cpp | 6 +++--- src/logging.h | 16 ++++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 268614065a..418a701179 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -41,7 +41,7 @@ static int FileWriteStr(const std::string &str, FILE *fp) bool BCLog::Logger::StartLogging() { - std::lock_guard scoped_lock(m_file_mutex); + std::lock_guard scoped_lock(m_cs); assert(m_buffering); assert(m_fileout == nullptr); @@ -216,9 +216,9 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str) return strStamped; } -void BCLog::Logger::LogPrintStr(const std::string &str) +void BCLog::Logger::LogPrintStr(const std::string& str) { - std::lock_guard scoped_lock(m_file_mutex); + std::lock_guard scoped_lock(m_cs); std::string str_prefixed = str; if (m_log_threadnames && m_started_new_line) { diff --git a/src/logging.h b/src/logging.h index f40a10bc4c..36b1a4045d 100644 --- a/src/logging.h +++ b/src/logging.h @@ -60,10 +60,10 @@ namespace BCLog { class Logger { private: - FILE* m_fileout = nullptr; - std::mutex m_file_mutex; - std::list m_msgs_before_open; - bool m_buffering = true; //!< Buffer messages before logging can be started + mutable std::mutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected + FILE* m_fileout = nullptr; // GUARDED_BY(m_cs) + std::list m_msgs_before_open; // GUARDED_BY(m_cs) + bool m_buffering{true}; //!< Buffer messages before logging can be started. GUARDED_BY(m_cs) /** * m_started_new_line is a state variable that will suppress printing of @@ -89,10 +89,14 @@ namespace BCLog { std::atomic m_reopen_file{false}; /** Send a string to the log output */ - void LogPrintStr(const std::string &str); + void LogPrintStr(const std::string& str); /** Returns whether logs will be written to any output */ - bool Enabled() const { return m_buffering || m_print_to_console || m_print_to_file; } + bool Enabled() const + { + std::lock_guard scoped_lock(m_cs); + return m_buffering || m_print_to_console || m_print_to_file; + } /** Start logging (and flush all buffered messages) */ bool StartLogging(); -- cgit v1.2.3