-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add ENVOY_LOG_ONCE #12830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ENVOY_LOG_ONCE #12830
Changes from all commits
8f8b17b
6770737
72829d5
63b8a08
9e29f4a
2d47780
99e9686
d8a6304
aa67ebb
d8783b7
371fa5d
9480137
09ff427
de23f2a
53d5cc1
2ab747d
536300e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #pragma once | ||
|
|
||
| #include <bitset> | ||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
@@ -400,6 +401,35 @@ template <Id id> class Loggable { | |
| } \ | ||
| } while (0) | ||
|
|
||
| #define ENVOY_LOG_FIRST_N(LEVEL, N, ...) \ | ||
| do { \ | ||
| static auto* countdown = new std::atomic<uint64_t>(); \ | ||
| if (countdown->fetch_add(1) < N) { \ | ||
| ENVOY_LOG(LEVEL, ##__VA_ARGS__); \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| #define ENVOY_LOG_ONCE(LEVEL, ...) \ | ||
| do { \ | ||
| ENVOY_LOG_FIRST_N(LEVEL, 1, ##__VA_ARGS__); \ | ||
| } while (0) | ||
|
|
||
| #define ENVOY_LOG_EVERY_NTH(LEVEL, N, ...) \ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per earlier discussion, every Pow2 could be good as well.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yes good catch, my intent was actually to add that. Will do.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, added
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /cc @ggreenway ^^ |
||
| do { \ | ||
| static auto* count = new std::atomic<uint64_t>(); \ | ||
| if ((count->fetch_add(1) % N) == 0) { \ | ||
| ENVOY_LOG(LEVEL, ##__VA_ARGS__); \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| #define ENVOY_LOG_EVERY_POW_2(LEVEL, ...) \ | ||
| do { \ | ||
| static auto* count = new std::atomic<uint64_t>(); \ | ||
| if (std::bitset<64>(1 + count->fetch_add(1)).count() == 1) { \ | ||
| ENVOY_LOG(LEVEL, ##__VA_ARGS__); \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| #define ENVOY_FLUSH_LOG() \ | ||
| do { \ | ||
| if (Envoy::Logger::Context::useFancyLogger()) { \ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't thread-safe. If two threads do this at the same time, there could be a leak. I think this ends up being better as
static std::atomic<uint64_t> countdown;. Either the compiler will see that it is initialized by zeroing, and do so at program start, or it will double-zero the same memory by initializing multiple times, but should avoid a leak at least.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't that lead up to a potential deinitialization fiasco? I was following the lead of
envoy/source/common/common/macros.h
Line 32 in f2306b3
Reading up I am led to believe this would be thread safe since c++ 11 but I can't find a source that I would consider authorative,. Will dig some more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relevant on the subject of leaking and stack vs heap allocation in the construct on first use idiom: Why doesn’t the Construct On First Use Idiom use a static object instead of a static pointer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables has:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some reading, and you're correct. TIL something new about static initialization.