]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crimson/net: define fmt::formatter for write_state_t 48584/head
authorKefu Chai <tchaikov@gmail.com>
Fri, 21 Oct 2022 16:12:19 +0000 (00:12 +0800)
committerKefu Chai <tchaikov@gmail.com>
Fri, 21 Oct 2022 16:12:20 +0000 (00:12 +0800)
for better readability

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/crimson/net/Protocol.cc
src/crimson/net/Protocol.h

index fe5189bb7759552f5533dfa48609811fcca0ea1c..ebdcf22a29b5727d9f1bd46f2d1c01043276a1b1 100644 (file)
@@ -221,7 +221,7 @@ seastar::future<stop_t> Protocol::try_exit_sweep() {
         exit_open = std::nullopt;
         logger().info("{} write_event: nothing queued at {},"
                       " set exit_open",
-                      conn, get_state_name(write_state));
+                      conn, write_state);
       }
       return seastar::make_ready_future<stop_t>(stop_t::yes);
     } else {
@@ -291,17 +291,17 @@ seastar::future<> Protocol::do_write_dispatch_sweep()
         e.code() != std::errc::connection_reset &&
         e.code() != error::negotiation_failure) {
       logger().error("{} write_event(): unexpected error at {} -- {}",
-                     conn, get_state_name(write_state), e);
+                     conn, write_state, e);
       ceph_abort();
     }
     socket->shutdown();
     if (write_state == write_state_t::open) {
       logger().info("{} write_event(): fault at {}, going to delay -- {}",
-                    conn, get_state_name(write_state), e);
+                    conn, write_state, e);
       write_state = write_state_t::delay;
     } else {
       logger().info("{} write_event(): fault at {} -- {}",
-                    conn, get_state_name(write_state), e);
+                    conn, write_state, e);
     }
     return do_write_dispatch_sweep();
   });
index e34d2d1d11bf855a75baad6ee73395b80ba2ec55..0b5dc18ffae6c8938bf98f7498e103314caeb952 100644 (file)
@@ -110,16 +110,7 @@ class Protocol {
     drop
   };
 
-  static const char* get_state_name(write_state_t state) {
-    uint8_t index = static_cast<uint8_t>(state);
-    static const char *const state_names[] = {"none",
-                                              "delay",
-                                              "open",
-                                              "drop"};
-    assert(index < std::size(state_names));
-    return state_names[index];
-  }
-
+  friend class fmt::formatter<write_state_t>;
   void set_write_state(const write_state_t& state) {
     if (write_state == write_state_t::open &&
         state != write_state_t::open &&
@@ -184,3 +175,28 @@ inline std::ostream& operator<<(std::ostream& out, const Protocol& proto) {
 
 
 } // namespace crimson::net
+
+template <>
+struct fmt::formatter<crimson::net::Protocol::write_state_t>
+  : fmt::formatter<std::string_view> {
+  template <typename FormatContext>
+  auto format(crimson::net::Protocol::write_state_t state, FormatContext& ctx) {
+    using enum crimson::net::Protocol::write_state_t;
+    std::string_view name;
+    switch (state) {
+    case none:
+      name = "none";
+      break;
+    case delay:
+      name = "delay";
+      break;
+    case open:
+      name = "open";
+      break;
+    case drop:
+      name = "drop";
+      break;
+    }
+    return formatter<string_view>::format(name, ctx);
+  }
+};