From f863d6e559fd9aeb8def46131f912d8eac08da47 Mon Sep 17 00:00:00 2001 From: Wenbin <85331908+Wenbin1002@users.noreply.github.com> Date: Fri, 1 Dec 2023 15:17:12 +0800 Subject: [PATCH] add a lock to make LOG_HEAD thread-safe #{317} (#327) ### What problem were solved in this pull request? Issue Number: close #317 Problem: localtime is not a thread-safe function. ### What is changed and how it works? use localtime_r instead of localtime Co-authored-by: Helloworld-lbl <114924425+Helloworld-lbl@users.noreply.github.com> Co-authored-by: wangyunlai --- deps/common/log/log.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/deps/common/log/log.h b/deps/common/log/log.h index 71272b9c0..33b931c50 100644 --- a/deps/common/log/log.h +++ b/deps/common/log/log.h @@ -185,8 +185,10 @@ extern Log *g_log; if (common::g_log) { \ struct timeval tv; \ gettimeofday(&tv, NULL); \ - struct tm *p = localtime(&tv.tv_sec); \ - char sz_head[LOG_HEAD_SIZE] = {0}; \ + struct tm curr_time; \ + struct tm *p = localtime_r(&tv.tv_sec, &curr_time); \ + \ + char sz_head[LOG_HEAD_SIZE] = {0}; \ if (p) { \ int usec = (int)tv.tv_usec; \ snprintf(sz_head, \