From 5cae9fe5cec7358e251fba6fd3671d765fbd77b6 Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Fri, 5 Apr 2024 13:26:31 +0200 Subject: [PATCH] Added float type support for timed logging, made lpp backend templated --- include/log++.h | 76 +++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/include/log++.h b/include/log++.h index aa99bb9..1951671 100644 --- a/include/log++.h +++ b/include/log++.h @@ -390,9 +390,9 @@ if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I #endif #if defined MODE_ROSLOG || defined MODE_LPP || defined MODE_DEFAULT -#define LOG_EVERY(severity, n, x) LPP_INTL::InternalLogCount::getInstance().update(LPP_GET_KEY(), n, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity), LPP_INTL::PolicyType::EVERY_N) // NOLINT(bugprone-macro-parentheses) -#define LOG_FIRST(severity, n, x) LPP_INTL::InternalLogCount::getInstance().update(LPP_GET_KEY(), n, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity), LPP_INTL::PolicyType::FIRST_N) // NOLINT(bugprone-macro-parentheses) -#define LOG_TIMED(severity, t, x) LPP_INTL::InternalLogCount::getInstance().update(LPP_GET_KEY(), t, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity), LPP_INTL::PolicyType::TIMED) // NOLINT(bugprone-macro-parentheses) +#define LOG_EVERY(severity, n, x) LPP_INTL::InternalLogCount::getInstance().update(LPP_GET_KEY(), n, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity)) // NOLINT(bugprone-macro-parentheses) +#define LOG_FIRST(severity, n, x) LPP_INTL::InternalLogCount::getInstance().update(LPP_GET_KEY(), n, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity)) // NOLINT(bugprone-macro-parentheses) +#define LOG_TIMED(severity, t, x) LPP_INTL::InternalLogCount::getInstance().update(LPP_GET_KEY(), t, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity)) // NOLINT(bugprone-macro-parentheses) #endif #if defined MODE_ROSLOG || defined MODE_LPP || MODE_NOLOG @@ -661,21 +661,31 @@ class InternalCondLog : public InternalLog { }; -class LogPolicy { +class LogPolicyBase { public: virtual void update() = 0; [[nodiscard]] virtual bool shouldLog() const = 0; virtual void onLog() {}; - virtual ~LogPolicy() = default; - protected: - explicit LogPolicy(int max) : max_(max) {} +}; + +template +class LogPolicy : public LogPolicyBase { +public: + explicit LogPolicy(T max) : max_(max) {} +protected: + T max_{0}; +}; + +class CountableLogPolicy : public LogPolicy { +public: + explicit CountableLogPolicy(int max) : LogPolicy(max) {} +protected: int counter_{0}; - int max_{0}; }; -class OccasionPolicy : public LogPolicy { +class OccasionPolicy : public CountableLogPolicy { public: - explicit OccasionPolicy(int max) : LogPolicy(max) { + explicit OccasionPolicy(int max) : CountableLogPolicy(max) { counter_ = max_; } @@ -693,13 +703,14 @@ class OccasionPolicy : public LogPolicy { return should_log_; } + virtual ~OccasionPolicy() = default; private: bool should_log_{false}; }; -class FirstNOccurrencesPolicy : public LogPolicy { +class FirstNOccurrencesPolicy : public CountableLogPolicy { public: - explicit FirstNOccurrencesPolicy(int max) : LogPolicy(max) {} + explicit FirstNOccurrencesPolicy(int max) : CountableLogPolicy(max) {} inline void update() override { if (!is_n_occurences_reached) { counter_++; @@ -714,22 +725,23 @@ class FirstNOccurrencesPolicy : public LogPolicy { return !is_n_occurences_reached; } + virtual ~FirstNOccurrencesPolicy() = default; private: bool is_n_occurences_reached = false; }; using namespace std::chrono; -class TimePolicy : public LogPolicy { +class TimePolicy : public LogPolicy { public: - explicit TimePolicy(int max) : LogPolicy(max) {}; + explicit TimePolicy(float max) : LogPolicy(max) {}; inline void update() override { now_ = duration_cast(system_clock::now().time_since_epoch()).count(); } [[nodiscard]] inline bool shouldLog() const override { - if (now_ >= last_ + (max_ * 1000000)) { + if (now_ >= last_ + static_cast(max_ * 1000000.f)) { return true; } return false; @@ -739,6 +751,7 @@ class TimePolicy : public LogPolicy { last_ = now_; } + virtual ~TimePolicy() = default; private: long now_{0}; long last_{0}; @@ -750,11 +763,12 @@ enum PolicyType { TIMED }; -typedef std::shared_ptr LogPolicyPtr; +typedef std::shared_ptr LogPolicyPtr; class LogPolicyFactory { public: - static LogPolicyPtr create(PolicyType policy_type, int max) { + template + static LogPolicyPtr create(PolicyType policy_type, T max) { switch (policy_type) { case FIRST_N: return std::make_shared(max); case EVERY_N: return std::make_shared(max); @@ -779,16 +793,23 @@ class InternalLogCount { return instance; } + template inline void update(const std::string &key, - int max, + T max, const InternalLog &internal_log, - const BaseSeverity base_severity, - PolicyType policy_type) { - update(key, max, internal_log.ss.str(), base_severity, policy_type); + const BaseSeverity base_severity) { + //Check if T is a float and only allow it for PolicyType::TIMED + static_assert(std::is_same::value || std::is_same::value, "Only int or float is allowed for T"); + + if constexpr (std::is_same::value) { + static_assert(policyType == PolicyType::TIMED, "Only PolicyType::TIMED can be used with float"); + } + update(key, max, internal_log.ss.str(), base_severity, policyType); } + template inline void update(const std::string &key, - int max, + T max, const std::string &log_msg, const BaseSeverity base_severity, PolicyType policy_type) { @@ -799,8 +820,9 @@ class InternalLogCount { mtx_.unlock(); } + template inline void updatePolicy(const std::string &key, - int max, + T max, const std::string &log_msg, BaseSeverity base_severity, PolicyType policy_type) { @@ -880,10 +902,10 @@ class InternalGlogLogStringLog : public InternalLog { std::vector* vecptr_; }; - +template class InternalPolicyLog : public InternalLog { public: - InternalPolicyLog(std::string key, int n, BaseSeverity base_severity, PolicyType policy_type) : + InternalPolicyLog(std::string key, T n, BaseSeverity base_severity, PolicyType policy_type) : InternalLog(base_severity), key_(std::move(key)), n_(n), policy_type_(policy_type) { should_print_ = false; @@ -898,11 +920,11 @@ class InternalPolicyLog : public InternalLog { protected: bool should_update_{true}; std::string key_{}; - int n_{}; + T n_{}; PolicyType policy_type_{}; }; -class LppGlogExtensionLog : public InternalPolicyLog { +class LppGlogExtensionLog : public InternalPolicyLog { public: LppGlogExtensionLog(std::string key, int n, GlogSeverity glog_severity, PolicyType policy_type, std::function fn) :