From efa02459e956165d928d04dd2a73a9da9909950f Mon Sep 17 00:00:00 2001 From: Audric Date: Sat, 18 Apr 2026 21:02:42 +0200 Subject: [PATCH] logging: address code-review feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix a critical regression where startup errors emitted before geulog::configure() (bad --config path, unknown CLI flag, --version banner) were queued into the async deque with no running worker and silently dropped on exit(). detail::enqueue() now falls back to synchronous ::write() to stdout/stderr when the worker is inactive. - Remove the dead TRUNK_DEBUG parse block in Reflector.cpp and the m_trunk_debug member in Reflector.h — all gates were already removed in Phase 1; only the (unread) parse remained. - Replace the #TRUNK_DEBUG=0 comment in svxreflector.conf.in with a documented LOG= reference block. - Restore the [host:port] peer identity on the ReflectorClient ENOTCONN debug line so sysops can still correlate failures. - Revert sighup_handler and sigterm_handler to async-signal-safe ::write(STDERR_FILENO, ...) instead of calling the (mutex-taking, allocating) facade from signal context. - Retag two MsgTrunk* diagnostic sites in ReflectorMsg.h from core to trunk to match the enclosing-class rule. - Doc: note that argument expressions are evaluated at the call site before the shouldLog() gate runs, and reword the "never drops" claim to acknowledge rare short-write truncation. --- docs/LOGGING.md | 15 ++++++++---- src/misc/Log.cpp | 15 ++++++++++++ src/misc/Log.h | 11 +++++++++ src/svxlink/reflector/Reflector.cpp | 12 ---------- src/svxlink/reflector/Reflector.h | 1 - src/svxlink/reflector/ReflectorClient.cpp | 3 ++- src/svxlink/reflector/ReflectorMsg.h | 4 ++-- src/svxlink/reflector/svxreflector.conf.in | 14 ++++++++++- src/svxlink/reflector/svxreflector.cpp | 28 +++++++++++++++------- 9 files changed, 73 insertions(+), 30 deletions(-) diff --git a/docs/LOGGING.md b/docs/LOGGING.md index bca3584..931c910 100644 --- a/docs/LOGGING.md +++ b/docs/LOGGING.md @@ -216,7 +216,14 @@ boolean flag could not do. rules a line out, the call site checks one `std::atomic` and returns without formatting arguments. Debug lines on per-frame paths cost nothing at runtime when the level is `warn`. -- **The worker never drops.** If the queue grows unboundedly (writer - consumer is paused), the process accumulates memory rather than - losing lines. In practice this does not happen; if you ever observe - it, it is a sign of a genuinely stuck log consumer. +- **The worker never drops a line intentionally.** Queue growth is + unbounded — if the log consumer stalls (full pipe, paused + `logrotate`), memory grows rather than lines being dropped. A rare + short `write(2)` to a stalled pipe can truncate the tail of one + line; the line header is preserved. +- **Argument expressions are still evaluated** on every call, even when + the subsystem is filtered out — the gate skips formatting, not + evaluation. Call sites doing heavy work inside the argument list (an + RPC, a large string format) should guard with an explicit + `geulog::shouldLog(...)` check first. Not a concern at current call + sites (all pass cheap member accesses). diff --git a/src/misc/Log.cpp b/src/misc/Log.cpp index 611a039..7737f2c 100644 --- a/src/misc/Log.cpp +++ b/src/misc/Log.cpp @@ -33,6 +33,7 @@ std::condition_variable g_queue_cv; std::deque g_queue; bool g_stop = false; // guarded by g_queue_mu std::thread g_worker; +std::atomic g_worker_active{false}; int subIndex(const char* sub) { for (std::size_t i = 0; i < NUM_SUBSYSTEMS; ++i) { @@ -95,6 +96,7 @@ void startWorker() { g_stop = false; } g_worker = std::thread(workerLoop); + g_worker_active.store(true, std::memory_order_release); } } // anonymous namespace @@ -118,6 +120,18 @@ void enqueue(Level lvl, const char* sub, std::string&& msg) { line += "] "; line += msg; line += "\n"; + + if (!g_worker_active.load(std::memory_order_acquire)) { + // Worker not running (pre-configure, post-shutdown, or configure + // aborted before startWorker). Write synchronously so startup + // errors and --version output still reach the sysop. warn/error + // go to stderr; info/debug/trace to stdout (matches the + // pre-facade std::cerr / std::cout split). + int fd = (lvl >= Level::Warn) ? STDERR_FILENO : STDOUT_FILENO; + (void)::write(fd, line.data(), line.size()); + return; + } + { std::lock_guard lock(g_queue_mu); g_queue.push_back(std::move(line)); @@ -191,6 +205,7 @@ void shutdown() { } g_queue_cv.notify_all(); if (g_worker.joinable()) g_worker.join(); + g_worker_active.store(false, std::memory_order_release); } bool setLevel(const std::string& sub, const std::string& lvl_s) { diff --git a/src/misc/Log.h b/src/misc/Log.h index 151fff9..37d45fe 100644 --- a/src/misc/Log.h +++ b/src/misc/Log.h @@ -71,6 +71,17 @@ namespace detail { } } +// NOTE: argument expressions passed to geulog::debug/info/... are +// evaluated at the call site BEFORE shouldLog() runs. The gate skips +// formatting (the ostringstream + concat), not evaluation. For call +// sites where argument evaluation is expensive, guard with an explicit +// shouldLog() check: +// +// if (geulog::shouldLog("trunk", geulog::Level::Debug)) +// geulog::debug("trunk", expensive_rpc_call()); +// +// All current call sites pass cheap member accesses, so this is only +// a concern for future migrations with non-trivial argument work. #define _LOG_DEFINE(fnname, lvlenum) \ template \ inline void fnname(const char* sub, Args&&... args) { \ diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index 498c41f..d10dd3f 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -524,18 +524,6 @@ bool Reflector::initialize(Async::Config &cfg) return true; } - // TRUNK_DEBUG — verbose logging for diagnosing trunk connection issues - std::string trunk_debug_str; - if (cfg.getValue("GLOBAL", "TRUNK_DEBUG", trunk_debug_str)) - { - m_trunk_debug = (trunk_debug_str == "1" || trunk_debug_str == "true" - || trunk_debug_str == "yes"); - if (m_trunk_debug) - { - geulog::info("core", "Trunk debug logging enabled"); - } - } - initTrunkLinks(); initTrunkServer(); initTwinLink(); diff --git a/src/svxlink/reflector/Reflector.h b/src/svxlink/reflector/Reflector.h index 2c651b4..9449880 100644 --- a/src/svxlink/reflector/Reflector.h +++ b/src/svxlink/reflector/Reflector.h @@ -342,7 +342,6 @@ class Reflector : public sigc::trackable std::map> m_peer_node_cache; std::set m_redis_trunk_sections; // sections loaded from Redis std::set m_cluster_tgs; - bool m_trunk_debug = false; Async::Timer m_nodelist_timer; static const size_t TRUNK_MAX_PENDING_CONS = 5; diff --git a/src/svxlink/reflector/ReflectorClient.cpp b/src/svxlink/reflector/ReflectorClient.cpp index 23a50c0..91b3c57 100644 --- a/src/svxlink/reflector/ReflectorClient.cpp +++ b/src/svxlink/reflector/ReflectorClient.cpp @@ -300,7 +300,8 @@ int ReflectorClient::sendMsg(const ReflectorMsg& msg) return ret; } } - geulog::debug("client", "sendMsg: write failed: ", strerror(errno), + geulog::debug("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] sendMsg: write failed: ", strerror(errno), " (msg type ", msg.type(), ")"); disconnect(); return -1; diff --git a/src/svxlink/reflector/ReflectorMsg.h b/src/svxlink/reflector/ReflectorMsg.h index 8f3d194..11af96d 100644 --- a/src/svxlink/reflector/ReflectorMsg.h +++ b/src/svxlink/reflector/ReflectorMsg.h @@ -1439,13 +1439,13 @@ class MsgTrunkHello : public ReflectorMsgBase<115> int rc = RAND_bytes(&m_nonce.front(), NONCE_LEN); if (rc != 1) { - geulog::warn("core", "RAND_bytes failed in MsgTrunkHello"); + geulog::warn("trunk", "RAND_bytes failed in MsgTrunkHello"); m_nonce.clear(); return; } if (!calcHMAC(m_digest, secret, m_nonce.data(), m_nonce.size())) { - geulog::error("core", "HMAC calculation failed in MsgTrunkHello"); + geulog::error("trunk", "HMAC calculation failed in MsgTrunkHello"); } } diff --git a/src/svxlink/reflector/svxreflector.conf.in b/src/svxlink/reflector/svxreflector.conf.in index 0ab1782..173299d 100644 --- a/src/svxlink/reflector/svxreflector.conf.in +++ b/src/svxlink/reflector/svxreflector.conf.in @@ -97,7 +97,19 @@ CERT_CA_HOOK=@SVX_SHARE_INSTALL_DIR@/ca-hook.py # #LOCAL_PREFIX=1 #TRUNK_LISTEN_PORT=5302 -#TRUNK_DEBUG=0 # verbose trunk logging (high log volume — disable after diagnosis) +# Per-subsystem logging levels. Comma-separated = pairs. +# '*' sets the default for subsystems not explicitly listed. +# Levels: trace, debug, info, warn, error, off. +# Subsystems: trunk, satellite, twin, client, mqtt, redis, core. +# +# Absent or empty => *=info. +# Live-reloadable via the command PTY: +# echo "LOG trunk=debug" > /dev/shm/reflector_ctrl +# echo "LOG SHOW" > /dev/shm/reflector_ctrl +# echo "LOG RESET" > /dev/shm/reflector_ctrl +# +# TRUNK_DEBUG is obsolete; use LOG=trunk=debug instead. +#LOG=*=info #[TRUNK_AB] #HOST=reflector-b.example.com diff --git a/src/svxlink/reflector/svxreflector.cpp b/src/svxlink/reflector/svxreflector.cpp index 7dc0ff8..b27d4ef 100644 --- a/src/svxlink/reflector/svxreflector.cpp +++ b/src/svxlink/reflector/svxreflector.cpp @@ -759,32 +759,42 @@ static void sighup_handler(int signal) { if (logfile_name == 0) { - geulog::info("core", "Ignoring SIGHUP"); + static const char MSG[] = "Ignoring SIGHUP\n"; + (void)::write(STDERR_FILENO, MSG, sizeof(MSG) - 1); return; } - geulog::info("core", "SIGHUP received"); + static const char MSG[] = "SIGHUP received\n"; + (void)::write(STDERR_FILENO, MSG, sizeof(MSG) - 1); logwriter.reopenLogfile(); } /* sighup_handler */ static void sigterm_handler(int signal) { - const char *signame = 0; switch (signal) { case SIGTERM: - signame = "SIGTERM"; + { + static const char MSG[] = + "NOTICE: SIGTERM received. Shutting down application...\n"; + (void)::write(STDERR_FILENO, MSG, sizeof(MSG) - 1); break; + } case SIGINT: - signame = "SIGINT"; + { + static const char MSG[] = + "NOTICE: SIGINT received. Shutting down application...\n"; + (void)::write(STDERR_FILENO, MSG, sizeof(MSG) - 1); break; + } default: - signame = "Unknown signal"; + { + static const char MSG[] = + "NOTICE: Unknown signal received. Shutting down application...\n"; + (void)::write(STDERR_FILENO, MSG, sizeof(MSG) - 1); break; + } } - - geulog::info("core", "NOTICE: ", signame, - " received. Shutting down application..."); Application::app().quit(); } /* sigterm_handler */