From: Ronen Friedman Date: Thu, 18 Aug 2022 15:27:47 +0000 (+0300) Subject: common: improving fmtlib handling of ceph::utime_t X-Git-Tag: v18.0.0~157^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=93dfb7e88b9cb117ad74078300f886eb2b2049f1;p=ceph.git common: improving fmtlib handling of ceph::utime_t 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 --- diff --git a/src/include/utime_fmt.h b/src/include/utime_fmt.h index 44c9a40cd7b2..e7a98d2097d2 100644 --- a/src/include/utime_fmt.h +++ b/src/include/utime_fmt.h @@ -4,16 +4,23 @@ /** * \file fmtlib formatter for utime_t */ -#include #include - -#include +#include #include "include/utime.h" template <> struct fmt::formatter { - constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } + template + constexpr auto parse(ParseContext& ctx) + { + auto it = ctx.begin(); + if (it != ctx.end() && *it == 's') { + short_format = true; + ++it; + } + return it; + } template auto format(const utime_t& utime, FormatContext& ctx) @@ -21,12 +28,20 @@ struct fmt::formatter { 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}; };