Skip to content

Commit

Permalink
Added float type support for timed logging, made lpp backend templated
Browse files Browse the repository at this point in the history
  • Loading branch information
4c3y committed Apr 5, 2024
1 parent 1c1b137 commit 5cae9fe
Showing 1 changed file with 49 additions and 27 deletions.
76 changes: 49 additions & 27 deletions include/log++.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, LPP_INTL::PolicyType::EVERY_N>(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<int, LPP_INTL::PolicyType::FIRST_N>(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<float, LPP_INTL::PolicyType::TIMED>(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
Expand Down Expand Up @@ -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<typename T>
class LogPolicy : public LogPolicyBase {
public:
explicit LogPolicy(T max) : max_(max) {}
protected:
T max_{0};
};

class CountableLogPolicy : public LogPolicy<int> {
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_;
}

Expand All @@ -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_++;
Expand All @@ -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<float> {
public:
explicit TimePolicy(int max) : LogPolicy(max) {};
explicit TimePolicy(float max) : LogPolicy(max) {};

inline void update() override {
now_ = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
}

[[nodiscard]] inline bool shouldLog() const override {
if (now_ >= last_ + (max_ * 1000000)) {
if (now_ >= last_ + static_cast<long>(max_ * 1000000.f)) {
return true;
}
return false;
Expand All @@ -739,6 +751,7 @@ class TimePolicy : public LogPolicy {
last_ = now_;
}

virtual ~TimePolicy() = default;
private:
long now_{0};
long last_{0};
Expand All @@ -750,11 +763,12 @@ enum PolicyType {
TIMED
};

typedef std::shared_ptr<LogPolicy> LogPolicyPtr;
typedef std::shared_ptr<LogPolicyBase> LogPolicyPtr;

class LogPolicyFactory {
public:
static LogPolicyPtr create(PolicyType policy_type, int max) {
template<typename T>
static LogPolicyPtr create(PolicyType policy_type, T max) {
switch (policy_type) {
case FIRST_N: return std::make_shared<FirstNOccurrencesPolicy>(max);
case EVERY_N: return std::make_shared<OccasionPolicy>(max);
Expand All @@ -779,16 +793,23 @@ class InternalLogCount {
return instance;
}

template<typename T, PolicyType policyType>
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<T, int>::value || std::is_same<T, float>::value, "Only int or float is allowed for T");

if constexpr (std::is_same<T, float>::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<typename T>
inline void update(const std::string &key,
int max,
T max,
const std::string &log_msg,
const BaseSeverity base_severity,
PolicyType policy_type) {
Expand All @@ -799,8 +820,9 @@ class InternalLogCount {
mtx_.unlock();
}

template<typename T>
inline void updatePolicy(const std::string &key,
int max,
T max,
const std::string &log_msg,
BaseSeverity base_severity,
PolicyType policy_type) {
Expand Down Expand Up @@ -880,10 +902,10 @@ class InternalGlogLogStringLog : public InternalLog {
std::vector<std::string>* vecptr_;
};


template<typename T>
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;
Expand All @@ -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<int> {
public:
LppGlogExtensionLog(std::string key, int n, GlogSeverity glog_severity, PolicyType policy_type,
std::function<void(const std::string &str)> fn) :
Expand Down

0 comments on commit 5cae9fe

Please sign in to comment.