Skip to content

Commit faa2a47

Browse files
author
MarcoFalke
committed
logging: Add threadsafety comments
1 parent 0b282f9 commit faa2a47

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/logging.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static int FileWriteStr(const std::string &str, FILE *fp)
4141

4242
bool BCLog::Logger::StartLogging()
4343
{
44-
std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
44+
std::lock_guard<std::mutex> scoped_lock(m_cs);
4545

4646
assert(m_buffering);
4747
assert(m_fileout == nullptr);
@@ -216,9 +216,9 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
216216
return strStamped;
217217
}
218218

219-
void BCLog::Logger::LogPrintStr(const std::string &str)
219+
void BCLog::Logger::LogPrintStr(const std::string& str)
220220
{
221-
std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
221+
std::lock_guard<std::mutex> scoped_lock(m_cs);
222222
std::string str_prefixed = str;
223223

224224
if (m_log_threadnames && m_started_new_line) {

src/logging.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ namespace BCLog {
6060
class Logger
6161
{
6262
private:
63-
FILE* m_fileout = nullptr;
64-
std::mutex m_file_mutex;
65-
std::list<std::string> m_msgs_before_open;
66-
bool m_buffering = true; //!< Buffer messages before logging can be started
63+
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
64+
FILE* m_fileout = nullptr; // GUARDED_BY(m_cs)
65+
std::list<std::string> m_msgs_before_open; // GUARDED_BY(m_cs)
66+
bool m_buffering{true}; //!< Buffer messages before logging can be started. GUARDED_BY(m_cs)
6767

6868
/**
6969
* m_started_new_line is a state variable that will suppress printing of
@@ -89,10 +89,14 @@ namespace BCLog {
8989
std::atomic<bool> m_reopen_file{false};
9090

9191
/** Send a string to the log output */
92-
void LogPrintStr(const std::string &str);
92+
void LogPrintStr(const std::string& str);
9393

9494
/** Returns whether logs will be written to any output */
95-
bool Enabled() const { return m_buffering || m_print_to_console || m_print_to_file; }
95+
bool Enabled() const
96+
{
97+
std::lock_guard<std::mutex> scoped_lock(m_cs);
98+
return m_buffering || m_print_to_console || m_print_to_file;
99+
}
96100

97101
/** Start logging (and flush all buffered messages) */
98102
bool StartLogging();

0 commit comments

Comments
 (0)