]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
crimson/net/ProtocolV2: include fmt/chrono.h if fmt version >= 6.0
authorKefu Chai <kchai@redhat.com>
Tue, 27 Aug 2019 16:50:06 +0000 (00:50 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 28 Aug 2019 06:46:43 +0000 (14:46 +0800)
* check libfmt version instead of the exisistence of chrono.h, as
  libfmt merged fmt/time.h into fmt/chrono.h since v 6.0, while in
  libfmt v5.3, fmt/chrono.h did not provide all the necessary bits
  for fmt::to_format(..., const tm&). see also
  https://github.com/fmtlib/fmt/commit/a939c75956d27996680ce498529da0d1668c4eac
  and https://github.com/fmtlib/fmt/blob/master/ChangeLog.rst
* use fmt API instead of libc API for getting `localtime()`, it's
  more C++ friendly than plain `localtime_r()`

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/crimson/net/ProtocolV2.cc

index a475c535d4e96802fde83564b8b343c3043c1485..6d4003b89780b7e9fad108a82d6ed7e712b51960 100644 (file)
@@ -5,7 +5,7 @@
 
 #include <seastar/core/lowres_clock.hh>
 #include <fmt/format.h>
-#if __has_include(<fmt/chrono.h>)
+#if FMT_VERSION >= 60000
 #include <fmt/chrono.h>
 #else
 #include <fmt/time.h>
@@ -95,9 +95,8 @@ inline uint64_t generate_client_cookie() {
 
 } // namespace anonymous
 
-namespace fmt {
 template <>
-struct formatter<seastar::lowres_system_clock::time_point> {
+struct fmt::formatter<seastar::lowres_system_clock::time_point> {
   // ignore the format string
   template <typename ParseContext>
   constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
@@ -105,17 +104,14 @@ struct formatter<seastar::lowres_system_clock::time_point> {
   template <typename FormatContext>
   auto format(const seastar::lowres_system_clock::time_point& t,
              FormatContext& ctx) {
-    struct tm bdt;
-    time_t tt = std::chrono::duration_cast<std::chrono::seconds>(
+    std::time_t tt = std::chrono::duration_cast<std::chrono::seconds>(
       t.time_since_epoch()).count();
-    localtime_r(&tt, &bdt);
     auto milliseconds = (t.time_since_epoch() %
                         std::chrono::seconds(1)).count();
-    return format_to(ctx.out(), "{:%Y-%m-%d %H:%M:%S} {:03d}",
-                    bdt, milliseconds);
+    return fmt::format_to(ctx.out(), "{:%Y-%m-%d %H:%M:%S} {:03d}",
+                         fmt::localtime(tt), milliseconds);
   }
 };
-}
 
 namespace std {
 inline ostream& operator<<(