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:
parent
95c0e2505c
commit
f1d06396a7
@ -39,6 +39,8 @@ class Client final
|
|||||||
friend struct ClientPerPartitionListHook;
|
friend struct ClientPerPartitionListHook;
|
||||||
friend class ClientList;
|
friend class ClientList;
|
||||||
|
|
||||||
|
const std::string name;
|
||||||
|
|
||||||
IntrusiveListHook<> list_siblings, partition_siblings;
|
IntrusiveListHook<> list_siblings, partition_siblings;
|
||||||
|
|
||||||
CoarseTimerEvent timeout_event;
|
CoarseTimerEvent timeout_event;
|
||||||
@ -52,8 +54,6 @@ class Client final
|
|||||||
|
|
||||||
CommandListBuilder cmd_list;
|
CommandListBuilder cmd_list;
|
||||||
|
|
||||||
const unsigned int num; /* client number */
|
|
||||||
|
|
||||||
/** is this client waiting for an "idle" response? */
|
/** is this client waiting for an "idle" response? */
|
||||||
bool idle_waiting = false;
|
bool idle_waiting = false;
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ public:
|
|||||||
Client(EventLoop &loop, Partition &partition,
|
Client(EventLoop &loop, Partition &partition,
|
||||||
UniqueSocketDescriptor fd, int uid,
|
UniqueSocketDescriptor fd, int uid,
|
||||||
unsigned _permission,
|
unsigned _permission,
|
||||||
int num) noexcept;
|
std::string &&_name) noexcept;
|
||||||
|
|
||||||
~Client() noexcept;
|
~Client() noexcept;
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
void
|
void
|
||||||
Client::OnSocketError(std::exception_ptr ep) noexcept
|
Client::OnSocketError(std::exception_ptr ep) noexcept
|
||||||
{
|
{
|
||||||
FmtError(client_domain, "error on client {}: {}", num, ep);
|
FmtError(client_domain, "[{}] error: {}", name, ep);
|
||||||
|
|
||||||
SetExpired();
|
SetExpired();
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ Client::OnTimeout() noexcept
|
|||||||
assert(!idle_waiting);
|
assert(!idle_waiting);
|
||||||
assert(!background_command);
|
assert(!background_command);
|
||||||
|
|
||||||
FmtDebug(client_domain, "[{}] timeout", num);
|
FmtDebug(client_domain, "[{}] timeout", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
Close();
|
Close();
|
||||||
|
@ -12,10 +12,13 @@
|
|||||||
#include "net/PeerCredentials.hxx"
|
#include "net/PeerCredentials.hxx"
|
||||||
#include "net/UniqueSocketDescriptor.hxx"
|
#include "net/UniqueSocketDescriptor.hxx"
|
||||||
#include "net/SocketAddress.hxx"
|
#include "net/SocketAddress.hxx"
|
||||||
|
#include "net/ToString.hxx"
|
||||||
#include "util/SpanCast.hxx"
|
#include "util/SpanCast.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
#include "Version.h"
|
#include "Version.h"
|
||||||
|
|
||||||
|
#include <fmt/core.h>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
using std::string_view_literals::operator""sv;
|
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,
|
Client::Client(EventLoop &_loop, Partition &_partition,
|
||||||
UniqueSocketDescriptor _fd,
|
UniqueSocketDescriptor _fd,
|
||||||
int _uid, unsigned _permission,
|
int _uid, unsigned _permission,
|
||||||
int _num) noexcept
|
std::string &&_name) noexcept
|
||||||
:FullyBufferedSocket(_fd.Release(), _loop,
|
:FullyBufferedSocket(_fd.Release(), _loop,
|
||||||
16384, client_max_output_buffer_size),
|
16384, client_max_output_buffer_size),
|
||||||
|
name(std::move(_name)),
|
||||||
timeout_event(_loop, BIND_THIS_METHOD(OnTimeout)),
|
timeout_event(_loop, BIND_THIS_METHOD(OnTimeout)),
|
||||||
partition(&_partition),
|
partition(&_partition),
|
||||||
permission(_permission),
|
permission(_permission),
|
||||||
uid(_uid),
|
uid(_uid),
|
||||||
num(_num),
|
|
||||||
last_album_art(_loop)
|
last_album_art(_loop)
|
||||||
{
|
{
|
||||||
|
FmtInfo(client_domain, "[{}] client connected", name);
|
||||||
|
|
||||||
timeout_event.Schedule(client_timeout);
|
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
|
void
|
||||||
client_new(EventLoop &loop, Partition &partition,
|
client_new(EventLoop &loop, Partition &partition,
|
||||||
UniqueSocketDescriptor fd, SocketAddress remote_address,
|
UniqueSocketDescriptor fd, SocketAddress remote_address,
|
||||||
SocketPeerCredentials cred,
|
SocketPeerCredentials cred,
|
||||||
unsigned permission) noexcept
|
unsigned permission) noexcept
|
||||||
{
|
{
|
||||||
static unsigned int next_client_num;
|
|
||||||
|
|
||||||
assert(fd.IsDefined());
|
assert(fd.IsDefined());
|
||||||
|
|
||||||
ClientList &client_list = *partition.instance.client_list;
|
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 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,
|
auto *client = new Client(loop, partition, std::move(fd), uid,
|
||||||
permission,
|
permission,
|
||||||
num);
|
MakeClientName(remote_address, cred));
|
||||||
|
|
||||||
client_list.Add(*client);
|
client_list.Add(*client);
|
||||||
partition.clients.push_back(*client);
|
partition.clients.push_back(*client);
|
||||||
|
|
||||||
FmtInfo(client_domain, "[{}] opened from {}",
|
|
||||||
num, remote_address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -79,6 +92,6 @@ Client::Close() noexcept
|
|||||||
if (FullyBufferedSocket::IsDefined())
|
if (FullyBufferedSocket::IsDefined())
|
||||||
FullyBufferedSocket::Close();
|
FullyBufferedSocket::Close();
|
||||||
|
|
||||||
FmtInfo(client_domain, "[{}] closed", num);
|
FmtInfo(client_domain, "[{}] disconnected", name);
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
@ -54,14 +54,14 @@ Client::ProcessLine(char *line) noexcept
|
|||||||
request */
|
request */
|
||||||
FmtWarning(client_domain,
|
FmtWarning(client_domain,
|
||||||
"[{}] malformed command {:?}",
|
"[{}] malformed command {:?}",
|
||||||
num, line);
|
name, line);
|
||||||
return CommandResult::CLOSE;
|
return CommandResult::CLOSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmd_list.IsActive() && IsAsyncCommmand(line)) {
|
if (cmd_list.IsActive() && IsAsyncCommmand(line)) {
|
||||||
FmtWarning(client_domain,
|
FmtWarning(client_domain,
|
||||||
"[{}] not possible in comand list: {:?}",
|
"[{}] not possible in comand list: {:?}",
|
||||||
num, line);
|
name, line);
|
||||||
return CommandResult::CLOSE;
|
return CommandResult::CLOSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,17 +82,15 @@ Client::ProcessLine(char *line) noexcept
|
|||||||
except "noidle" */
|
except "noidle" */
|
||||||
FmtWarning(client_domain,
|
FmtWarning(client_domain,
|
||||||
"[{}] command {:?} during idle",
|
"[{}] command {:?} during idle",
|
||||||
num, line);
|
name, line);
|
||||||
return CommandResult::CLOSE;
|
return CommandResult::CLOSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmd_list.IsActive()) {
|
if (cmd_list.IsActive()) {
|
||||||
if (StringIsEqual(line, CLIENT_LIST_MODE_END)) {
|
if (StringIsEqual(line, CLIENT_LIST_MODE_END)) {
|
||||||
const unsigned id = num;
|
|
||||||
|
|
||||||
FmtDebug(client_domain,
|
FmtDebug(client_domain,
|
||||||
"[{}] process command list",
|
"[{}] process command list",
|
||||||
id);
|
name);
|
||||||
|
|
||||||
const bool ok_mode = cmd_list.IsOKMode();
|
const bool ok_mode = cmd_list.IsOKMode();
|
||||||
auto list = cmd_list.Commit();
|
auto list = cmd_list.Commit();
|
||||||
@ -102,7 +100,7 @@ Client::ProcessLine(char *line) noexcept
|
|||||||
std::move(list));
|
std::move(list));
|
||||||
FmtDebug(client_domain,
|
FmtDebug(client_domain,
|
||||||
"[{}] process command "
|
"[{}] process command "
|
||||||
"list returned {}", id, unsigned(ret));
|
"list returned {}", name, unsigned(ret));
|
||||||
|
|
||||||
if (ret == CommandResult::OK)
|
if (ret == CommandResult::OK)
|
||||||
WriteOK();
|
WriteOK();
|
||||||
@ -113,7 +111,7 @@ Client::ProcessLine(char *line) noexcept
|
|||||||
FmtWarning(client_domain,
|
FmtWarning(client_domain,
|
||||||
"[{}] command list size "
|
"[{}] command list size "
|
||||||
"is larger than the max ({})",
|
"is larger than the max ({})",
|
||||||
num, client_max_command_list_size);
|
name, client_max_command_list_size);
|
||||||
return CommandResult::CLOSE;
|
return CommandResult::CLOSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,15 +125,13 @@ Client::ProcessLine(char *line) noexcept
|
|||||||
cmd_list.Begin(true);
|
cmd_list.Begin(true);
|
||||||
return CommandResult::OK;
|
return CommandResult::OK;
|
||||||
} else {
|
} else {
|
||||||
const unsigned id = num;
|
|
||||||
|
|
||||||
FmtDebug(client_domain,
|
FmtDebug(client_domain,
|
||||||
"[{}] process command {:?}",
|
"[{}] process command {:?}",
|
||||||
id, line);
|
name, line);
|
||||||
auto ret = command_process(*this, 0, line);
|
auto ret = command_process(*this, 0, line);
|
||||||
FmtDebug(client_domain,
|
FmtDebug(client_domain,
|
||||||
"[{}] command returned {}",
|
"[{}] command returned {}",
|
||||||
id, unsigned(ret));
|
name, unsigned(ret));
|
||||||
|
|
||||||
if (IsExpired())
|
if (IsExpired())
|
||||||
return CommandResult::CLOSE;
|
return CommandResult::CLOSE;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user