Skip to content

Commit dec2937

Browse files
committed
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 <tchaikov@gmail.com>
1 parent 6ce2bc0 commit dec2937

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/logging.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,18 @@ void logger_t::generate_common_prefix(std::string *str,
2727
fmt::text_style const &ts,
2828
char const *prefix) const
2929
{
30-
*str += fmt::format("{:%Y-%m-%d %H:%M:%S} ",
31-
fmt::localtime(std::time(nullptr)));
30+
auto const now = std::time(nullptr);
31+
std::tm tm_local{};
32+
#ifdef _MSC_VER
33+
if (localtime_s(&tm_local, &now) != 0) {
34+
throw fmt::format_error("time_t value out of range");
35+
}
36+
#else
37+
if (!localtime_r(&now, &tm_local)) {
38+
throw fmt::format_error("time_t value out of range");
39+
}
40+
#endif
41+
*str += fmt::format("{:%F %T} ", tm_local);
3242

3343
if (m_current_level == log_level::debug) {
3444
*str += fmt::format(ts, "[{:02d}] ", this_thread_num);

0 commit comments

Comments
 (0)