util: a log_once for the warnings that speak once per process

Three sites -- the kvstream clamp (L-02), the batch overlap guard and
the bidi-plane belt -- each open-coded the same four lines: a function-
local static atomic<bool>, a bool expected, and a compare_exchange to
decide which of the many steps gets to print. The pattern is the point,
not the flag name (said, said2), so it moved to rad_internal.h as
log_once(flag): true on the first call, false after.

No behaviour change -- same first-of-many warnings, same compare-exchange
thread-safety, exactly one winner per flag -- only the idiom is now named
instead of re-typed where a fourth once-warning is ever needed.
This commit is contained in:
fl0l0u committed 2026-11-15 22:02:29 +00:00
1 parent b9ecbde0d6
commit c3a4775f54
3 files changed
+12 -6

No files matched your search

+9
View File
@@ -10,6 +10,7 @@
#include <cstdio>
#include <cstdarg>
#include <atomic>
#include <string>
#include <string_view>
#include <vector>
@@ -35,6 +36,14 @@ uint64_t log_last_line_ns();
* Multi-byte UTF-8 is left alone; only control bytes are replaced. */
std::string log_sanitize(const std::string& s);
/* The once-log idiom: true on the first call for this flag, false forever after -- a warning
* that would otherwise repeat every step logs its first occurrence and stays quiet. Thread-safe
* by compare_exchange, so exactly one thread wins. */
inline bool log_once(std::atomic<bool>& flag) {
bool expected = false;
return flag.compare_exchange_strong(expected, true);
}
/* The whole text of a file, refused BEFORE it is read when it is bigger than `cap`. Files a
* container supplies are screened by size as well as by JSON budget: the bounded read counts
* values, not bytes, and the slurp used to be the unbounded step -- a multi-gigabyte