From 349022e1120a4261771391d3f6463b4da1ffdbbc Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:44:43 +0200 Subject: [PATCH 01/11] Fixed LOG_TIMED and ROS_INFO_THROTTLE macros in MODE_GLOG --- CMakeLists.txt | 1 + include/log++.h | 46 +++++++++++++- test/glog/test_glog_timed.cc | 115 +++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 test/glog/test_glog_timed.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 25b6377..70f78d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,7 @@ if (GLOG_FOUND AND catkin_FOUND AND LPP_BUILD_TESTS) test/glog/test_glog_if_every_n.cc test/glog/test_glog_log_string.cc test/glog/test_glog_rosprintf.cc + test/glog/test_glog_timed.cc test/glog/test_glog_vlog.cc) target_include_directories(${GLOG_TESTS} PRIVATE ${LPP_INCLUDE_DIRECTORIES} test/glog) diff --git a/include/log++.h b/include/log++.h index 2a67bae..157b322 100644 --- a/include/log++.h +++ b/include/log++.h @@ -278,10 +278,15 @@ inline void LOG_INIT([[maybe_unused]] char *argv, [[maybe_unused]] const std::fu #define LOG_1(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() #ifndef LOG_EVERY_T +#define LPP_INTL_LOG_EVERY_T(severity, t) LPP_WARN("LOG_EVERY_T is only defined in GLOG v0.6 or newer. File name and line numbers will be invalid in the log output.") \ +LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(severity), LPP_INTL::PolicyType::TIMED) + #define LOG_EVERY_T(severity, t) LPP_WARN("LOG_EVERY_T is only defined in GLOG v0.6 or newer.") \ LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) #endif +#define LPP_INTL_DLOG_EVERY_T(severity, t) LPP_WARN("File name and line numbers will be invalid in the log output.") LOG_EVERY_T(severity, t) + #ifndef DLOG_EVERY_T #define DLOG_EVERY_T(severity, t) LPP_WARN("DLOG_EVERY_T is a Log++ extension") \ LPP_INTL::LppGlogExtensionLog(LPP_GET_KEY(), t, LPP_INTL::GlogSeverity::severity, LPP_INTL::PolicyType::TIMED, [](const std::string& str) {LOG_1(severity) << str;}) @@ -321,6 +326,15 @@ else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::E) { else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) {LOG_FIRST_N(FATAL, n) << x;} \ } while(0) +#define LOG_TIMED(severity, t, x) do { \ +if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I \ + || LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::W \ + || LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::E \ + || LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F \ + || LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::D \ + ){LPP_INTL_LOG_EVERY_T(LppSeverity::severity, t) << x;} \ +} while(0) + #ifndef MODE_DEFAULT #define ROS_DEBUG(...) DLOG(INFO) << LPP_INTL::formatToString(__VA_ARGS__) #define ROS_DEBUG_STREAM(x) DLOG(INFO) << x @@ -349,6 +363,17 @@ else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) { #define ROS_WARN_ONCE(...) LOG_FIRST_N(WARNING, 1) << LPP_INTL::formatToString(__VA_ARGS__) #define ROS_ERROR_ONCE(...) LOG_FIRST_N(ERROR, 1) << LPP_INTL::formatToString(__VA_ARGS__) #define ROS_FATAL_ONCE(...) LOG_FIRST_N(FATAL, 1) << LPP_INTL::formatToString(__VA_ARGS__) + +#define ROS_DEBUG_THROTTLE(n, ...) LPP_INTL_DLOG_EVERY_T(INFO, n) << LPP_INTL::formatToString(__VA_ARGS__); +#define ROS_DEBUG_STREAM_THROTTLE(n, x) LPP_INTL_DLOG_EVERY_T(INFO, n) << x +#define ROS_INFO_THROTTLE(n, ...) LPP_INTL_LOG_EVERY_T(LppSeverity::I, n) << LPP_INTL::formatToString(__VA_ARGS__); +#define ROS_INFO_STREAM_THROTTLE(n, x) LPP_INTL_LOG_EVERY_T(LppSeverity::I, n) << x +#define ROS_WARN_THROTTLE(n, ...) LPP_INTL_LOG_EVERY_T(LppSeverity::W, n) << LPP_INTL::formatToString(__VA_ARGS__); +#define ROS_WARN_STREAM_THROTTLE(n, x) LPP_INTL_LOG_EVERY_T(LppSeverity::W, n) << x +#define ROS_ERROR_THROTTLE(n, ...) LPP_INTL_LOG_EVERY_T(LppSeverity::E, n) << LPP_INTL::formatToString(__VA_ARGS__); +#define ROS_ERROR_STREAM_THROTTLE(n, x) LPP_INTL_LOG_EVERY_T(LppSeverity::E, n) << x +#define ROS_FATAL_THROTTLE(n, ...) LPP_INTL_LOG_EVERY_T(LppSeverity::F, n) << LPP_INTL::formatToString(__VA_ARGS__); +#define ROS_FATAL_STREAM_THROTTLE(n, x) LPP_INTL_LOG_EVERY_T(LppSeverity::F, n) << x #endif #pragma clang diagnostic pop @@ -367,7 +392,7 @@ else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) { #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, n, x) LPP_INTL::InternalLogCount::getInstance().update(LPP_GET_KEY(), n, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity), LPP_INTL::PolicyType::TIMED) // NOLINT(bugprone-macro-parentheses) +#define LOG_TIMED(severity, t, x) LPP_INTL::InternalLogCount::getInstance().update(LPP_GET_KEY(), n, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity), LPP_INTL::PolicyType::TIMED) // NOLINT(bugprone-macro-parentheses) #endif #if defined MODE_ROSLOG || defined MODE_LPP || MODE_NOLOG @@ -594,6 +619,25 @@ class InternalLog { #endif #if defined MODE_LPP || defined MODE_DEFAULT lpp::internal::logging.call(severity_, ss.str()); +#endif +#if defined MODE_GLOG + switch (severity_) { + case BaseSeverity::DEBUG: + LOG(INFO) << ss.str(); + break; + case BaseSeverity::INFO: + LOG(INFO) << ss.str(); + break; + case BaseSeverity::WARN: + LOG(WARNING) << ss.str(); + break; + case BaseSeverity::ERROR: + LOG(ERROR) << ss.str(); + break; + case BaseSeverity::FATAL: + LOG(FATAL) << ss.str(); + break; + } #endif } diff --git a/test/glog/test_glog_timed.cc b/test/glog/test_glog_timed.cc new file mode 100644 index 0000000..01d9be8 --- /dev/null +++ b/test/glog/test_glog_timed.cc @@ -0,0 +1,115 @@ +// +// Created by 4c3y (acey) on 16.09.22. +// + +#include +#include +#include +#include + +std::vector generateTests() { + return { + {"glog_timed_lpp_syntax_severity_debug_Test","Test123",[]() { LOG_TIMED(D, 1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + {"glog_timed_lpp_syntax_severity_info_Test","Test123",[]() { LOG_TIMED(I, 1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + {"glog_timed_lpp_syntax_severity_warning_Test","Test123",[]() { LOG_TIMED(W, 1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + {"glog_timed_lpp_syntax_severity_error_Test","Test123",[]() { LOG_TIMED(E, 1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + //{"glog_timed_lpp_syntax_severity_fatal_Test","Test123",[]() { LOG_TIMED(F, 1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + + {"glog_timed_glog_syntax_severity_debug_Test","Test123",[]() { DLOG_EVERY_T(INFO, 1) << "Test" << 123; }, IS_SUBSTRING, STDERR}, + {"glog_timed_glog_syntax_severity_info_Test","Test123",[]() { LOG_EVERY_T(INFO, 1) << "Test" << 123; }, IS_SUBSTRING, STDERR}, + {"glog_timed_glog_syntax_severity_warning_Test","Test123",[]() { LOG_EVERY_T(WARNING, 1) << "Test" << 123; }, IS_SUBSTRING, STDERR}, + {"glog_timed_glog_syntax_severity_error_Test","Test123",[]() { LOG_EVERY_T(ERROR, 1) << "Test" << 123; }, IS_SUBSTRING, STDERR}, + //{"glog_timed_glog_syntax_severity_fatal_Test","Test123",[]() { LOG_EVERY_T(FATAL, 1) << "Test" << 123; }, IS_SUBSTRING, STDERR}, + + {"glog_timed_ros_syntax_severity_debug_Test", "Test123", []() {ROS_DEBUG_THROTTLE(1, "Test123"); }, IS_SUBSTRING, STDERR}, + {"glog_timed_ros_syntax_severity_debug_stream_Test", "Test123", []() {ROS_DEBUG_STREAM_THROTTLE(1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + {"glog_timed_ros_syntax_severity_info_Test", "Test123", []() {ROS_INFO_THROTTLE(1, "Test123"); }, IS_SUBSTRING, STDERR}, + {"glog_timed_ros_syntax_severity_info_stream_Test", "Test123", []() {ROS_INFO_STREAM_THROTTLE(1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + {"glog_timed_ros_syntax_severity_warning_Test", "Test123", []() {ROS_WARN_THROTTLE(1, "Test123"); }, IS_SUBSTRING, STDERR}, + {"glog_timed_ros_syntax_severity_warning_stream_Test", "Test123", []() {ROS_WARN_STREAM_THROTTLE(1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + {"glog_timed_ros_syntax_severity_error_Test", "Test123", []() {ROS_ERROR_THROTTLE(1, "Test123"); }, IS_SUBSTRING, STDERR}, + {"glog_timed_ros_syntax_severity_error_stream_Test", "Test123", []() {ROS_ERROR_STREAM_THROTTLE(1, "Test" << 123); }, IS_SUBSTRING, STDERR}, + //{"glog_timed_ros_syntax_severity_fatal_Test", "Test123", []() {ROS_FATAL_THROTTLE(1, "Test123"); }, IS_SUBSTRING, STDERR}, + //{"glog_timed_ros_syntax_severity_fatal_stream_Test", "Test123", []() {ROS_FATAL_STREAM_THROTTLE(1, "Test123"); }, IS_SUBSTRING, STDERR}, + }; +} + +TEST(glog_timed, lpp_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, lpp_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, lpp_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, lpp_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, lpp_syntax_severity_fatal) { + //ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, glog_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, glog_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, glog_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, glog_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, glog_syntax_severity_fatal) { + //ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_debug_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_info_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_warning_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_error_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_fatal) { + //ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(glog_timed, ros_syntax_severity_fatal_stream) { + //ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} \ No newline at end of file From 1c1b1371b26e15733e59bc9d6158eeeca9175a90 Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:51:10 +0200 Subject: [PATCH 02/11] Fixed variable in LOG_TIMED --- include/log++.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/log++.h b/include/log++.h index 157b322..aa99bb9 100644 --- a/include/log++.h +++ b/include/log++.h @@ -392,7 +392,7 @@ if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I #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(), n, LPP_INTL::InternalLog() << x, toBase(LPP_INTL::LppSeverity::severity), LPP_INTL::PolicyType::TIMED) // 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) #endif #if defined MODE_ROSLOG || defined MODE_LPP || MODE_NOLOG 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 03/11] 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) : From 65656860e458321cb61852c86de05b74d47ea5cf Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Fri, 5 Apr 2024 13:39:08 +0200 Subject: [PATCH 04/11] Fix implicit template parameters for C++11/C++14 --- include/log++.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/log++.h b/include/log++.h index 1951671..caeb369 100644 --- a/include/log++.h +++ b/include/log++.h @@ -279,10 +279,10 @@ inline void LOG_INIT([[maybe_unused]] char *argv, [[maybe_unused]] const std::fu #ifndef LOG_EVERY_T #define LPP_INTL_LOG_EVERY_T(severity, t) LPP_WARN("LOG_EVERY_T is only defined in GLOG v0.6 or newer. File name and line numbers will be invalid in the log output.") \ -LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(severity), LPP_INTL::PolicyType::TIMED) +LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(severity), LPP_INTL::PolicyType::TIMED) #define LOG_EVERY_T(severity, t) LPP_WARN("LOG_EVERY_T is only defined in GLOG v0.6 or newer.") \ -LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) +LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) #endif #define LPP_INTL_DLOG_EVERY_T(severity, t) LPP_WARN("File name and line numbers will be invalid in the log output.") LOG_EVERY_T(severity, t) @@ -408,15 +408,15 @@ if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I #endif //defined MODE_ROSLOG || defined MODE_LPP || MODE_NOLOG #if defined MODE_ROSLOG || defined MODE_LPP -#define LOG_EVERY_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::EVERY_N) +#define LOG_EVERY_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::EVERY_N) #define LOG_IF_EVERY_N(severity, condition, n) if (condition) LOG_EVERY_N(severity, n) -#define LOG_FIRST_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::FIRST_N) +#define LOG_FIRST_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::FIRST_N) #define DLOG(severity) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalLog(LPP_INTL::BaseSeverity::DEBUG) -#define DLOG_EVERY_N(severity, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) +#define DLOG_EVERY_N(severity, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) #define DLOG_FIRST_N(severity, n) LPP_WARN("DLOG_FIRST_N is a Log++ extension"); LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); \ -LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::FIRST_N) -#define DLOG_IF_EVERY_N(severity, condition, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); if (condition) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) +LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::FIRST_N) +#define DLOG_IF_EVERY_N(severity, condition, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); if (condition) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) #define LOG_STRING(severity, ptr) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalGlogLogStringLog(toBase(LPP_INTL::GlogSeverity::severity), ptr) #undef VLOG_IS_ON From 169bd7ea719f8af77c3fe64f1e237e88cc64ee6c Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Fri, 5 Apr 2024 13:40:08 +0200 Subject: [PATCH 05/11] Added unittests for timed logging with floating point periods --- test/default/test_default_timed.cc | 13 +++++++++++++ test/glog/test_glog_timed.cc | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/test/default/test_default_timed.cc b/test/default/test_default_timed.cc index 6b583af..7c475c4 100644 --- a/test/default/test_default_timed.cc +++ b/test/default/test_default_timed.cc @@ -34,6 +34,19 @@ std::vector generateTests() { }; } +TEST(default_timed, lpp_syntax_floating_point_time) { + for (int i = 0; i < 5; i++) { + + std::string output = LPP_CAPTURE_STDOUT(LOG_TIMED(I, 0.1, "Test" << 123)); + if (i % 2 == 0) { + ASSERT_TRUE(isSubstring(output, "I")); + ASSERT_TRUE(isSubstring(output, "Test123")); + } + //sleep 0.1s + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } +} + TEST(default_timed, lpp_syntax_severity_debug) { ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); } diff --git a/test/glog/test_glog_timed.cc b/test/glog/test_glog_timed.cc index 01d9be8..6fd1eb2 100644 --- a/test/glog/test_glog_timed.cc +++ b/test/glog/test_glog_timed.cc @@ -34,6 +34,19 @@ std::vector generateTests() { }; } +TEST(glog_timed, lpp_syntax_floating_point_time) { + for (int i = 0; i < 5; i++) { + + std::string output = LPP_CAPTURE_STDOUT(LOG_TIMED(I, 0.1, "Test" << 123)); + if (i % 2 == 0) { + ASSERT_TRUE(isSubstring(output, "I")); + ASSERT_TRUE(isSubstring(output, "Test123")); + } + //sleep 0.1s + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } +} + TEST(glog_timed, lpp_syntax_severity_debug) { ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); } From c286d5c6cedb8c9d7c76d0e075e3ecbd4b39aaca Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:27:32 +0200 Subject: [PATCH 06/11] Fixed unittest --- test/glog/test_glog_timed.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/glog/test_glog_timed.cc b/test/glog/test_glog_timed.cc index 6fd1eb2..0377cfc 100644 --- a/test/glog/test_glog_timed.cc +++ b/test/glog/test_glog_timed.cc @@ -35,9 +35,10 @@ std::vector generateTests() { } TEST(glog_timed, lpp_syntax_floating_point_time) { + LOG_INIT(*test_argv); for (int i = 0; i < 5; i++) { - std::string output = LPP_CAPTURE_STDOUT(LOG_TIMED(I, 0.1, "Test" << 123)); + std::string output = LPP_CAPTURE_STDERR(LOG_TIMED(I, 0.1, "Test" << 123)); if (i % 2 == 0) { ASSERT_TRUE(isSubstring(output, "I")); ASSERT_TRUE(isSubstring(output, "Test123")); From 6c2c16f9613dbe41b3ddc013d3bf343c84dd80cb Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:54:41 +0200 Subject: [PATCH 07/11] Fixed timed logging for MODE_LPP --- CMakeLists.txt | 1 + include/log++.h | 13 ++++ test/lpp/test_lpp_timed.cc | 127 +++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 test/lpp/test_lpp_timed.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 70f78d0..a29b87c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,7 @@ if (GLOG_FOUND AND catkin_FOUND AND LPP_BUILD_TESTS) test/lpp/test_lpp_if_every_n.cc test/lpp/test_lpp_log_string.cc test/lpp/test_lpp_rosprintf.cc + test/lpp/test_lpp_timed.cc test/lpp/test_lpp_vlog.cc) target_include_directories(${LPP_TESTS} PRIVATE ${LPP_INCLUDE_DIRECTORIES} test/lpp) diff --git a/include/log++.h b/include/log++.h index caeb369..8e1c159 100644 --- a/include/log++.h +++ b/include/log++.h @@ -457,8 +457,21 @@ LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG #define ROS_ERROR_ONCE(...) LOG_FIRST(E, 1, LPP_INTL::formatToString(__VA_ARGS__)) #define ROS_FATAL_ONCE(...) LOG_FIRST(F, 1, LPP_INTL::formatToString(__VA_ARGS__)) +#define ROS_DEBUG_THROTTLE(period, ...) LOG_TIMED(D, period, LPP_INTL::formatToString(__VA_ARGS__)) +#define ROS_DEBUG_STREAM_THROTTLE(period, x) LOG_TIMED(D, period, x) +#define ROS_INFO_THROTTLE(period, ...) LOG_TIMED(I, period, LPP_INTL::formatToString(__VA_ARGS__)) +#define ROS_INFO_STREAM_THROTTLE(period, x) LOG_TIMED(I, period, x) +#define ROS_WARN_THROTTLE(period, ...) LOG_TIMED(W, period, LPP_INTL::formatToString(__VA_ARGS__)) +#define ROS_WARN_STREAM_THROTTLE(period, x) LOG_TIMED(W, period, x) +#define ROS_ERROR_THROTTLE(period, ...) LOG_TIMED(E, period, LPP_INTL::formatToString(__VA_ARGS__)) +#define ROS_ERROR_STREAM_THROTTLE(period, x) LOG_TIMED(E, period, x) +#define ROS_FATAL_THROTTLE(period, ...) LOG_TIMED(F, period, LPP_INTL::formatToString(__VA_ARGS__)) +#define ROS_FATAL_STREAM_THROTTLE(period, x) LOG_TIMED(F, period, x) + #define LOG_IF(severity, cond) if (cond) LPP_INTL::InternalLog(GlogSeverity::severity) #define LOG_1(severity) LPP_INTL::InternalLog(LPP_INTL::GlogSeverity::severity) +#define DLOG_EVERY_T(severity, t) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(LPP_INTL::GlogSeverity::DEBUG), LPP_INTL::PolicyType::TIMED) +#define LOG_EVERY_T(severity, t) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) #endif #if defined MODE_LPP || defined MODE_DEFAULT diff --git a/test/lpp/test_lpp_timed.cc b/test/lpp/test_lpp_timed.cc new file mode 100644 index 0000000..7a36838 --- /dev/null +++ b/test/lpp/test_lpp_timed.cc @@ -0,0 +1,127 @@ +// +// Created by 4c3y (acey) on 16.09.22. +// + +#include +#include +#include +#include + +std::vector generateTests() { + return { + {"lpp_timed_lpp_syntax_severity_debug_Test","DEBUG Test123\n",[]() { LOG_TIMED(D, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_lpp_syntax_severity_info_Test","INFO Test123\n",[]() { LOG_TIMED(I, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_lpp_syntax_severity_warning_Test","WARN Test123\n",[]() { LOG_TIMED(W, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_lpp_syntax_severity_error_Test","ERROR Test123\n",[]() { LOG_TIMED(E, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_lpp_syntax_severity_fatal_Test","FATAL Test123\n",[]() { LOG_TIMED(F, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, + + {"lpp_timed_glog_syntax_severity_debug_Test","DEBUG Test123\n",[]() { DLOG_EVERY_T(INFO, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_glog_syntax_severity_info_Test","INFO Test123\n",[]() { LOG_EVERY_T(INFO, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_glog_syntax_severity_warning_Test","WARN Test123\n",[]() { LOG_EVERY_T(WARNING, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_glog_syntax_severity_error_Test","ERROR Test123\n",[]() { LOG_EVERY_T(ERROR, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_glog_syntax_severity_fatal_Test","FATAL Test123\n",[]() { LOG_EVERY_T(FATAL, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, + + {"lpp_timed_ros_syntax_severity_debug_Test", "DEBUG Test123\n", []() {ROS_DEBUG_THROTTLE(1, "Test123"); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_debug_stream_Test", "DEBUG Test123\n", []() {ROS_DEBUG_STREAM_THROTTLE(1, "Test" << 123); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_info_Test", "INFO Test123\n", []() {ROS_INFO_THROTTLE(1, "Test123"); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_info_stream_Test", "INFO Test123\n", []() {ROS_INFO_STREAM_THROTTLE(1, "Test" << 123); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_warning_Test", "WARN Test123\n", []() {ROS_WARN_THROTTLE(1, "Test123"); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_warning_stream_Test", "WARN Test123\n", []() {ROS_WARN_STREAM_THROTTLE(1, "Test" << 123); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_error_Test", "ERROR Test123\n", []() {ROS_ERROR_THROTTLE(1, "Test123"); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_error_stream_Test", "ERROR Test123\n", []() {ROS_ERROR_STREAM_THROTTLE(1, "Test" << 123); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_fatal_Test", "FATAL Test123\n", []() {ROS_FATAL_THROTTLE(1, "Test123"); }, EQUAL, STDOUT}, + {"lpp_timed_ros_syntax_severity_fatal_stream_Test", "FATAL Test123\n", []() {ROS_FATAL_STREAM_THROTTLE(1, "Test123"); }, EQUAL, STDOUT}, + }; +} + +TEST(lpp_timed, lpp_syntax_floating_point_time) { + LOG_INIT(*test_argv); + for (int i = 0; i < 5; i++) { + std::string output = LPP_CAPTURE_STDOUT(LOG_TIMED(I, 0.1, "Test" << 123)); + if (i % 2 == 0) { + ASSERT_EQ(output, "INFO Test123\n"); + } + //sleep 0.1s + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } +} + +TEST(lpp_timed, lpp_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, lpp_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, lpp_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, lpp_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, lpp_syntax_severity_fatal) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, glog_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, glog_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, glog_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, glog_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, glog_syntax_severity_fatal) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_debug_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_info_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_warning_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_error_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_fatal) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(lpp_timed, ros_syntax_severity_fatal_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} \ No newline at end of file From 51578224b0fdfbe18a1843a05debcaa6b7bd2f2b Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Fri, 5 Apr 2024 16:15:21 +0200 Subject: [PATCH 08/11] Fixed timed logging for MODE_ROSLOG --- CMakeLists.txt | 1 + include/log++.h | 2 + test/common/async_tests.h | 8 +- test/lpp/test_lpp_timed.cc | 22 +++--- test/roslog/test_roslog_timed.cc | 129 +++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 12 deletions(-) create mode 100644 test/roslog/test_roslog_timed.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index a29b87c..1cfb40a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,6 +140,7 @@ if (GLOG_FOUND AND catkin_FOUND AND LPP_BUILD_TESTS) test/roslog/test_roslog_if_every_n.cc test/roslog/test_roslog_log_string.cc test/roslog/test_roslog_rosprintf.cc + test/roslog/test_roslog_timed.cc test/roslog/test_roslog_vlog.cc) target_include_directories(${ROSLOG_TESTS} PRIVATE ${LPP_INCLUDE_DIRECTORIES} test/roslog) diff --git a/include/log++.h b/include/log++.h index 8e1c159..184d954 100644 --- a/include/log++.h +++ b/include/log++.h @@ -409,11 +409,13 @@ if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I #if defined MODE_ROSLOG || defined MODE_LPP #define LOG_EVERY_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::EVERY_N) +#define LOG_EVERY_T(severity, t) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) #define LOG_IF_EVERY_N(severity, condition, n) if (condition) LOG_EVERY_N(severity, n) #define LOG_FIRST_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::FIRST_N) #define DLOG(severity) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalLog(LPP_INTL::BaseSeverity::DEBUG) #define DLOG_EVERY_N(severity, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) +#define DLOG_EVERY_T(severity, t) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::TIMED) #define DLOG_FIRST_N(severity, n) LPP_WARN("DLOG_FIRST_N is a Log++ extension"); LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); \ LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::FIRST_N) #define DLOG_IF_EVERY_N(severity, condition, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); if (condition) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) diff --git a/test/common/async_tests.h b/test/common/async_tests.h index af47ade..d439c85 100644 --- a/test/common/async_tests.h +++ b/test/common/async_tests.h @@ -16,7 +16,8 @@ enum CompareType { EQUAL, - IS_SUBSTRING + IS_SUBSTRING, + REMOVE_NUMBERS_FROM_STRING }; enum StreamType { @@ -128,6 +129,7 @@ class TestResult { switch (compare_type) { case EQUAL:return compareEquality(output, expected_output); case IS_SUBSTRING: return compareSubstring(output, expected_output); + case REMOVE_NUMBERS_FROM_STRING: return compareRemoveNumbersFromString(output, expected_output); default:return false; } } @@ -143,6 +145,10 @@ class TestResult { return output == expected_output; }; + static inline bool compareRemoveNumbersFromString(const std::string &output, const std::string &expected_output) { + return expected_output == removeNumbersFromString(output); + } + inline void insert(const std::string &test_name, bool test_status) { test_results.insert({test_name, test_status}); } diff --git a/test/lpp/test_lpp_timed.cc b/test/lpp/test_lpp_timed.cc index 7a36838..1198ab0 100644 --- a/test/lpp/test_lpp_timed.cc +++ b/test/lpp/test_lpp_timed.cc @@ -9,17 +9,17 @@ std::vector generateTests() { return { - {"lpp_timed_lpp_syntax_severity_debug_Test","DEBUG Test123\n",[]() { LOG_TIMED(D, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, - {"lpp_timed_lpp_syntax_severity_info_Test","INFO Test123\n",[]() { LOG_TIMED(I, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, - {"lpp_timed_lpp_syntax_severity_warning_Test","WARN Test123\n",[]() { LOG_TIMED(W, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, - {"lpp_timed_lpp_syntax_severity_error_Test","ERROR Test123\n",[]() { LOG_TIMED(E, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, - {"lpp_timed_lpp_syntax_severity_fatal_Test","FATAL Test123\n",[]() { LOG_TIMED(F, 1, "Test" << 123); }, IS_SUBSTRING, STDOUT}, - - {"lpp_timed_glog_syntax_severity_debug_Test","DEBUG Test123\n",[]() { DLOG_EVERY_T(INFO, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, - {"lpp_timed_glog_syntax_severity_info_Test","INFO Test123\n",[]() { LOG_EVERY_T(INFO, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, - {"lpp_timed_glog_syntax_severity_warning_Test","WARN Test123\n",[]() { LOG_EVERY_T(WARNING, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, - {"lpp_timed_glog_syntax_severity_error_Test","ERROR Test123\n",[]() { LOG_EVERY_T(ERROR, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, - {"lpp_timed_glog_syntax_severity_fatal_Test","FATAL Test123\n",[]() { LOG_EVERY_T(FATAL, 1) << "Test" << 123; }, IS_SUBSTRING, STDOUT}, + {"lpp_timed_lpp_syntax_severity_debug_Test","DEBUG Test123\n",[]() { LOG_TIMED(D, 1, "Test" << 123); }, EQUAL, STDOUT}, + {"lpp_timed_lpp_syntax_severity_info_Test","INFO Test123\n",[]() { LOG_TIMED(I, 1, "Test" << 123); }, EQUAL, STDOUT}, + {"lpp_timed_lpp_syntax_severity_warning_Test","WARN Test123\n",[]() { LOG_TIMED(W, 1, "Test" << 123); }, EQUAL, STDOUT}, + {"lpp_timed_lpp_syntax_severity_error_Test","ERROR Test123\n",[]() { LOG_TIMED(E, 1, "Test" << 123); }, EQUAL, STDOUT}, + {"lpp_timed_lpp_syntax_severity_fatal_Test","FATAL Test123\n",[]() { LOG_TIMED(F, 1, "Test" << 123); }, EQUAL, STDOUT}, + + {"lpp_timed_glog_syntax_severity_debug_Test","DEBUG Test123\n",[]() { DLOG_EVERY_T(INFO, 1) << "Test" << 123; }, EQUAL, STDOUT}, + {"lpp_timed_glog_syntax_severity_info_Test","INFO Test123\n",[]() { LOG_EVERY_T(INFO, 1) << "Test" << 123; }, EQUAL, STDOUT}, + {"lpp_timed_glog_syntax_severity_warning_Test","WARN Test123\n",[]() { LOG_EVERY_T(WARNING, 1) << "Test" << 123; }, EQUAL, STDOUT}, + {"lpp_timed_glog_syntax_severity_error_Test","ERROR Test123\n",[]() { LOG_EVERY_T(ERROR, 1) << "Test" << 123; }, EQUAL, STDOUT}, + {"lpp_timed_glog_syntax_severity_fatal_Test","FATAL Test123\n",[]() { LOG_EVERY_T(FATAL, 1) << "Test" << 123; }, EQUAL, STDOUT}, {"lpp_timed_ros_syntax_severity_debug_Test", "DEBUG Test123\n", []() {ROS_DEBUG_THROTTLE(1, "Test123"); }, EQUAL, STDOUT}, {"lpp_timed_ros_syntax_severity_debug_stream_Test", "DEBUG Test123\n", []() {ROS_DEBUG_STREAM_THROTTLE(1, "Test" << 123); }, EQUAL, STDOUT}, diff --git a/test/roslog/test_roslog_timed.cc b/test/roslog/test_roslog_timed.cc new file mode 100644 index 0000000..fb1022f --- /dev/null +++ b/test/roslog/test_roslog_timed.cc @@ -0,0 +1,129 @@ +// +// Created by 4c3y (acey) on 16.09.22. +// + +#include +#include +#include +#include + +using namespace lpp::rostest; + +std::vector generateTests() { + return { + {"roslog_timed_lpp_syntax_severity_debug_Test", debug,[]() { LOG_TIMED(D, 1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDOUT}, + {"roslog_timed_lpp_syntax_severity_info_Test", info,[]() { LOG_TIMED(I, 1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDOUT}, + {"roslog_timed_lpp_syntax_severity_warning_Test", warning,[]() { LOG_TIMED(W, 1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_lpp_syntax_severity_error_Test",error,[]() { LOG_TIMED(E, 1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_lpp_syntax_severity_fatal_Test",fatal,[]() { LOG_TIMED(F, 1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + + {"roslog_timed_glog_syntax_severity_debug_Test",debug,[]() { DLOG_EVERY_T(INFO, 1) << "Test" << 123; }, REMOVE_NUMBERS_FROM_STRING, STDOUT}, + {"roslog_timed_glog_syntax_severity_info_Test", info,[]() { LOG_EVERY_T(INFO, 1) << "Test" << 123; }, REMOVE_NUMBERS_FROM_STRING, STDOUT}, + {"roslog_timed_glog_syntax_severity_warning_Test", warning,[]() { LOG_EVERY_T(WARNING, 1) << "Test" << 123; }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_glog_syntax_severity_error_Test",error,[]() { LOG_EVERY_T(ERROR, 1) << "Test" << 123; }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_glog_syntax_severity_fatal_Test",fatal,[]() { LOG_EVERY_T(FATAL, 1) << "Test" << 123; }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + + {"roslog_timed_ros_syntax_severity_debug_Test", debug, []() {ROS_DEBUG_THROTTLE(1, "Test123"); }, REMOVE_NUMBERS_FROM_STRING, STDOUT}, + {"roslog_timed_ros_syntax_severity_debug_stream_Test", debug, []() {ROS_DEBUG_STREAM_THROTTLE(1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDOUT}, + {"roslog_timed_ros_syntax_severity_info_Test", info, []() {ROS_INFO_THROTTLE(1, "Test123"); }, REMOVE_NUMBERS_FROM_STRING, STDOUT}, + {"roslog_timed_ros_syntax_severity_info_stream_Test", info, []() {ROS_INFO_STREAM_THROTTLE(1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDOUT}, + {"roslog_timed_ros_syntax_severity_warning_Test", warning, []() {ROS_WARN_THROTTLE(1, "Test123"); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_ros_syntax_severity_warning_stream_Test", warning, []() {ROS_WARN_STREAM_THROTTLE(1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_ros_syntax_severity_error_Test", error, []() {ROS_ERROR_THROTTLE(1, "Test123"); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_ros_syntax_severity_error_stream_Test", error, []() {ROS_ERROR_STREAM_THROTTLE(1, "Test" << 123); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_ros_syntax_severity_fatal_Test", fatal, []() {ROS_FATAL_THROTTLE(1, "Test123"); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + {"roslog_timed_ros_syntax_severity_fatal_stream_Test", fatal, []() {ROS_FATAL_STREAM_THROTTLE(1, "Test123"); }, REMOVE_NUMBERS_FROM_STRING, STDERR}, + }; +} + +TEST(roslog_timed, lpp_syntax_floating_point_time) { + LOG_INIT(*test_argv); + for (int i = 0; i < 5; i++) { + std::string output = LPP_CAPTURE_STDOUT(LOG_TIMED(I, 0.1, "Test" << 123)); + if (i % 2 == 0) { + ASSERT_EQ(removeNumbersFromString(output), info); + } + //sleep 0.1s + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } +} + +TEST(roslog_timed, lpp_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, lpp_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, lpp_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, lpp_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, lpp_syntax_severity_fatal) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, glog_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, glog_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, glog_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, glog_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, glog_syntax_severity_fatal) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_debug) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_debug_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_info) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_info_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_warning) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_warning_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_error) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_error_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_fatal) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} + +TEST(roslog_timed, ros_syntax_severity_fatal_stream) { + ASSERT_TRUE(TestResult::getInstance().get(GET_CLASS_NAME(*this, nullptr))); +} \ No newline at end of file From 910902929b502ed63abb38de1b85b9f8602beb77 Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Fri, 5 Apr 2024 16:23:15 +0200 Subject: [PATCH 09/11] Fixed multiple definition of LOG_EVERY_T --- include/log++.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/log++.h b/include/log++.h index 184d954..ac3cb93 100644 --- a/include/log++.h +++ b/include/log++.h @@ -472,8 +472,6 @@ LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG #define LOG_IF(severity, cond) if (cond) LPP_INTL::InternalLog(GlogSeverity::severity) #define LOG_1(severity) LPP_INTL::InternalLog(LPP_INTL::GlogSeverity::severity) -#define DLOG_EVERY_T(severity, t) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(LPP_INTL::GlogSeverity::DEBUG), LPP_INTL::PolicyType::TIMED) -#define LOG_EVERY_T(severity, t) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) #endif #if defined MODE_LPP || defined MODE_DEFAULT From d45463b03ca7bbc43865467bb8dff94b5dde1f0f Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Fri, 5 Apr 2024 19:07:16 +0200 Subject: [PATCH 10/11] added warnings for glogs timed logging macros --- include/log++.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/log++.h b/include/log++.h index ac3cb93..ae2b141 100644 --- a/include/log++.h +++ b/include/log++.h @@ -409,13 +409,13 @@ if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I #if defined MODE_ROSLOG || defined MODE_LPP #define LOG_EVERY_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::EVERY_N) -#define LOG_EVERY_T(severity, t) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) +#define LOG_EVERY_T(severity, t) LPP_WARN("LOG_EVERY_T is only defined in GLOG v0.6 or newer.") LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) #define LOG_IF_EVERY_N(severity, condition, n) if (condition) LOG_EVERY_N(severity, n) #define LOG_FIRST_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::FIRST_N) #define DLOG(severity) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalLog(LPP_INTL::BaseSeverity::DEBUG) #define DLOG_EVERY_N(severity, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) -#define DLOG_EVERY_T(severity, t) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::TIMED) +#define DLOG_EVERY_T(severity, t) LPP_WARN("DLOG_FIRST_N is a Log++ extension"); LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::TIMED) #define DLOG_FIRST_N(severity, n) LPP_WARN("DLOG_FIRST_N is a Log++ extension"); LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); \ LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::FIRST_N) #define DLOG_IF_EVERY_N(severity, condition, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); if (condition) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) From 9342236e032764f293c3b5195b1dc7577ae38824 Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Wed, 17 Apr 2024 18:01:46 +0200 Subject: [PATCH 11/11] Use unsigned int for logging parameters --- include/log++.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/include/log++.h b/include/log++.h index ae2b141..6f9a460 100644 --- a/include/log++.h +++ b/include/log++.h @@ -390,8 +390,8 @@ 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)) // 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_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 @@ -408,17 +408,17 @@ if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I #endif //defined MODE_ROSLOG || defined MODE_LPP || MODE_NOLOG #if defined MODE_ROSLOG || defined MODE_LPP -#define LOG_EVERY_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::EVERY_N) +#define LOG_EVERY_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::EVERY_N) #define LOG_EVERY_T(severity, t) LPP_WARN("LOG_EVERY_T is only defined in GLOG v0.6 or newer.") LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::TIMED) #define LOG_IF_EVERY_N(severity, condition, n) if (condition) LOG_EVERY_N(severity, n) -#define LOG_FIRST_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::FIRST_N) +#define LOG_FIRST_N(severity, n) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::toBase(LPP_INTL::GlogSeverity::severity), LPP_INTL::PolicyType::FIRST_N) #define DLOG(severity) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalLog(LPP_INTL::BaseSeverity::DEBUG) -#define DLOG_EVERY_N(severity, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) +#define DLOG_EVERY_N(severity, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) #define DLOG_EVERY_T(severity, t) LPP_WARN("DLOG_FIRST_N is a Log++ extension"); LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), t, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::TIMED) #define DLOG_FIRST_N(severity, n) LPP_WARN("DLOG_FIRST_N is a Log++ extension"); LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); \ -LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::FIRST_N) -#define DLOG_IF_EVERY_N(severity, condition, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); if (condition) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) +LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::FIRST_N) +#define DLOG_IF_EVERY_N(severity, condition, n) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); if (condition) LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP_INTL::PolicyType::EVERY_N) #define LOG_STRING(severity, ptr) LPP_ASSERT_GLOG(LPP_INTL::GlogSeverity::severity); LPP_INTL::InternalGlogLogStringLog(toBase(LPP_INTL::GlogSeverity::severity), ptr) #undef VLOG_IS_ON @@ -689,16 +689,16 @@ class LogPolicy : public LogPolicyBase { T max_{0}; }; -class CountableLogPolicy : public LogPolicy { +class CountableLogPolicy : public LogPolicy { public: - explicit CountableLogPolicy(int max) : LogPolicy(max) {} + explicit CountableLogPolicy(unsigned int max) : LogPolicy(max) {} protected: - int counter_{0}; + unsigned int counter_{0}; }; class OccasionPolicy : public CountableLogPolicy { public: - explicit OccasionPolicy(int max) : CountableLogPolicy(max) { + explicit OccasionPolicy(unsigned int max) : CountableLogPolicy(max) { counter_ = max_; } @@ -723,7 +723,7 @@ class OccasionPolicy : public CountableLogPolicy { class FirstNOccurrencesPolicy : public CountableLogPolicy { public: - explicit FirstNOccurrencesPolicy(int max) : CountableLogPolicy(max) {} + explicit FirstNOccurrencesPolicy(unsigned int max) : CountableLogPolicy(max) {} inline void update() override { if (!is_n_occurences_reached) { counter_++; @@ -812,7 +812,7 @@ class InternalLogCount { const InternalLog &internal_log, 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"); + 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"); @@ -937,9 +937,9 @@ class InternalPolicyLog : public InternalLog { PolicyType policy_type_{}; }; -class LppGlogExtensionLog : public InternalPolicyLog { +class LppGlogExtensionLog : public InternalPolicyLog { public: - LppGlogExtensionLog(std::string key, int n, GlogSeverity glog_severity, PolicyType policy_type, + LppGlogExtensionLog(std::string key, unsigned int n, GlogSeverity glog_severity, PolicyType policy_type, std::function fn) : InternalPolicyLog(std::move(key), n, toBase(glog_severity), policy_type), fn_(std::move(fn)) { should_print_ = false;