diff --git a/aixlog_example.cpp b/aixlog_example.cpp index 46aa704..20a7540 100644 --- a/aixlog_example.cpp +++ b/aixlog_example.cpp @@ -132,4 +132,16 @@ int main(int /*argc*/, char** /*argv*/) LOG(INFO) << every_x << "4th will not be logged\n"; LOG(INFO) << every_x << "5th will not be logged\n"; LOG(INFO) << every_x << "6th will be logged\n"; + + AixLog::Conditional not_every_3(AixLog::Conditional::EvalFunc([] { + static size_t n(0); + return (++n % 3 != 0); + })); + + LOG(INFO) << not_every_3 << "1st will be logged\n"; + LOG(INFO) << not_every_3 << "2nd will be logged\n"; + LOG(INFO) << not_every_3 << "3rd will not be logged\n"; + LOG(INFO) << not_every_3 << "4th will be logged\n"; + LOG(INFO) << not_every_3 << "5th will be logged\n"; + LOG(INFO) << not_every_3 << "6th will not be logged\n"; } diff --git a/include/aixlog.hpp b/include/aixlog.hpp index 42f9eda..3d0d30c 100644 --- a/include/aixlog.hpp +++ b/include/aixlog.hpp @@ -269,11 +269,17 @@ struct TextColor */ struct Conditional { - Conditional() : Conditional(true) + using EvalFunc = std::function; + + Conditional() : func_([](void) { return true; }) + { + } + + Conditional(const EvalFunc& func) : func_(func) { } - Conditional(bool value) : is_true_(value) + Conditional(bool value) : func_([value](void) { return value; }) { } @@ -281,11 +287,11 @@ struct Conditional virtual bool is_true() const { - return is_true_; + return func_(); } protected: - bool is_true_; + EvalFunc func_; }; /**