]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: improving fmtlib handling of ceph::utime_t
authorRonen Friedman <rfriedma@redhat.com>
Thu, 18 Aug 2022 15:27:47 +0000 (18:27 +0300)
committerRonen Friedman <rfriedma@redhat.com>
Sun, 28 Aug 2022 05:47:46 +0000 (08:47 +0300)
1. fixing the output to show local-time instead of UTC format, matching
   operator<<() handling (and all the rest of our logs)
2. adding a 'short' mode (as {:s}) for when, e.g. in most scrub logs,
   we only need 3 digits for the sub-second, and do not need the
   trailing TZ designation.

Signed-off-by: Ronen Friedman <rfriedma@redhat.com>
src/include/utime_fmt.h

index 44c9a40cd7b2d0151a2b13e035364cdf237ad54d..e7a98d2097d2b41af390879b94f2247d5a23d1da 100644 (file)
@@ -4,16 +4,23 @@
 /**
  * \file fmtlib formatter for utime_t
  */
-#include <fmt/format.h>
 #include <fmt/chrono.h>
-
-#include <string_view>
+#include <fmt/format.h>
 
 #include "include/utime.h"
 
 template <>
 struct fmt::formatter<utime_t> {
-  constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
+  template <typename ParseContext>
+  constexpr auto parse(ParseContext& ctx)
+  {
+    auto it = ctx.begin();
+    if (it != ctx.end() && *it == 's') {
+      short_format = true;
+      ++it;
+    }
+    return it;
+  }
 
   template <typename FormatContext>
   auto format(const utime_t& utime, FormatContext& ctx)
@@ -21,12 +28,20 @@ struct fmt::formatter<utime_t> {
     if (utime.sec() < ((time_t)(60 * 60 * 24 * 365 * 10))) {
       // raw seconds.  this looks like a relative time.
       return fmt::format_to(ctx.out(), "{}.{:06}", (long)utime.sec(),
-                            utime.usec());
+                           utime.usec());
     }
 
     // this looks like an absolute time.
     // conform to http://en.wikipedia.org/wiki/ISO_8601
-    auto asgmt = fmt::gmtime(utime.sec());
-    return fmt::format_to(ctx.out(), "{:%FT%T}.{:06}{:%z}", asgmt, utime.usec(), asgmt);
+    // (unless short_format is set)
+    auto aslocal = fmt::localtime(utime.sec());
+    if (short_format) {
+      return fmt::format_to(ctx.out(), "{:%FT%T}.{:03}", aslocal,
+                           utime.usec() / 1000);
+    }
+    return fmt::format_to(ctx.out(), "{:%FT%T}.{:06}{:%z}", aslocal,
+                         utime.usec(), aslocal);
   }
+
+  bool short_format{false};
 };