upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h42
1 files changed, 37 insertions, 5 deletions
diff --git a/src/util.h b/src/util.h
index c53293a..641b838 100644
--- a/src/util.h
+++ b/src/util.h
@@ -26,11 +26,14 @@ typedef unsigned long long uint64;
26#define UEND(a) ((unsigned char*)&((&(a))[1])) 26#define UEND(a) ((unsigned char*)&((&(a))[1]))
27#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0])) 27#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
28 28
29// Route printf to debug.log (and the console) on the GUI Windows build and on 29// Route printf to debug.log (and the console on non-Windows) everywhere.
30// the headless Linux build alike. 30// (Previously gated on `defined(_WINDOWS) || !defined(_WIN32)` -- but
31#if defined(_WINDOWS) || !defined(_WIN32) 31// _WINDOWS is only ever defined by the legacy, unused makefile.vc build;
32// the real Windows build (makefile.mingw) never defines it, so on actual
33// Windows this condition was always false and debug.log was never written.
34// OutputDebugStringF() is already platform-safe internally: it always
35// writes debug.log, and only echoes to the console when not on Windows.)
32#define printf OutputDebugStringF 36#define printf OutputDebugStringF
33#endif
34 37
35#ifdef snprintf 38#ifdef snprintf
36#undef snprintf 39#undef snprintf
@@ -70,6 +73,35 @@ inline T& REF(const T& val)
70 73
71extern bool fDebug; 74extern bool fDebug;
72 75
76// ---------------------------------------------------------------------------
77// Categorized, burst-collapsing logging
78//
79// Plain printf() (routed to OutputDebugStringF below) writes every call
80// unfiltered to debug.log. That's fine for one-off events, but a loop that
81// logs once per item -- once per discovered peer, once per connect attempt,
82// once per share -- turns debug.log into a wall of near-identical lines that
83// differ only by an address, while the line that actually explains a stuck
84// flow ("Stratum: no job available for ...") scrolls off screen under the
85// noise. LogPrint() is a drop-in replacement for printf() that fixes both
86// problems without touching call sites' formatting:
87//
88// 1. Category tag: every call names the subsystem it belongs to ("pool",
89// "worker", "payout", "net", "nostr", ...). LogAcceptsCategory()
90// controls what's written (see util.cpp); the pool/worker/payout flow
91// the person actually needs visibility into is on by default.
92// 2. Burst collapse: repeated LogPrint() calls from the *same source line*
93// only emit one line per DEDUP_WINDOW_SECS; if more calls land inside
94// that window they're counted instead of reprinted, and the count is
95// flushed as a single "(xN similar in Ws)" line the moment the window
96// rolls over. A loop discovering 500 peers by address now produces a
97// small, bounded number of lines instead of 500 -- log growth is capped
98// by elapsed time, not by how many similar events happen to fire.
99// ---------------------------------------------------------------------------
100bool LogAcceptsCategory(const char* category);
101void LogPrintDedup(const char* category, const char* file, int line, const std::string& msg);
102#define LogPrint(category, ...) \
103 do { if (LogAcceptsCategory(category)) LogPrintDedup((category), __FILE__, __LINE__, strprintf(__VA_ARGS__)); } while (0)
104
73void RandAddSeed(bool fPerfmon=false); 105void RandAddSeed(bool fPerfmon=false);
74int my_snprintf(char* buffer, size_t limit, const char* format, ...); 106int my_snprintf(char* buffer, size_t limit, const char* format, ...);
75string strprintf(const char* format, ...); 107string strprintf(const char* format, ...);
@@ -231,7 +263,7 @@ void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSp
231 263
232 264
233 265
234// Forward declaration — defined in main.cpp 266// Forward declaration -- defined in main.cpp
235string GetAppDir(); 267string GetAppDir();
236 268
237inline int OutputDebugStringF(const char* pszFormat, ...) 269inline int OutputDebugStringF(const char* pszFormat, ...)