From 1795c8ab0cdd2c56109abd727c73d4aa1d830602 Mon Sep 17 00:00:00 2001 From: David Yastremsky Date: Tue, 5 Sep 2023 21:26:21 -0700 Subject: [PATCH] Add mutex to logging to avoid race condition --- include/triton/common/logging.h | 7 ++++ src/logging.cc | 74 +++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/include/triton/common/logging.h b/include/triton/common/logging.h index 0be8df4..1efc0df 100644 --- a/include/triton/common/logging.h +++ b/include/triton/common/logging.h @@ -125,6 +125,13 @@ class Logger { // Flush the log. void Flush(); + // Get the mutex. + std::mutex& GetMutex() + { + return mutex_; + } + + private: std::vector enables_; uint32_t vlevel_; diff --git a/src/logging.cc b/src/logging.cc index 67b01ba..ebbafe7 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -84,26 +84,32 @@ LogMessage::LogMessage(const char* file, int line, uint32_t level) #ifdef _WIN32 SYSTEMTIME system_time; GetSystemTime(&system_time); - stream_ << level_name_[std::min(level, (uint32_t)Level::kINFO)] - << std::setfill('0') << std::setw(2) << system_time.wMonth - << std::setw(2) << system_time.wDay << ' ' << std::setw(2) - << system_time.wHour << ':' << std::setw(2) << system_time.wMinute - << ':' << std::setw(2) << system_time.wSecond << '.' - << std::setw(6) << system_time.wMilliseconds * 1000 << ' ' - << static_cast(GetCurrentProcessId()) << ' ' << path - << ':' << line << "] "; + { + std::lock_guard lk(gLogger_.GetMutex()); + stream_ << level_name_[std::min(level, (uint32_t)Level::kINFO)] + << std::setfill('0') << std::setw(2) << system_time.wMonth + << std::setw(2) << system_time.wDay << ' ' << std::setw(2) + << system_time.wHour << ':' << std::setw(2) << system_time.wMinute + << ':' << std::setw(2) << system_time.wSecond << '.' + << std::setw(6) << system_time.wMilliseconds * 1000 << ' ' + << static_cast(GetCurrentProcessId()) << ' ' << path + << ':' << line << "] "; + } #else struct timeval tv; gettimeofday(&tv, NULL); struct tm tm_time; gmtime_r(((time_t*)&(tv.tv_sec)), &tm_time); - stream_ << level_name_[std::min(level, (uint32_t)Level::kINFO)] - << std::setfill('0') << std::setw(2) << (tm_time.tm_mon + 1) - << std::setw(2) << tm_time.tm_mday << ' ' << std::setw(2) - << tm_time.tm_hour << ':' << std::setw(2) << tm_time.tm_min << ':' - << std::setw(2) << tm_time.tm_sec << '.' << std::setw(6) - << tv.tv_usec << ' ' << static_cast(getpid()) << ' ' - << path << ':' << line << "] "; + { + std::lock_guard lk(gLogger_.GetMutex()); + stream_ << level_name_[std::min(level, (uint32_t)Level::kINFO)] + << std::setfill('0') << std::setw(2) << (tm_time.tm_mon + 1) + << std::setw(2) << tm_time.tm_mday << ' ' << std::setw(2) + << tm_time.tm_hour << ':' << std::setw(2) << tm_time.tm_min << ':' + << std::setw(2) << tm_time.tm_sec << '.' << std::setw(6) + << tv.tv_usec << ' ' << static_cast(getpid()) << ' ' + << path << ':' << line << "] "; + } #endif break; } @@ -112,27 +118,33 @@ LogMessage::LogMessage(const char* file, int line, uint32_t level) #ifdef _WIN32 SYSTEMTIME system_time; GetSystemTime(&system_time); - stream_ << system_time.wYear << '-' << std::setfill('0') << std::setw(2) - << system_time.wMonth << '-' << std::setw(2) << system_time.wDay - << 'T' << std::setw(2) << system_time.wHour << ':' << std::setw(2) - << system_time.wMinute << ':' << std::setw(2) - << system_time.wSecond << "Z " - << level_name_[std::min(level, (uint32_t)Level::kINFO)] << ' ' - << static_cast(GetCurrentProcessId()) << ' ' << path - << ':' << line << "] "; + { + std::lock_guard lk(gLogger_.GetMutex()); + stream_ << system_time.wYear << '-' << std::setfill('0') << std::setw(2) + << system_time.wMonth << '-' << std::setw(2) << system_time.wDay + << 'T' << std::setw(2) << system_time.wHour << ':' << std::setw(2) + << system_time.wMinute << ':' << std::setw(2) + << system_time.wSecond << "Z " + << level_name_[std::min(level, (uint32_t)Level::kINFO)] << ' ' + << static_cast(GetCurrentProcessId()) << ' ' << path + << ':' << line << "] "; + } #else struct timeval tv; gettimeofday(&tv, NULL); struct tm tm_time; gmtime_r(((time_t*)&(tv.tv_sec)), &tm_time); - stream_ << (tm_time.tm_year + 1900) << '-' << std::setfill('0') - << std::setw(2) << (tm_time.tm_mon + 1) << '-' << std::setw(2) - << tm_time.tm_mday << 'T' << std::setw(2) << tm_time.tm_hour - << ':' << std::setw(2) << tm_time.tm_min << ':' << std::setw(2) - << tm_time.tm_sec << "Z " - << level_name_[std::min(level, (uint32_t)Level::kINFO)] << ' ' - << static_cast(getpid()) << ' ' << path << ':' << line - << "] "; + { + std::lock_guard lk(gLogger_.GetMutex()); + stream_ << (tm_time.tm_year + 1900) << '-' << std::setfill('0') + << std::setw(2) << (tm_time.tm_mon + 1) << '-' << std::setw(2) + << tm_time.tm_mday << 'T' << std::setw(2) << tm_time.tm_hour + << ':' << std::setw(2) << tm_time.tm_min << ':' << std::setw(2) + << tm_time.tm_sec << "Z " + << level_name_[std::min(level, (uint32_t)Level::kINFO)] << ' ' + << static_cast(getpid()) << ' ' << path << ':' << line + << "] "; + } #endif break; }