]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
test/crimson: specialize fmt::formater<> for types to be formatted
authorKefu Chai <tchaikov@gmail.com>
Thu, 15 Dec 2022 09:32:55 +0000 (17:32 +0800)
committerKefu Chai <tchaikov@gmail.com>
Fri, 16 Dec 2022 02:37:25 +0000 (10:37 +0800)
since fmt v9, fmt::formatter<> is not specialized for the types with
operator<<(ostream&, ...) anymore. so we need to specialize it manually.
in this change, fmt::formatter<> is specialized for some types to be
formatted with fmtlib, so the tree can compile with fmt v9.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/crimson/net/Interceptor.h
src/test/crimson/seastore/test_block.h
src/test/crimson/seastore/test_transaction_manager.cc
src/test/crimson/test_messenger.cc

index 8f34dc3b7c8292a5d5fa79d5098718451a8a0305..cc4dc77d6c51cc75b4b5e3c54e51091064d6364c 100644 (file)
@@ -42,15 +42,6 @@ enum class bp_action_t {
   STALL
 };
 
-inline std::ostream& operator<<(std::ostream& out, const bp_action_t& action) {
-  static const char *const action_names[] = {"CONTINUE",
-                                             "FAULT",
-                                             "BLOCK",
-                                             "STALL"};
-  assert(static_cast<size_t>(action) < std::size(action_names));
-  return out << action_names[static_cast<size_t>(action)];
-}
-
 class socket_blocker {
   std::optional<seastar::abort_source> p_blocked;
   std::optional<seastar::abort_source> p_unblocked;
@@ -122,11 +113,39 @@ struct Breakpoint {
   bool operator<(const Breakpoint& x) const { return bp < x.bp; }
 };
 
-inline std::ostream& operator<<(std::ostream& out, const Breakpoint& bp) {
-  if (auto custom_bp = std::get_if<custom_bp_t>(&bp.bp)) {
-    return out << get_bp_name(*custom_bp);
-  } else {
-    auto tag_bp = std::get<tag_bp_t>(bp.bp);
+struct Interceptor {
+  socket_blocker blocker;
+  virtual ~Interceptor() {}
+  virtual void register_conn(Connection& conn) = 0;
+  virtual void register_conn_ready(Connection& conn) = 0;
+  virtual void register_conn_closed(Connection& conn) = 0;
+  virtual void register_conn_replaced(Connection& conn) = 0;
+  virtual bp_action_t intercept(Connection& conn, Breakpoint bp) = 0;
+};
+
+} // namespace crimson::net
+
+template<>
+struct fmt::formatter<crimson::net::bp_action_t> : fmt::formatter<std::string_view> {
+  template <typename FormatContext>
+  auto format(const crimson::net::bp_action_t& action, FormatContext& ctx) const {
+    static const char *const action_names[] = {"CONTINUE",
+                                               "FAULT",
+                                               "BLOCK",
+                                               "STALL"};
+    assert(static_cast<size_t>(action) < std::size(action_names));
+    return formatter<std::string_view>::format(action_names[static_cast<size_t>(action)], ctx);
+  }
+};
+
+template<>
+struct fmt::formatter<crimson::net::Breakpoint> : fmt::formatter<std::string_view> {
+  template <typename FormatContext>
+  auto format(const crimson::net::Breakpoint& bp, FormatContext& ctx) const {
+    if (auto custom_bp = std::get_if<crimson::net::custom_bp_t>(&bp.bp)) {
+      return formatter<std::string_view>::format(crimson::net::get_bp_name(*custom_bp), ctx);
+    }
+    auto tag_bp = std::get<crimson::net::tag_bp_t>(bp.bp);
     static const char *const tag_names[] = {"NONE",
                                             "HELLO",
                                             "AUTH_REQUEST",
@@ -149,19 +168,8 @@ inline std::ostream& operator<<(std::ostream& out, const Breakpoint& bp) {
                                             "KEEPALIVE2_ACK",
                                             "ACK"};
     assert(static_cast<size_t>(tag_bp.tag) < std::size(tag_names));
-    return out << tag_names[static_cast<size_t>(tag_bp.tag)]
-               << (tag_bp.type == bp_type_t::WRITE ? "_WRITE" : "_READ");
+    return fmt::format_to(ctx.out(), "{}_{}",
+                         tag_names[static_cast<size_t>(tag_bp.tag)],
+                         tag_bp.type == crimson::net::bp_type_t::WRITE ? "WRITE" : "READ");
   }
-}
-
-struct Interceptor {
-  socket_blocker blocker;
-  virtual ~Interceptor() {}
-  virtual void register_conn(Connection& conn) = 0;
-  virtual void register_conn_ready(Connection& conn) = 0;
-  virtual void register_conn_closed(Connection& conn) = 0;
-  virtual void register_conn_replaced(Connection& conn) = 0;
-  virtual bp_action_t intercept(Connection& conn, Breakpoint bp) = 0;
 };
-
-} // namespace crimson::net
index 9324694d38e090f17bbc4809267a046c8b290aa1..26588321d09790d98fece6c928448de071ca136a 100644 (file)
@@ -148,6 +148,7 @@ struct test_block_mutator_t {
 WRITE_CLASS_DENC_BOUNDED(crimson::os::seastore::test_block_delta_t)
 
 #if FMT_VERSION >= 90000
+template <> struct fmt::formatter<crimson::os::seastore::test_extent_desc_t> : fmt::ostream_formatter {};
 template <> struct fmt::formatter<crimson::os::seastore::TestBlock> : fmt::ostream_formatter {};
 template <> struct fmt::formatter<crimson::os::seastore::TestBlockPhysical> : fmt::ostream_formatter {};
 #endif
index 0e88aa355758febd9582176c9c06b38623cd8f3b..6c300519ae72d48a702f4c0d489863198268b874 100644 (file)
@@ -45,10 +45,14 @@ struct test_extent_record_t {
   }
 };
 
-std::ostream &operator<<(std::ostream &lhs, const test_extent_record_t &rhs) {
-  return lhs << "test_extent_record_t(" << rhs.desc
-            << ", refcount=" << rhs.refcount << ")";
-}
+template<>
+struct fmt::formatter<test_extent_record_t> : fmt::formatter<std::string_view> {
+  template <typename FormatContext>
+  auto format(const test_extent_record_t& r, FormatContext& ctx) const {
+    return fmt::format_to(ctx.out(), "test_extent_record_t({}, refcount={})",
+                         r.desc, r.refcount);
+  }
+};
 
 struct transaction_manager_test_t :
   public seastar_test_suite_t,
index 6409ffa7990db0419844a4d3509d455a4feca623..b8cde621689586b3f313e06291828e8cdee93565 100644 (file)
@@ -524,6 +524,15 @@ std::ostream& operator<<(std::ostream& out, const conn_state_t& state) {
   }
 }
 
+} // anonymous namespace
+
+#if FMT_VERSION >= 90000
+template<>
+struct fmt::formatter<conn_state_t> : fmt::ostream_formatter {};
+#endif
+
+namespace {
+
 struct ConnResult {
   ConnectionRef conn;
   unsigned index;