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