lib/fmt/SocketAddressFormatter: new library
This commit is contained in:
parent
b36f5f1ec4
commit
64647edbe1
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
// SPDX-License-Identifier: BSD-2-Clause
|
||||
// author: Max Kellermann <max.kellermann@gmail.com>
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "net/SocketAddress.hxx"
|
||||
#include "net/ToString.hxx"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include <concepts>
|
||||
|
||||
template<typename T>
|
||||
requires std::convertible_to<T, SocketAddress>
|
||||
struct fmt::formatter<T> : formatter<string_view>
|
||||
{
|
||||
template<typename FormatContext>
|
||||
auto format(SocketAddress address, FormatContext &ctx) {
|
||||
return formatter<string_view>::format(ToString(address), ctx);
|
||||
}
|
||||
};
|
|
@ -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 <fmt/format.h>
|
||||
|
||||
#include <exception>
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue