0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-15 11:36:00 -05:00

logging: Add threadsafety comments

This commit is contained in:
MarcoFalke 2019-05-28 10:39:29 -04:00
parent 0b282f9b00
commit faa2a47cd7
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 13 additions and 9 deletions

View file

@ -41,7 +41,7 @@ static int FileWriteStr(const std::string &str, FILE *fp)
bool BCLog::Logger::StartLogging() bool BCLog::Logger::StartLogging()
{ {
std::lock_guard<std::mutex> scoped_lock(m_file_mutex); std::lock_guard<std::mutex> scoped_lock(m_cs);
assert(m_buffering); assert(m_buffering);
assert(m_fileout == nullptr); assert(m_fileout == nullptr);
@ -216,9 +216,9 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
return strStamped; return strStamped;
} }
void BCLog::Logger::LogPrintStr(const std::string &str) void BCLog::Logger::LogPrintStr(const std::string& str)
{ {
std::lock_guard<std::mutex> scoped_lock(m_file_mutex); std::lock_guard<std::mutex> scoped_lock(m_cs);
std::string str_prefixed = str; std::string str_prefixed = str;
if (m_log_threadnames && m_started_new_line) { if (m_log_threadnames && m_started_new_line) {

View file

@ -60,10 +60,10 @@ namespace BCLog {
class Logger class Logger
{ {
private: private:
FILE* m_fileout = nullptr; 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
std::mutex m_file_mutex; FILE* m_fileout = nullptr; // GUARDED_BY(m_cs)
std::list<std::string> m_msgs_before_open; std::list<std::string> m_msgs_before_open; // GUARDED_BY(m_cs)
bool m_buffering = true; //!< Buffer messages before logging can be started 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 * m_started_new_line is a state variable that will suppress printing of
@ -89,10 +89,14 @@ namespace BCLog {
std::atomic<bool> m_reopen_file{false}; std::atomic<bool> m_reopen_file{false};
/** Send a string to the log output */ /** 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 */ /** 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<std::mutex> scoped_lock(m_cs);
return m_buffering || m_print_to_console || m_print_to_file;
}
/** Start logging (and flush all buffered messages) */ /** Start logging (and flush all buffered messages) */
bool StartLogging(); bool StartLogging();