1
0

Use thread safe localtime_r on linux

This commit is contained in:
Peter Bell 2020-05-15 17:23:22 +01:00 committed by Tiger Wang
parent 89c9a7f563
commit c1e9d9d0e0

View File

@ -13,28 +13,25 @@
static void WriteLogOpener(fmt::memory_buffer & Buffer) static void WriteLogOpener(fmt::memory_buffer & Buffer)
{ {
time_t rawtime; const time_t rawtime = time(nullptr);
time(&rawtime);
struct tm * timeinfo; struct tm timeinfo;
#ifdef _MSC_VER #ifdef _MSC_VER
struct tm timeinforeal; localtime_s(&timeinfo, &rawtime);
timeinfo = &timeinforeal;
localtime_s(timeinfo, &rawtime);
#else #else
timeinfo = localtime(&rawtime); localtime_r(&rawtime, &timeinfo);
#endif #endif
#ifdef _DEBUG #ifdef _DEBUG
const auto ThreadID = std::hash<std::thread::id>()(std::this_thread::get_id()); const auto ThreadID = std::hash<std::thread::id>()(std::this_thread::get_id());
fmt::format_to( fmt::format_to(
Buffer, "[{0:04x}|{1:02d}:{2:02d}:{3:02d}] ", 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 #else
fmt::format_to( fmt::format_to(
Buffer, "[{0:02d}:{1:02d}:{2:02d}] ", 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 #endif
} }