From c1e9d9d0e007a40f0913b83df0c5d33dbef3d97e Mon Sep 17 00:00:00 2001 From: Peter Bell Date: Fri, 15 May 2020 17:23:22 +0100 Subject: Use thread safe localtime_r on linux --- src/Logger.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Logger.cpp b/src/Logger.cpp index 500007a84..05039cb1b 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -13,28 +13,25 @@ static void WriteLogOpener(fmt::memory_buffer & Buffer) { - time_t rawtime; - time(&rawtime); + const time_t rawtime = time(nullptr); - struct tm * timeinfo; + struct tm timeinfo; #ifdef _MSC_VER - struct tm timeinforeal; - timeinfo = &timeinforeal; - localtime_s(timeinfo, &rawtime); + localtime_s(&timeinfo, &rawtime); #else - timeinfo = localtime(&rawtime); + localtime_r(&rawtime, &timeinfo); #endif #ifdef _DEBUG const auto ThreadID = std::hash()(std::this_thread::get_id()); fmt::format_to( Buffer, "[{0:04x}|{1:02d}:{2:02d}:{3:02d}] ", - ThreadID, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec + ThreadID, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec ); #else fmt::format_to( Buffer, "[{0:02d}:{1:02d}:{2:02d}] ", - timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec + timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec ); #endif } -- cgit v1.2.3