From 985a4c11cf71baf4113384b96fdf2a999f84545a Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Wed, 20 Dec 2023 18:41:06 +0100 Subject: [PATCH 1/2] Improved performance of LOG_EVERY, LOG_FIRST and LOG() if MODE_GLOG is used --- include/log++.h | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/include/log++.h b/include/log++.h index f1f6e88..dd642a0 100644 --- a/include/log++.h +++ b/include/log++.h @@ -296,29 +296,29 @@ LPP_INTL::LppGlogExtensionLog(LPP_GET_KEY(), n, LPP_INTL::GlogSeverity::severity #pragma clang diagnostic push #pragma ide diagnostic ignored "bugprone-macro-parentheses" -#define LOG_2(severity, x) LPP_ASSERT_LPP(LPP_INTL::LppSeverity::severity); \ -if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I || LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::D) {LOG_1(INFO) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::W) {LOG_1(WARNING) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::E) {LOG_1(ERROR) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) {LOG_1(FATAL) << x;} \ -true +#define LOG_2(severity, x) LPP_ASSERT_LPP(LPP_INTL::LppSeverity::severity); do {\ +if constexpr (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I || LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::D) {LOG_1(INFO) << x;} \ +else if constexpr (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::W) {LOG_1(WARNING) << x;} \ +else if constexpr (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::E) {LOG_1(ERROR) << x;} \ +else if constexpr (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) {LOG_1(FATAL) << x;} \ +} while(0) //Add true at the end to make semicolons mandatory. Compiles to nothing. #define LOG_3(severity, cond, x) if (cond) { LOG_2(severity, x);} true -#define LOG_EVERY(severity, n, x) LPP_ASSERT_LPP(LPP_INTL::LppSeverity::severity); \ -if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I) {LOG_EVERY_N(INFO, n) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::W) {LOG_EVERY_N(WARNING, n) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::E) {LOG_EVERY_N(ERROR, n) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) {LOG_EVERY_N(FATAL, n) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::D) {DLOG_EVERY_N(INFO, n) << x;} \ -true - -#define LOG_FIRST(severity, n, x) LPP_ASSERT_LPP(LPP_INTL::LppSeverity::severity); \ -if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I || LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::D) {LOG_FIRST_N(INFO, n) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::W) {LOG_FIRST_N(WARNING, n) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::E) {LOG_FIRST_N(ERROR, n) << x;} \ -else if (LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) {LOG_FIRST_N(FATAL, n) << x;} \ -true +#define LOG_EVERY(severity, n, x) LPP_ASSERT_LPP(LPP_INTL::LppSeverity::severity); do {\ +if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I) {LOG_EVERY_N(INFO, n) << x;} \ +else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::W) {LOG_EVERY_N(WARNING, n) << x;} \ +else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::E) {LOG_EVERY_N(ERROR, n) << x;} \ +else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) {LOG_EVERY_N(FATAL, n) << x;} \ +else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::D) {DLOG_EVERY_N(INFO, n) << x;} \ +} while(0) + +#define LOG_FIRST(severity, n, x) LPP_ASSERT_LPP(LPP_INTL::LppSeverity::severity); do {\ +if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::I || LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::D) {LOG_FIRST_N(INFO, n) << x;} \ +else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::W) {LOG_FIRST_N(WARNING, n) << x;} \ +else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::E) {LOG_FIRST_N(ERROR, n) << x;} \ +else if constexpr(LPP_INTL::LppSeverity::severity == LPP_INTL::LppSeverity::F) {LOG_FIRST_N(FATAL, n) << x;} \ +} while(0) #ifndef MODE_DEFAULT #define ROS_DEBUG(...) DLOG(INFO) << LPP_INTL::formatToString(__VA_ARGS__) From eed1c922120a981df425487181c95e7b70247e94 Mon Sep 17 00:00:00 2001 From: 4c3y <69460051+4c3y@users.noreply.github.com> Date: Wed, 20 Dec 2023 18:52:01 +0100 Subject: [PATCH 2/2] Fixed all warnings and enabled -Wall -Wextra -Wpedantic -Werror --- CMakeLists.txt | 1 + include/log++.h | 11 ++++++----- test/common/test_utils.h | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b09bffa..b409dc0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ endif () # Set standard of top level project or C++17 if (NOT DEFINED ${CMAKE_CXX_STANDARD}) set(CMAKE_CXX_STANDARD 17) + add_definitions(-Wall -Wextra -Wpedantic -Werror -Wno-unknown-pragmas) else () set(CMAKE_CXX_STANDARD ${CMAKE_CXX_STANDARD}) endif () diff --git a/include/log++.h b/include/log++.h index dd642a0..db0826c 100644 --- a/include/log++.h +++ b/include/log++.h @@ -216,9 +216,10 @@ using namespace lpp::internal; * Used to initialize Log++ * * If called more than once, all further calls will be ignored. - * @param argv is used for GLOG if present, otherwise unused. + * @param argv is used if MODE_GLOG is defined, otherwise unused. + * @param callback is used if MODE_LPP is defined, otherwise unused. */ -inline void LOG_INIT([[maybe_unused]] char *argv, const std::function& callback = nullptr) { +inline void LOG_INIT([[maybe_unused]] char *argv, [[maybe_unused]] const std::function& callback = nullptr) { // If LOG_INIT is called more than once, do nothing. if (!lppInit.glog_initialized || !lppInit.lpp_initialized) { @@ -819,7 +820,7 @@ class InternalLogCount { class InternalGlogLogStringLog : public InternalLog { public: InternalGlogLogStringLog(BaseSeverity base_severity, std::vector* vecptr): - vecptr_(vecptr), InternalLog(base_severity) { + InternalLog(base_severity), vecptr_(vecptr) { if (vecptr != nullptr) { should_print_ = false; } @@ -839,8 +840,8 @@ class InternalGlogLogStringLog : public InternalLog { class InternalPolicyLog : public InternalLog { public: InternalPolicyLog(std::string key, int n, BaseSeverity base_severity, PolicyType policy_type) : - key_(std::move(key)), n_(n), policy_type_(policy_type), - InternalLog(base_severity) { + InternalLog(base_severity), key_(std::move(key)), n_(n), + policy_type_(policy_type) { should_print_ = false; }; diff --git a/test/common/test_utils.h b/test/common/test_utils.h index ab56fcd..3e256be 100644 --- a/test/common/test_utils.h +++ b/test/common/test_utils.h @@ -24,7 +24,7 @@ inline static bool isSubstring(const std::string &string, const std::string &sub inline static std::string removeNumbersFromString(std::string str) { int current = 0; - for (int i = 0; i < str.length(); i++) { + for (std::size_t i = 0; i < str.length(); i++) { if (!isdigit(str[i])) { str[current] = str[i]; current++;