#ifndef HYPERSWARM_DEBUG_LOG_H
#define HYPERSWARM_DEBUG_LOG_H
// Debug logging control.
// Compile-time: define HYPERSWARM_DEBUG=0 to hard-strip every DHT_LOG call.
// Runtime (when compiled in): logging is OFF by default — set env HSP_VERBOSE=1
// (or HYPERSWARM_DEBUG=1) to enable verbose DHT/connector/RPC/protomux output.
// Without this gate the per-packet DHT_LOG calls flood stderr (hundreds of
// lines/sec when meshed — one pair per inbound packet) and cost frames. The
// macro used to default ON; it is now quiet by default. See hyper-tps
// spec/next-fixes.md A2.
#include <cstdio>
#include <cstdlib>
#ifndef HYPERSWARM_DEBUG
#define HYPERSWARM_DEBUG 1
#endif
#if HYPERSWARM_DEBUG
// Evaluated once (C++11 thread-safe static init). Quiet unless an env flag is set.
inline bool hsp_log_enabled() {
static const bool enabled =
(std::getenv("HSP_VERBOSE") != nullptr) ||
(std::getenv("HYPERSWARM_DEBUG") != nullptr);
return enabled;
}
#define DHT_LOG(fmt, ...) \
do { if (hsp_log_enabled()) fprintf(stderr, fmt, ##__VA_ARGS__); } while (0)
// Focused discovery channel. Emits ONLY the announce/commit/token/lookup-result
// lines needed to diagnose two-peer mesh discovery (hyper-fleeky-dev #93) —
// none of these fire per-packet, so the stream stays readable. Enabled by
// HSP_DISCOVERY=1 (focused) OR HSP_VERBOSE=1 (full firehose includes it).
// Use HSP_DISCOVERY=1 to see the announce->commit->lookup chain WITHOUT the
// per-packet flood that truncated scrollback last session. See spec/next-fixes.md A2.
inline bool hsp_disc_enabled() {
static const bool enabled =
(std::getenv("HSP_DISCOVERY") != nullptr) ||
(std::getenv("HSP_VERBOSE") != nullptr) ||
(std::getenv("HYPERSWARM_DEBUG") != nullptr);
return enabled;
}
#define DHT_DISC(fmt, ...) \
do { if (hsp_disc_enabled()) fprintf(stderr, fmt, ##__VA_ARGS__); } while (0)
#else
#define DHT_LOG(fmt, ...) ((void)0)
#define DHT_DISC(fmt, ...) ((void)0)
#endif
#endif // HYPERSWARM_DEBUG_LOG_H