From e9784125241064709d25e9c64d16e1c72b4d7bed Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 10 Mar 2020 19:41:19 +0800 Subject: [PATCH] crimson: extract formatter for time_point types Signed-off-by: Kefu Chai --- src/crimson/CMakeLists.txt | 3 +- src/crimson/common/formatter.cc | 64 +++++++++++++++++++++++++++++++++ src/crimson/common/formatter.h | 15 ++++++++ src/crimson/net/ProtocolV2.cc | 32 +---------------- 4 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 src/crimson/common/formatter.cc create mode 100644 src/crimson/common/formatter.h diff --git a/src/crimson/CMakeLists.txt b/src/crimson/CMakeLists.txt index 224deb55de9aa..e29bb98b37808 100644 --- a/src/crimson/CMakeLists.txt +++ b/src/crimson/CMakeLists.txt @@ -14,10 +14,11 @@ set_target_properties(crimson::cflags PROPERTIES set(crimson_common_srcs admin/admin_socket.cc admin/osd_admin.cc + common/assert.cc common/buffer_io.cc common/config_proxy.cc + common/formatter.cc common/perf_counters_collection.cc - common/assert.cc common/log.cc) # the specialized version of ceph-common, where diff --git a/src/crimson/common/formatter.cc b/src/crimson/common/formatter.cc new file mode 100644 index 0000000000000..677216224ee81 --- /dev/null +++ b/src/crimson/common/formatter.cc @@ -0,0 +1,64 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "formatter.h" + +#include +#if FMT_VERSION >= 60000 +#include +#else +#include +#endif + + +template <> +struct fmt::formatter { + // ignore the format string + template + constexpr auto parse(ParseContext &ctx) { return ctx.begin(); } + + template + auto format(const seastar::lowres_system_clock::time_point& t, + FormatContext& ctx) { + 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); + } +}; + +template <> +struct fmt::formatter { + // ignore the format string + template + constexpr auto parse(ParseContext &ctx) { return ctx.begin(); } + + template + auto format(const ceph::coarse_real_clock::time_point& t, + FormatContext& ctx) { + 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); + } +}; + +namespace std { + +ostream& operator<<(ostream& out, + const seastar::lowres_system_clock::time_point& t) +{ + return out << fmt::format("{}", t); +} + +ostream& operator<<(ostream& out, + const ceph::coarse_real_clock::time_point& t) +{ + return out << fmt::format("{}", t); +} + +} diff --git a/src/crimson/common/formatter.h b/src/crimson/common/formatter.h new file mode 100644 index 0000000000000..1775b0954f497 --- /dev/null +++ b/src/crimson/common/formatter.h @@ -0,0 +1,15 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include + +#include "common/ceph_time.h" + +namespace std { + +ostream& operator<<(ostream& out, + const seastar::lowres_system_clock::time_point& t); +ostream& operator<<(ostream& out, + const ceph::coarse_real_clock::time_point& t); + +} diff --git a/src/crimson/net/ProtocolV2.cc b/src/crimson/net/ProtocolV2.cc index 2535584bb6efb..befbaa5862e45 100644 --- a/src/crimson/net/ProtocolV2.cc +++ b/src/crimson/net/ProtocolV2.cc @@ -5,16 +5,12 @@ #include #include -#if FMT_VERSION >= 60000 -#include -#else -#include -#endif #include "include/msgr.h" #include "include/random.h" #include "crimson/auth/AuthClient.h" #include "crimson/auth/AuthServer.h" +#include "crimson/common/formatter.h" #include "Config.h" #include "Dispatcher.h" @@ -99,32 +95,6 @@ inline uint64_t generate_client_cookie() { } // namespace anonymous -template <> -struct fmt::formatter { - // ignore the format string - template - constexpr auto parse(ParseContext &ctx) { return ctx.begin(); } - - template - auto format(const seastar::lowres_system_clock::time_point& t, - FormatContext& ctx) { - 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); - } -}; - -namespace std { -inline ostream& operator<<( - ostream& out, const seastar::lowres_system_clock::time_point& t) -{ - return out << fmt::format("{}", t); -} -} - namespace crimson::net { #ifdef UNIT_TESTS_BUILT -- 2.39.5