From: Kefu Chai Date: Sun, 13 Jul 2025 00:25:15 +0000 (+0800) Subject: crimson/common: replace deprecated fmt::localtime() with localtime_r() X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=4de277a410507b45664ae5353b67204b563aa956;p=ceph.git crimson/common: replace deprecated fmt::localtime() with localtime_r() Replace fmt::localtime() with localtime_r() to fix build failure with fmt 11.2.0. The fmt::localtime() function was deprecated in favor of std::localtime, causing build errors when treating warnings as errors: ``` [1/2] Building CXX object src/crimson/CMakeFiles/crimson-common.dir/common/formatter.cc.o /home/kefu/dev/ceph/src/crimson/common/formatter.cc: In member function ‘auto fmt::v11::formatter > > >::format(const seastar::lowres_system_clock::time_point&, FormatContext&) const’: /home/kefu/dev/ceph/src/crimson/common/formatter.cc:28:41: warning: ‘tm fmt::v11::localtime(time_t)’ is deprecated [-Wdeprecated-declarations] 28 | fmt::localtime(tt), milliseconds); | ~~~~~~~~~~~~~~^~~~ In file included from /home/kefu/dev/ceph/src/fmt/include/fmt/ostream.h:23, from /home/kefu/dev/ceph/src/seastar/include/seastar/util/backtrace.hh:40, from /home/kefu/dev/ceph/src/seastar/include/seastar/core/task.hh:25, from /home/kefu/dev/ceph/src/seastar/include/seastar/core/future.hh:36, from /home/kefu/dev/ceph/src/seastar/include/seastar/core/timer.hh:24, from /home/kefu/dev/ceph/src/seastar/include/seastar/core/lowres_clock.hh:26, from /home/kefu/dev/ceph/src/crimson/common/formatter.h:4, from /home/kefu/dev/ceph/src/crimson/common/formatter.cc:4: /home/kefu/dev/ceph/src/fmt/include/fmt/chrono.h:538:28: note: declared here 538 | FMT_DEPRECATED inline auto localtime(std::time_t time) -> std::tm { | ^~~~~~~~~ ``` Unlike other parts of the codebase, this implementation checks the return value of localtime_r() to preserve the error-handling behavior of fmt::localtime(), which throws on failure. Future changes may opt for consistency with the broader codebase over behavioral compatibility. Signed-off-by: Kefu Chai --- diff --git a/src/crimson/common/formatter.cc b/src/crimson/common/formatter.cc index 9ced61d984864..2b59a710cf9ba 100644 --- a/src/crimson/common/formatter.cc +++ b/src/crimson/common/formatter.cc @@ -3,6 +3,7 @@ #include "formatter.h" +#include #include #if FMT_VERSION >= 60000 #include @@ -20,12 +21,15 @@ struct fmt::formatter { template auto format(const seastar::lowres_system_clock::time_point& t, FormatContext& ctx) const { - std::time_t tt = std::chrono::duration_cast( - t.time_since_epoch()).count(); - auto milliseconds = (t.time_since_epoch() % - std::chrono::seconds(1)).count(); - return fmt::format_to(ctx.out(), "{:%Y-%m-%d %H:%M:%S} {:03d}", - fmt::localtime(tt), milliseconds); + auto milliseconds = std::chrono::duration_cast( + t.time_since_epoch() % std::chrono::seconds(1)).count(); + + std::time_t time = seastar::lowres_system_clock::to_time_t(t); + std::tm tm_local; + if (!localtime_r(&time, &tm_local)) { + throw fmt::format_error("time_t value out of range"); + } + return fmt::format_to(ctx.out(), "{:%F %T} {:03d}", tm_local, milliseconds); } };