From 64647edbe1f0712a173a73896f270f6bf6ae6065 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 19 Sep 2023 11:17:48 +0200 Subject: [PATCH] lib/fmt/SocketAddressFormatter: new library --- src/client/New.cxx | 7 +++---- src/event/ServerSocket.cxx | 18 +++++++----------- src/lib/fmt/SocketAddressFormatter.hxx | 21 +++++++++++++++++++++ test/run_resolver.cxx | 6 ++++-- 4 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 src/lib/fmt/SocketAddressFormatter.hxx diff --git a/src/client/New.cxx b/src/client/New.cxx index b4d65ab62..d3df5158b 100644 --- a/src/client/New.cxx +++ b/src/client/New.cxx @@ -8,9 +8,9 @@ #include "BackgroundCommand.hxx" #include "Partition.hxx" #include "Instance.hxx" +#include "lib/fmt/SocketAddressFormatter.hxx" #include "net/UniqueSocketDescriptor.hxx" #include "net/SocketAddress.hxx" -#include "net/ToString.hxx" #include "Log.hxx" #include "Version.h" @@ -36,11 +36,10 @@ Client::Client(EventLoop &_loop, Partition &_partition, void client_new(EventLoop &loop, Partition &partition, - UniqueSocketDescriptor fd, SocketAddress address, int uid, + UniqueSocketDescriptor fd, SocketAddress remote_address, int uid, unsigned permission) noexcept { static unsigned int next_client_num; - const auto remote = ToString(address); assert(fd.IsDefined()); @@ -61,7 +60,7 @@ client_new(EventLoop &loop, Partition &partition, partition.clients.push_back(*client); FmtInfo(client_domain, "[{}] opened from {}", - num, remote); + num, remote_address); } void diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx index fff8e3e6e..2fcca8a7c 100644 --- a/src/event/ServerSocket.cxx +++ b/src/event/ServerSocket.cxx @@ -4,6 +4,7 @@ #include "ServerSocket.hxx" #include "lib/fmt/ExceptionFormatter.hxx" #include "lib/fmt/RuntimeError.hxx" +#include "lib/fmt/SocketAddressFormatter.hxx" #include "net/IPv4Address.hxx" #include "net/IPv6Address.hxx" #include "net/StaticSocketAddress.hxx" @@ -13,7 +14,6 @@ #include "net/UniqueSocketDescriptor.hxx" #include "net/Resolver.hxx" #include "net/AddressInfo.hxx" -#include "net/ToString.hxx" #include "event/SocketEvent.hxx" #include "fs/AllocatedPath.hxx" #include "util/Domain.hxx" @@ -83,9 +83,9 @@ public: event.Close(); } - [[nodiscard]] [[gnu::pure]] - std::string ToString() const noexcept { - return ::ToString(address); + [[nodiscard]] + SocketAddress GetAddress() const noexcept { + return address; } void SetFD(UniqueSocketDescriptor _fd) noexcept { @@ -224,23 +224,19 @@ ServerSocket::Open() i.Open(); } catch (...) { if (good != nullptr && good->GetSerial() == i.GetSerial()) { - const auto address_string = i.ToString(); - const auto good_string = good->ToString(); FmtError(server_socket_domain, "bind to '{}' failed " "(continuing anyway, because " "binding to '{}' succeeded): {}", - address_string, - good_string, + i.GetAddress(), + good->GetAddress(), std::current_exception()); } else if (bad == nullptr) { bad = &i; - const auto address_string = i.ToString(); - try { std::throw_with_nested(FmtRuntimeError("Failed to bind to '{}'", - address_string)); + i.GetAddress())); } catch (...) { last_error = std::current_exception(); } diff --git a/src/lib/fmt/SocketAddressFormatter.hxx b/src/lib/fmt/SocketAddressFormatter.hxx new file mode 100644 index 000000000..14a133155 --- /dev/null +++ b/src/lib/fmt/SocketAddressFormatter.hxx @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: BSD-2-Clause +// author: Max Kellermann + +#pragma once + +#include "net/SocketAddress.hxx" +#include "net/ToString.hxx" + +#include + +#include + +template +requires std::convertible_to +struct fmt::formatter : formatter +{ + template + auto format(SocketAddress address, FormatContext &ctx) { + return formatter::format(ToString(address), ctx); + } +}; diff --git a/test/run_resolver.cxx b/test/run_resolver.cxx index c29c65a8a..02f834eed 100644 --- a/test/run_resolver.cxx +++ b/test/run_resolver.cxx @@ -1,12 +1,14 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project +#include "lib/fmt/SocketAddressFormatter.hxx" #include "net/Resolver.hxx" #include "net/AddressInfo.hxx" -#include "net/ToString.hxx" #include "net/SocketAddress.hxx" #include "util/PrintException.hxx" +#include + #include #include @@ -20,7 +22,7 @@ try { } for (const auto &i : Resolve(argv[1], 80, AI_PASSIVE, SOCK_STREAM)) { - printf("%s\n", ToString(i).c_str()); + fmt::print("{}\n", i); } return EXIT_SUCCESS;