From cb6c7bb9bf89660d07704f7f0fe29a76ac6fc32e Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 8 Mar 2024 11:15:09 +0800 Subject: [PATCH] service/paxos: add fmt::formatter for paxos::proposal 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 formatters for `service::paxos::proposal`, but its operator<< is preserved, as it is still used by our generic formatter for std::tuple<> which uses operator<< for printing the elements in it, so operator<< of this class is indirectly used. Refs #13245 Signed-off-by: Kefu Chai --- service/paxos/proposal.cc | 12 +++--------- service/paxos/proposal.hh | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/service/paxos/proposal.cc b/service/paxos/proposal.cc index aacd7b9ef2..1382f5c36c 100644 --- a/service/paxos/proposal.cc +++ b/service/paxos/proposal.cc @@ -9,13 +9,7 @@ #include "proposal.hh" -namespace service { - -namespace paxos { - -std::ostream& operator<<(std::ostream& os, const proposal& proposal) { - return os << "proposal(" << proposal.ballot << ")"; +auto fmt::formatter::format(const service::paxos::proposal& proposal, + fmt::format_context& ctx) const -> decltype(ctx.out()) { + return fmt::format_to(ctx.out(), "proposal({})", proposal.ballot); } - -} // end of namespace "paxos" -} // endf of namespace "service" diff --git a/service/paxos/proposal.hh b/service/paxos/proposal.hh index 98ae12cb4d..2444b9a793 100644 --- a/service/paxos/proposal.hh +++ b/service/paxos/proposal.hh @@ -9,6 +9,7 @@ #pragma once #include "mutation/frozen_mutation.hh" +#include namespace service { @@ -45,8 +46,19 @@ inline bool operator>(const proposal& lhs, const proposal& rhs) { return lhs.ballot.timestamp() > rhs.ballot.timestamp(); } -// Used for logging and debugging. -std::ostream& operator<<(std::ostream& os, const proposal& proposal); - } // end of namespace "paxos" } // end of namespace "service" + +// Used for logging and debugging. +template <> struct fmt::formatter : fmt::formatter { + auto format(const service::paxos::proposal&, fmt::format_context& ctx) const -> decltype(ctx.out()); +}; + +namespace service::paxos { + +static inline std::ostream& operator<<(std::ostream& os, const proposal& proposal) { + fmt::print(os, "{}", proposal); + return os; +} + +}