From dec29373b408915a140d128abed4c10ff2131156 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 3 Apr 2026 14:58:10 +0800 Subject: [PATCH] Replace fmt::localtime with portable thread-safe equivalent fmt::localtime was removed in fmt 12. Replace it with an equivalent that is thread-safe and portable: - POSIX: localtime_r(&time, &tm) - MSVC: localtime_s(&tm, &time) (reversed argument order) Both are re-entrant and thread-safe, matching the guarantee that fmt::localtime provided. Signed-off-by: Kefu Chai --- src/logging.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 95ed5f8f3..b4ecf345c 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -27,8 +27,18 @@ void logger_t::generate_common_prefix(std::string *str, fmt::text_style const &ts, char const *prefix) const { - *str += fmt::format("{:%Y-%m-%d %H:%M:%S} ", - fmt::localtime(std::time(nullptr))); + auto const now = std::time(nullptr); + std::tm tm_local{}; +#ifdef _MSC_VER + if (localtime_s(&tm_local, &now) != 0) { + throw fmt::format_error("time_t value out of range"); + } +#else + if (!localtime_r(&now, &tm_local)) { + throw fmt::format_error("time_t value out of range"); + } +#endif + *str += fmt::format("{:%F %T} ", tm_local); if (m_current_level == log_level::debug) { *str += fmt::format(ts, "[{:02d}] ", this_thread_num);