From b2c61113ffb72bf7819103aa11fd8a04d50e9cbb Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 22 Oct 2022 00:12:19 +0800 Subject: [PATCH] crimson/net: define fmt::formatter for write_state_t for better readability Signed-off-by: Kefu Chai --- src/crimson/net/Protocol.cc | 8 ++++---- src/crimson/net/Protocol.h | 36 ++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/crimson/net/Protocol.cc b/src/crimson/net/Protocol.cc index fe5189bb7759..ebdcf22a29b5 100644 --- a/src/crimson/net/Protocol.cc +++ b/src/crimson/net/Protocol.cc @@ -221,7 +221,7 @@ seastar::future 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::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(); }); diff --git a/src/crimson/net/Protocol.h b/src/crimson/net/Protocol.h index e34d2d1d11bf..0b5dc18ffae6 100644 --- a/src/crimson/net/Protocol.h +++ b/src/crimson/net/Protocol.h @@ -110,16 +110,7 @@ class Protocol { drop }; - static const char* get_state_name(write_state_t state) { - uint8_t index = static_cast(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; 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 + : fmt::formatter { + template + 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::format(name, ctx); + } +}; -- 2.47.3