client: replace num with a name string

This logs the client address (or the process id and uid for local
connections) in each log line instead of the number.
This commit is contained in:
Max Kellermann
2025-01-29 19:55:30 +01:00
parent 95c0e2505c
commit f1d06396a7
5 changed files with 37 additions and 28 deletions

View File

@@ -12,10 +12,13 @@
#include "net/PeerCredentials.hxx"
#include "net/UniqueSocketDescriptor.hxx"
#include "net/SocketAddress.hxx"
#include "net/ToString.hxx"
#include "util/SpanCast.hxx"
#include "Log.hxx"
#include "Version.h"
#include <fmt/core.h>
#include <cassert>
using std::string_view_literals::operator""sv;
@@ -25,27 +28,41 @@ static constexpr auto GREETING = "OK MPD " PROTOCOL_VERSION "\n"sv;
Client::Client(EventLoop &_loop, Partition &_partition,
UniqueSocketDescriptor _fd,
int _uid, unsigned _permission,
int _num) noexcept
std::string &&_name) noexcept
:FullyBufferedSocket(_fd.Release(), _loop,
16384, client_max_output_buffer_size),
name(std::move(_name)),
timeout_event(_loop, BIND_THIS_METHOD(OnTimeout)),
partition(&_partition),
permission(_permission),
uid(_uid),
num(_num),
last_album_art(_loop)
{
FmtInfo(client_domain, "[{}] client connected", name);
timeout_event.Schedule(client_timeout);
}
[[gnu::pure]]
static std::string
MakeClientName(SocketAddress address, const SocketPeerCredentials &cred) noexcept
{
if (cred.IsDefined()) {
if (cred.GetPid() > 0)
return fmt::format("pid={} uid={}", cred.GetPid(), cred.GetUid());
return fmt::format("uid={}", cred.GetUid());
}
return ToString(address);
}
void
client_new(EventLoop &loop, Partition &partition,
UniqueSocketDescriptor fd, SocketAddress remote_address,
SocketPeerCredentials cred,
unsigned permission) noexcept
{
static unsigned int next_client_num;
assert(fd.IsDefined());
ClientList &client_list = *partition.instance.client_list;
@@ -58,16 +75,12 @@ client_new(EventLoop &loop, Partition &partition,
const int uid = cred.IsDefined() ? static_cast<int>(cred.GetUid()) : -1;
const unsigned num = next_client_num++;
auto *client = new Client(loop, partition, std::move(fd), uid,
permission,
num);
permission,
MakeClientName(remote_address, cred));
client_list.Add(*client);
partition.clients.push_back(*client);
FmtInfo(client_domain, "[{}] opened from {}",
num, remote_address);
}
void
@@ -79,6 +92,6 @@ Client::Close() noexcept
if (FullyBufferedSocket::IsDefined())
FullyBufferedSocket::Close();
FmtInfo(client_domain, "[{}] closed", num);
FmtInfo(client_domain, "[{}] disconnected", name);
delete this;
}