Commit 7e4c8713 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

XLOG_FIRST_N

Summary: Add `XLOG_FIRST_N` log function.

Reviewed By: simpkins

Differential Revision: D26184283

fbshipit-source-id: 23081f759897bbf7949ce603f8e5edb29869b40d
parent 4eaba962
......@@ -396,6 +396,20 @@ TEST_F(XlogTest, rateLimiting) {
"2x int arg 7",
}));
handler->clearMessages();
// Test XLOG_FIRST_N
for (size_t n = 0; n < 10; ++n) {
XLOG_FIRST_N(DBG1, 4, "bah ", n);
}
EXPECT_THAT(
handler->getMessageValues(),
ElementsAreArray({
"bah 0",
"bah 1",
"bah 2",
"bah 3",
}));
handler->clearMessages();
}
TEST_F(XlogTest, rateLimitingEndOfThread) {
......
......@@ -234,6 +234,35 @@ FOLLY_EXPORT FOLLY_ALWAYS_INLINE bool xlogEveryNThreadImpl(size_t n) {
return folly_detail_xlog_limiter.check(); \
}(), \
##__VA_ARGS__)
namespace folly {
namespace detail {
template <typename Tag>
FOLLY_EXPORT FOLLY_ALWAYS_INLINE bool xlogFirstNExactImpl(std::size_t n) {
static std::atomic<std::size_t> counter{0};
auto const value = counter.load(std::memory_order_relaxed);
return value < n && counter.fetch_add(1, std::memory_order_relaxed) < n;
}
} // namespace detail
} // namespace folly
/**
* Similar to XLOG(...) except only log a message the first n times, exactly.
*
* The internal counter is process-global and threadsafe and exchanges are
* atomic.
*/
#define XLOG_FIRST_N(level, n, ...) \
XLOG_IF( \
level, \
[&] { \
struct folly_detail_xlog_tag {}; \
return ::folly::detail::xlogFirstNExactImpl<folly_detail_xlog_tag>(n); \
}(), \
##__VA_ARGS__)
/**
* FOLLY_XLOG_STRIP_PREFIXES can be defined to a string containing a
* colon-separated list of directory prefixes to strip off from the filename
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment