Skip to content

Commit

Permalink
gms/inet_address: specialize fmt::formatter<gms::inet_address>
Browse files Browse the repository at this point in the history
this is a part of a series to migrating from `operator<<(ostream&, ..)`
based formatting to fmtlib based formatting. the goal here is to enable
fmtlib to print `gms::inet_address` with the help of fmt::ostream.
please note, the ':' delimiter is specified when printing the IPv6 address.

Refs #13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
  • Loading branch information
tchaikov committed Mar 27, 2023
1 parent a606606 commit 4ea6e06
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gms/inet_address.hh
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ struct hash<gms::inet_address> {
size_t operator()(gms::inet_address a) const noexcept { return std::hash<net::inet_address>()(a._addr); }
};
}

template <>
struct fmt::formatter<gms::inet_address> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(const ::gms::inet_address& x, FormatContext& ctx) const {
if (x.addr().is_ipv4()) {
return fmt::format_to(ctx.out(), "{}", x.addr());
}
// print 2 bytes in a group, and use ':' as the delimeter
format_to(ctx.out(), "{:2:}", fmt_hex(x.bytes()));
if (x.addr().scope() != seastar::net::inet_address::invalid_scope) {
return format_to(ctx.out(), "%{}", x.addr().scope());
}
return ctx.out();
}
};

0 comments on commit 4ea6e06

Please sign in to comment.