Skip to content

Commit

Permalink
client: replace num with a name string
Browse files Browse the repository at this point in the history
This logs the client address (or the process id and uid for local
connections) in each log line instead of the number.
  • Loading branch information
MaxKellermann committed Jan 29, 2025
1 parent 95c0e25 commit f1d0639
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/client/Client.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Client final
friend struct ClientPerPartitionListHook;
friend class ClientList;

const std::string name;

IntrusiveListHook<> list_siblings, partition_siblings;

CoarseTimerEvent timeout_event;
Expand All @@ -52,8 +54,6 @@ class Client final

CommandListBuilder cmd_list;

const unsigned int num; /* client number */

/** is this client waiting for an "idle" response? */
bool idle_waiting = false;

Expand Down Expand Up @@ -122,7 +122,7 @@ public:
Client(EventLoop &loop, Partition &partition,
UniqueSocketDescriptor fd, int uid,
unsigned _permission,
int num) noexcept;
std::string &&_name) noexcept;

~Client() noexcept;

Expand Down
2 changes: 1 addition & 1 deletion src/client/Event.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
void
Client::OnSocketError(std::exception_ptr ep) noexcept
{
FmtError(client_domain, "error on client {}: {}", num, ep);
FmtError(client_domain, "[{}] error: {}", name, ep);

SetExpired();
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/Expire.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Client::OnTimeout() noexcept
assert(!idle_waiting);
assert(!background_command);

FmtDebug(client_domain, "[{}] timeout", num);
FmtDebug(client_domain, "[{}] timeout", name);
}

Close();
Expand Down
35 changes: 24 additions & 11 deletions src/client/New.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -79,6 +92,6 @@ Client::Close() noexcept
if (FullyBufferedSocket::IsDefined())
FullyBufferedSocket::Close();

FmtInfo(client_domain, "[{}] closed", num);
FmtInfo(client_domain, "[{}] disconnected", name);
delete this;
}
20 changes: 8 additions & 12 deletions src/client/Process.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ Client::ProcessLine(char *line) noexcept
request */
FmtWarning(client_domain,
"[{}] malformed command {:?}",
num, line);
name, line);
return CommandResult::CLOSE;
}

if (cmd_list.IsActive() && IsAsyncCommmand(line)) {
FmtWarning(client_domain,
"[{}] not possible in comand list: {:?}",
num, line);
name, line);
return CommandResult::CLOSE;
}

Expand All @@ -82,17 +82,15 @@ Client::ProcessLine(char *line) noexcept
except "noidle" */
FmtWarning(client_domain,
"[{}] command {:?} during idle",
num, line);
name, line);
return CommandResult::CLOSE;
}

if (cmd_list.IsActive()) {
if (StringIsEqual(line, CLIENT_LIST_MODE_END)) {
const unsigned id = num;

FmtDebug(client_domain,
"[{}] process command list",
id);
name);

const bool ok_mode = cmd_list.IsOKMode();
auto list = cmd_list.Commit();
Expand All @@ -102,7 +100,7 @@ Client::ProcessLine(char *line) noexcept
std::move(list));
FmtDebug(client_domain,
"[{}] process command "
"list returned {}", id, unsigned(ret));
"list returned {}", name, unsigned(ret));

if (ret == CommandResult::OK)
WriteOK();
Expand All @@ -113,7 +111,7 @@ Client::ProcessLine(char *line) noexcept
FmtWarning(client_domain,
"[{}] command list size "
"is larger than the max ({})",
num, client_max_command_list_size);
name, client_max_command_list_size);
return CommandResult::CLOSE;
}

Expand All @@ -127,15 +125,13 @@ Client::ProcessLine(char *line) noexcept
cmd_list.Begin(true);
return CommandResult::OK;
} else {
const unsigned id = num;

FmtDebug(client_domain,
"[{}] process command {:?}",
id, line);
name, line);
auto ret = command_process(*this, 0, line);
FmtDebug(client_domain,
"[{}] command returned {}",
id, unsigned(ret));
name, unsigned(ret));

if (IsExpired())
return CommandResult::CLOSE;
Expand Down

0 comments on commit f1d0639

Please sign in to comment.