Skip to content

Commit

Permalink
MODE_NOLOG: Added support for first n logging + Added compile time pr…
Browse files Browse the repository at this point in the history
…eprocessor macro argument validation
  • Loading branch information
4c3y committed Sep 7, 2023
1 parent 4b23d9b commit e7c8dfa
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ if (GLOG_FOUND AND catkin_FOUND AND LPP_BUILD_TESTS)
add_executable(${NOLOG_TESTS} test/test_entry_point.cpp
test/nolog/test_nolog_basic.cc
test/nolog/test_nolog_every_n.cc
#test/nolog/test_nolog_first_n.cc
test/nolog/test_nolog_first_n.cc
#test/nolog/test_nolog_if_every_n.cc
#test/nolog/test_nolog_log_string.cc
#test/nolog/test_nolog_rosprintf.cc
Expand Down
18 changes: 15 additions & 3 deletions include/log++.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,20 +432,32 @@ LPP_INTL::InternalPolicyLog(LPP_GET_KEY(), n, LPP_INTL::BaseSeverity::DEBUG, LPP
//! MODE_NOLOG

#ifdef MODE_NOLOG
#define LOG_EVERY(severity, n, x) (void) LPP_INTL::LppSeverity::severity; InternalLog()
//lpp
#define LOG_2(severity, x) (void) LPP_INTL::LppSeverity::severity; InternalLog() << x
#define LOG_EVERY(severity, n, x) (void) LPP_INTL::LppSeverity::severity; static_assert(std::is_integral_v<decltype(n)>); InternalLog()
#define LOG_FIRST(severity, n, x) (void) LPP_INTL::LppSeverity::severity; static_assert(std::is_integral_v<decltype(n)>); InternalLog()

//glog
#define LOG_1(severity) (void) LPP_INTL::GlogSeverity::severity; InternalLog()
#define DLOG(severity) (void) LPP_INTL::GlogSeverity::severity; InternalLog()
#define DLOG_EVERY_N(severity, n) (void) LPP_INTL::GlogSeverity::severity; InternalLog()
#define LOG_EVERY_N(severity, n) (void) LPP_INTL::GlogSeverity::severity; InternalLog()
#define LOG_1(severity) (void) LPP_INTL::GlogSeverity::severity; InternalLog()
#define DLOG_FIRST_N(severity, n) (void) LPP_INTL::GlogSeverity::severity; static_assert(std::is_integral_v<decltype(n)>); InternalLog()
#define LOG_FIRST_N(severity, n) (void) LPP_INTL::GlogSeverity::severity; static_assert(std::is_integral_v<decltype(n)>); InternalLog()

#define LOG_2(severity, x) (void) LPP_INTL::LppSeverity::severity; InternalLog() << x

//ros
#define ROS_DEBUG_STREAM(x) (void) x
#define ROS_INFO_STREAM(x) (void) x
#define ROS_WARN_STREAM(x) (void) x
#define ROS_ERROR_STREAM(x) (void) x
#define ROS_FATAL_STREAM(x) (void) x

#define ROS_DEBUG_ONCE(x) (void) x
#define ROS_INFO_ONCE(x) (void) x
#define ROS_WARN_ONCE(x) (void) x
#define ROS_ERROR_ONCE(x) (void) x
#define ROS_FATAL_ONCE(x) (void) x
#endif

namespace lpp {
Expand Down
147 changes: 147 additions & 0 deletions test/nolog/test_nolog_first_n.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//
// Created by acey on 07.09.23.
//

#include <gtest/gtest.h>
#include <log++.h>
#include <test_utils.h>

TEST(nolog_LogFirstN, lpp_syntax_severity_debug) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDOUT(LOG_FIRST(D, 3, "Test" << 123));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, lpp_syntax_severity_info) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDOUT(LOG_FIRST(I, 3, "Test" << 123));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, lpp_syntax_severity_warning) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDERR(LOG_FIRST(W, 3, "Test" << 123));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, lpp_syntax_severity_error) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDERR(LOG_FIRST(E, 3, "Test" << 123));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, lpp_syntax_severity_fatal) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDERR(LOG_FIRST(F, 3, "Test" << 123));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, glog_syntax_severity_debug) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output =
LPP_CAPTURE_STDOUT(DLOG_FIRST_N(INFO, 3) << "Test" << 123);
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, glog_syntax_severity_info) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output =
LPP_CAPTURE_STDOUT(LOG_FIRST_N(INFO, 3) << "Test" << 123);
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, glog_syntax_severity_warning) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output =
LPP_CAPTURE_STDERR(LOG_FIRST_N(WARNING, 3) << "Test" << 123);
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, glog_syntax_severity_error) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output =
LPP_CAPTURE_STDERR(LOG_FIRST_N(ERROR, 3) << "Test" << 123);
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, glog_syntax_severity_fatal) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output =
LPP_CAPTURE_STDERR(LOG_FIRST_N(FATAL, 3) << "Test" << 123);
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, ros_debug_once) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDERR(ROS_DEBUG_ONCE("Test123"));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, ros_info_once) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDOUT(ROS_INFO_ONCE("Test123"));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, ros_warn_once) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDERR(ROS_WARN_ONCE("Test123"));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, ros_error_once) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDERR(ROS_ERROR_ONCE("Test123"));
ASSERT_EQ(output, "");
}
}

TEST(nolog_LogFirstN, ros_fatal_once) {
LOG_INIT(*test_argv);

for (int i = 0; i < 5; i++) {
std::string output = LPP_CAPTURE_STDERR(ROS_FATAL_ONCE("Test123"));
ASSERT_EQ(output, "");
}
}

0 comments on commit e7c8dfa

Please sign in to comment.