Skip to content

Commit ec67826

Browse files
committed
logger: make logger thread-safe
The commit replaces non-thread-safe `localtime` function call with thread-safe `localtime_r` - it uses external buffer instead of internal static one under the hood to achieve thread-safety. Note that logger uses `std::cout` and `std::cerr` - to my surprise, they are actually thread-safe, but parts of the messages sent from different threads can get mixed. That's not great, but anyway the log is mostly used to report errors, and they are pretty rare. So, as long as it is not UB, such implementation is acceptable. Part of #110
1 parent 667fc64 commit ec67826

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/Utils/Logger.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class Logger {
7373
return;
7474
time_t rawTime;
7575
time(&rawTime);
76-
struct tm *timeInfo = localtime(&rawTime);
76+
struct tm buf;
77+
struct tm *timeInfo = localtime_r(&rawTime, &buf);
7778
char timeString[10];
7879
strftime(timeString, sizeof(timeString), "%H:%M:%S", timeInfo);
7980
// The line below is commented for compatibility with previous

0 commit comments

Comments
 (0)