Skip to content

Commit

Permalink
Add FARM_WARN_EVERY_N() macro
Browse files Browse the repository at this point in the history
Sometimes it's helpful to only send warnings periodically instead of
constantly. This adds a macro for that which mimics the INFO version.
  • Loading branch information
Wylder Keane authored and wylderkeane committed Mar 27, 2024
1 parent f25ea36 commit 7eb19c5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cpp/farm_ng/core/logging/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ inline StreamLogger& defaultLogger() {
} \
} while (false)

/// LOG WARN: Only log every nth encounter.
#define FARM_WARN_EVERY_N(N, ...) \
do { \
static std::atomic<int> counter(0); \
++counter; \
if (counter > (N)) { \
counter -= (N); \
} \
if (counter == 1) { \
farm_ng::defaultLogger().log( \
farm_ng::LogLevel::warning, \
FARM_FORMAT("LOG WARN EVERY N( = {} )", #N), \
__FILE__, \
__LINE__, \
__func__ FARM_MAYBE_VARGS(__VA_ARGS__)(__VA_ARGS__)); \
} \
} while (false)

/// If condition is false, Print formatted error message and then panic.
#define FARM_ASSERT(condition, ...) \
do { \
Expand Down

0 comments on commit 7eb19c5

Please sign in to comment.