From f61f6c27e3d8f3fec63dcbf3849b7665cc0d083a Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 10 Jan 2024 10:16:11 +0800 Subject: [PATCH] gms: add formatter for gms::endpoint_state before this change, we rely on the default-generated fmt::formatter created from operator<<, but fmt v10 dropped the default-generated formatter. in this change, we define a formatter for gms::endpoint_state, and change update the callers of `operator<<` to use `fmt::print()`. but we cannot drop `operator<<` yet, as we are still using the templated operator<< and templated fmt::formatter to print containers in scylla and in seastar -- they are still using `operator<<` under the hood. Refs #13245 Signed-off-by: Kefu Chai Closes scylladb/scylladb#16705 --- gms/endpoint_state.cc | 17 +++++++++++------ gms/endpoint_state.hh | 10 +++++++++- gms/gossip_digest_ack.cc | 2 +- gms/gossip_digest_ack2.cc | 2 +- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/gms/endpoint_state.cc b/gms/endpoint_state.cc index c9ecc244bf..feb9c377cc 100644 --- a/gms/endpoint_state.cc +++ b/gms/endpoint_state.cc @@ -35,12 +35,7 @@ const versioned_value* endpoint_state::get_application_state_ptr(application_sta } std::ostream& operator<<(std::ostream& os, const endpoint_state& x) { - fmt::print(os, "HeartBeatState = {}, AppStateMap =", x._heart_beat_state); - for (auto&entry : x._application_state) { - const application_state& state = entry.first; - const versioned_value& value = entry.second; - os << " { " << state << " : " << value << " } "; - } + fmt::print(os, "{}", x); return os; } @@ -67,3 +62,13 @@ future<> i_endpoint_state_change_subscriber::on_application_state_change(inet_ad } } + +auto fmt::formatter::format(const gms::endpoint_state& x, + fmt::format_context& ctx) const -> decltype(ctx.out()) { + auto out = ctx.out(); + out = fmt::format_to(out, "HeartBeatState = {}, AppStateMap =", x._heart_beat_state); + for (auto& [state, value] : x._application_state) { + out = fmt::format_to(out, " {{ {} : {} }} ", state, value); + } + return out; +} diff --git a/gms/endpoint_state.hh b/gms/endpoint_state.hh index dcfb0000bc..94ceb811a9 100644 --- a/gms/endpoint_state.hh +++ b/gms/endpoint_state.hh @@ -149,9 +149,11 @@ public: bool is_cql_ready() const noexcept; - friend std::ostream& operator<<(std::ostream& os, const endpoint_state& x); + friend fmt::formatter; }; +std::ostream& operator<<(std::ostream& os, const endpoint_state& x); + using endpoint_state_ptr = lw_shared_ptr; inline endpoint_state_ptr make_endpoint_state_ptr(const endpoint_state& eps) { @@ -174,3 +176,9 @@ using permit_id = utils::tagged_uuid; constexpr permit_id null_permit_id = permit_id::create_null_id(); } // gms + +template <> +struct fmt::formatter { + constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } + auto format(const gms::endpoint_state&, fmt::format_context& ctx) const -> decltype(ctx.out()); +}; diff --git a/gms/gossip_digest_ack.cc b/gms/gossip_digest_ack.cc index 2df259315e..08c4232424 100644 --- a/gms/gossip_digest_ack.cc +++ b/gms/gossip_digest_ack.cc @@ -21,7 +21,7 @@ std::ostream& operator<<(std::ostream& os, const gossip_digest_ack& ack) { os << "} "; os << "endpoint_state:{"; for (auto& d : ack._map) { - os << "[" << d.first << "->" << d.second << "]"; + fmt::print(os, "[{}->{}]", d.first, d.second); } return os << "}"; } diff --git a/gms/gossip_digest_ack2.cc b/gms/gossip_digest_ack2.cc index f102ac7d86..50ba7f0438 100644 --- a/gms/gossip_digest_ack2.cc +++ b/gms/gossip_digest_ack2.cc @@ -16,7 +16,7 @@ namespace gms { std::ostream& operator<<(std::ostream& os, const gossip_digest_ack2& ack2) { os << "endpoint_state:{"; for (auto& d : ack2._map) { - os << "[" << d.first << "->" << d.second << "]"; + fmt::print(os, "[{}->{}]", d.first, d.second); } return os << "}"; }