From 2e2bd1d137b25557c30bfed42070c74bd3aeb882 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 6 May 2022 20:04:01 +0200 Subject: [PATCH] Added log padding --- src/log.h | 37 +++++++++++++++++++++++++++++++++++++ src/p2p_server.cpp | 5 +++++ src/stratum_server.cpp | 20 +++++++++++++++----- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/log.h b/src/log.h index 9b87cdc..c664cb3 100644 --- a/src/log.h +++ b/src/log.h @@ -414,6 +414,43 @@ template<> struct log::Stream::Entry } }; +template +struct PadRight +{ + FORCEINLINE PadRight(const T& value, int len) : m_value(value), m_len(len) {} + + const T& m_value; + int m_len; + + // Declare it to make compiler happy + PadRight(const PadRight&); + +private: + PadRight& operator=(const PadRight&) = delete; + PadRight& operator=(PadRight&&) = delete; +}; + +template FORCEINLINE PadRight pad_right(const T& value, int len) { return PadRight(value, len); } + +template +struct log::Stream::Entry> +{ + static NOINLINE void put(PadRight&& data, Stream* wrapper) + { + char buf[log::Stream::BUF_SIZE + 1]; + log::Stream s(buf); + s << data.m_value; + + const int len = std::min(data.m_len, log::Stream::BUF_SIZE); + if (s.m_pos < len) { + memset(buf + s.m_pos, ' ', len - s.m_pos); + s.m_pos = len; + } + + wrapper->writeBuf(buf, s.m_pos); + } +}; + void put_rawip(const raw_ip& value, Stream* wrapper); template<> struct log::Stream::Entry diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 58e9700..ada0f84 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -866,11 +866,16 @@ void P2PServer::show_peers() { MutexLock lock(m_clientsListLock); + size_t n = 0; + for (P2PClient* client = static_cast(m_connectedClientsList->m_next); client != m_connectedClientsList; client = static_cast(client->m_next)) { if (client->m_listenPort >= 0) { LOGINFO(0, (client->m_isIncoming ? "I " : "O ") << client->m_pingTime << " ms\t" << static_cast(client->m_addrString)); + ++n; } } + + LOGINFO(0, "Total: " << n << " peers"); } void P2PServer::on_timer() diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index f8a9e85..dbefd01 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -447,13 +447,23 @@ void StratumServer::show_workers() MutexLock lock(m_clientsListLock); - for (StratumClient* c = static_cast(m_connectedClientsList->m_next); c != m_connectedClientsList; c = static_cast(c->m_next)) { - LOGINFO(0, static_cast(c->m_addrString) - << '\t' << (c->m_rpcId ? " " : "*") << log::Duration(cur_time - c->m_connectedTime) - << '\t' << c->m_customDiff - << '\t' << c->m_customUser + int addr_len = 0; + for (const StratumClient* c = static_cast(m_connectedClientsList->m_next); c != m_connectedClientsList; c = static_cast(c->m_next)) { + addr_len = std::max(addr_len, static_cast(strlen(c->m_addrString))); + } + + size_t n = 0; + + for (const StratumClient* c = static_cast(m_connectedClientsList->m_next); c != m_connectedClientsList; c = static_cast(c->m_next)) { + LOGINFO(0, log::pad_right(static_cast(c->m_addrString), addr_len + 8) + << log::pad_right(log::Duration(cur_time - c->m_connectedTime), 20) + << log::pad_right(c->m_customDiff, 20) + << (c->m_rpcId ? c->m_customUser.data() : "not logged in") ); + ++n; } + + LOGINFO(0, "Total: " << n << " workers"); } void StratumServer::reset_share_counters()