00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "ros/console.h"
00031
00032 #include "log4cxx/appenderskeleton.h"
00033 #include "log4cxx/spi/loggingevent.h"
00034
00035 #include <vector>
00036 #include <stdexcept>
00037
00038 #include <gtest/gtest.h>
00039
00040 #include <boost/shared_array.hpp>
00041
00042 class TestAppender : public log4cxx::AppenderSkeleton
00043 {
00044 public:
00045 struct Info
00046 {
00047 log4cxx::LevelPtr level_;
00048 std::string message_;
00049 std::string logger_name_;
00050 };
00051
00052 typedef std::vector<Info> V_Info;
00053
00054 V_Info info_;
00055
00056 protected:
00057 virtual void append(const log4cxx::spi::LoggingEventPtr& event, log4cxx::helpers::Pool&)
00058 {
00059 Info info;
00060 info.level_ = event->getLevel();
00061 info.message_ = event->getMessage();
00062 info.logger_name_ = event->getLoggerName();
00063
00064 info_.push_back( info );
00065 }
00066
00067 virtual void close()
00068 {
00069 }
00070 virtual bool requiresLayout() const
00071 {
00072 return false;
00073 }
00074 };
00075
00076 class TestAppenderWithThrow : public log4cxx::AppenderSkeleton
00077 {
00078 protected:
00079 virtual void append(const log4cxx::spi::LoggingEventPtr&, log4cxx::helpers::Pool&)
00080 {
00081 throw std::runtime_error("This should be caught");
00082 }
00083
00084 virtual void close()
00085 {
00086 }
00087 virtual bool requiresLayout() const
00088 {
00089 return false;
00090 }
00091 };
00092
00093 struct BasicFilter : public ros::console::FilterBase
00094 {
00095 BasicFilter(bool enabled)
00096 : enabled_(enabled)
00097 {}
00098
00099 inline virtual bool isEnabled() { return enabled_; };
00100
00101 bool enabled_;
00102 };
00103
00104 BasicFilter g_filter(true);
00105
00106 #define DEFINE_COND_TESTS(name, macro_base, level, log4cxx_level) \
00107 TEST(RosConsole, name##Cond) \
00108 { \
00109 TestAppender* appender = new TestAppender; \
00110 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00111 macro_base##_COND(true, "Testing %d %d %d", 1, 2, 3); \
00112 macro_base##_COND(false, "Testing %d %d %d", 1, 2, 3); \
00113 ASSERT_EQ((int)appender->info_.size(), 1); \
00114 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00115 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00116 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00117 } \
00118 TEST(RosConsole, name##NamedCond) \
00119 { \
00120 TestAppender* appender = new TestAppender; \
00121 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00122 macro_base##_COND_NAMED(true, "test", "Testing %d %d %d", 1, 2, 3); \
00123 macro_base##_COND_NAMED(false, "test", "Testing %d %d %d", 1, 2, 3); \
00124 ASSERT_EQ((int)appender->info_.size(), 1); \
00125 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00126 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00127 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00128 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00129 } \
00130 TEST(RosConsole, name##StreamCond) \
00131 { \
00132 TestAppender* appender = new TestAppender; \
00133 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00134 macro_base##_STREAM_COND(true, "Testing " << 1 << " " << 2 << " " << 3); \
00135 macro_base##_STREAM_COND(false, "Testing " << 1 << " " << 2 << " " << 3); \
00136 ASSERT_EQ((int)appender->info_.size(), 1); \
00137 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00138 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00139 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00140 } \
00141 TEST(RosConsole, name##StreamCondNamed) \
00142 { \
00143 TestAppender* appender = new TestAppender; \
00144 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00145 macro_base##_STREAM_COND_NAMED(true, "test", "Testing " << 1 << " " << 2 << " " << 3); \
00146 macro_base##_STREAM_COND_NAMED(false, "test", "Testing " << 1 << " " << 2 << " " << 3); \
00147 ASSERT_EQ((int)appender->info_.size(), 1); \
00148 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00149 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00150 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00151 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00152 }
00153
00154 #define DEFINE_ONCE_TESTS(name, macro_base, level, log4cxx_level) \
00155 TEST(RosConsole, name##Once) \
00156 { \
00157 TestAppender* appender = new TestAppender; \
00158 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00159 macro_base##_ONCE("Testing %d %d %d", 1, 2, 3); \
00160 ASSERT_EQ((int)appender->info_.size(), 1); \
00161 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00162 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00163 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00164 } \
00165 TEST(RosConsole, name##NamedOnce) \
00166 { \
00167 TestAppender* appender = new TestAppender; \
00168 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00169 macro_base##_ONCE_NAMED("test", "Testing %d %d %d", 1, 2, 3); \
00170 ASSERT_EQ((int)appender->info_.size(), 1); \
00171 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00172 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00173 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00174 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00175 } \
00176 TEST(RosConsole, name##StreamOnce) \
00177 { \
00178 TestAppender* appender = new TestAppender; \
00179 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00180 macro_base##_STREAM_ONCE("Testing " << 1 << " " << 2 << " " << 3); \
00181 ASSERT_EQ((int)appender->info_.size(), 1); \
00182 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00183 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00184 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00185 } \
00186 TEST(RosConsole, name##StreamOnceNamed) \
00187 { \
00188 TestAppender* appender = new TestAppender; \
00189 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00190 macro_base##_STREAM_ONCE_NAMED("test", "Testing " << 1 << " " << 2 << " " << 3); \
00191 ASSERT_EQ((int)appender->info_.size(), 1); \
00192 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00193 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00194 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00195 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00196 }
00197
00198 #define DEFINE_THROTTLE_TESTS(name, macro_base, level, log4cxx_level) \
00199 TEST(RosConsole, name##Throttle) \
00200 { \
00201 TestAppender* appender = new TestAppender; \
00202 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00203 macro_base##_THROTTLE(0.5, "Testing %d %d %d", 1, 2, 3); \
00204 ASSERT_EQ((int)appender->info_.size(), 1); \
00205 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00206 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00207 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00208 } \
00209 TEST(RosConsole, name##NamedThrottle) \
00210 { \
00211 TestAppender* appender = new TestAppender; \
00212 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00213 macro_base##_THROTTLE_NAMED(0.5, "test", "Testing %d %d %d", 1, 2, 3); \
00214 ASSERT_EQ((int)appender->info_.size(), 1); \
00215 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00216 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00217 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00218 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00219 } \
00220 TEST(RosConsole, name##StreamThrottle) \
00221 { \
00222 TestAppender* appender = new TestAppender; \
00223 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00224 macro_base##_STREAM_THROTTLE(0.5, "Testing " << 1 << " " << 2 << " " << 3); \
00225 ASSERT_EQ((int)appender->info_.size(), 1); \
00226 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00227 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00228 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00229 } \
00230 TEST(RosConsole, name##StreamThrottleNamed) \
00231 { \
00232 TestAppender* appender = new TestAppender; \
00233 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00234 macro_base##_STREAM_THROTTLE_NAMED(0.5, "test", "Testing " << 1 << " " << 2 << " " << 3); \
00235 ASSERT_EQ((int)appender->info_.size(), 1); \
00236 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00237 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00238 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00239 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00240 }
00241
00242 #define DEFINE_FILTER_TESTS(name, macro_base, level, log4cxx_level) \
00243 TEST(RosConsole, name##Filter) \
00244 { \
00245 TestAppender* appender = new TestAppender; \
00246 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00247 macro_base##_FILTER(&g_filter, "Testing %d %d %d", 1, 2, 3); \
00248 ASSERT_EQ((int)appender->info_.size(), 1); \
00249 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00250 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00251 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00252 } \
00253 TEST(RosConsole, name##NamedFilter) \
00254 { \
00255 TestAppender* appender = new TestAppender; \
00256 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00257 macro_base##_FILTER_NAMED(&g_filter, "test", "Testing %d %d %d", 1, 2, 3); \
00258 ASSERT_EQ((int)appender->info_.size(), 1); \
00259 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00260 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00261 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00262 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00263 } \
00264 TEST(RosConsole, name##StreamFilter) \
00265 { \
00266 TestAppender* appender = new TestAppender; \
00267 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00268 macro_base##_STREAM_FILTER(&g_filter, "Testing " << 1 << " " << 2 << " " << 3); \
00269 ASSERT_EQ((int)appender->info_.size(), 1); \
00270 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00271 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00272 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00273 } \
00274 TEST(RosConsole, name##StreamFilterNamed) \
00275 { \
00276 TestAppender* appender = new TestAppender; \
00277 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00278 macro_base##_STREAM_FILTER_NAMED(&g_filter, "test", "Testing " << 1 << " " << 2 << " " << 3); \
00279 ASSERT_EQ((int)appender->info_.size(), 1); \
00280 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00281 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00282 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00283 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00284 }
00285
00286 #define DEFINE_LEVEL_TESTS(name, macro_base, level, log4cxx_level) \
00287 TEST(RosConsole, name) \
00288 { \
00289 TestAppender* appender = new TestAppender; \
00290 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00291 macro_base("Testing %d %d %d", 1, 2, 3); \
00292 ASSERT_EQ((int)appender->info_.size(), 1); \
00293 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00294 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00295 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00296 } \
00297 TEST(RosConsole, name##Named) \
00298 { \
00299 TestAppender* appender = new TestAppender; \
00300 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00301 macro_base##_NAMED("test", "Testing %d %d %d", 1, 2, 3); \
00302 ASSERT_EQ((int)appender->info_.size(), 1); \
00303 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00304 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00305 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00306 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00307 } \
00308 TEST(RosConsole, name##Stream) \
00309 { \
00310 TestAppender* appender = new TestAppender; \
00311 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00312 macro_base##_STREAM("Testing " << 1 << " " << 2 << " " << 3); \
00313 ASSERT_EQ((int)appender->info_.size(), 1); \
00314 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00315 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00316 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00317 } \
00318 TEST(RosConsole, name##StreamNamed) \
00319 { \
00320 TestAppender* appender = new TestAppender; \
00321 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->addAppender( appender ); \
00322 macro_base##_STREAM_NAMED("test", "Testing " << 1 << " " << 2 << " " << 3); \
00323 ASSERT_EQ((int)appender->info_.size(), 1); \
00324 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Testing 1 2 3"); \
00325 EXPECT_EQ(appender->info_[0].level_, log4cxx_level); \
00326 EXPECT_STREQ(appender->info_[0].logger_name_.c_str(), ROSCONSOLE_ROOT_LOGGER_NAME".rosconsole.test"); \
00327 log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->removeAppender( appender ); \
00328 } \
00329 DEFINE_COND_TESTS(name, macro_base, level, log4cxx_level) \
00330 DEFINE_ONCE_TESTS(name, macro_base, level, log4cxx_level) \
00331 DEFINE_THROTTLE_TESTS(name, macro_base, level, log4cxx_level) \
00332 DEFINE_FILTER_TESTS(name, macro_base, level, log4cxx_level)
00333
00334 DEFINE_LEVEL_TESTS(debug, ROS_DEBUG, ros::console::levels::Debug, log4cxx::Level::getDebug())
00335 DEFINE_LEVEL_TESTS(info, ROS_INFO, ros::console::levels::Info, log4cxx::Level::getInfo())
00336 DEFINE_LEVEL_TESTS(warn, ROS_WARN, ros::console::levels::Warn, log4cxx::Level::getWarn())
00337 DEFINE_LEVEL_TESTS(error, ROS_ERROR, ros::console::levels::Error, log4cxx::Level::getError())
00338 DEFINE_LEVEL_TESTS(fatal, ROS_FATAL, ros::console::levels::Fatal, log4cxx::Level::getFatal())
00339
00340 TEST(RosConsole, loggingLevels)
00341 {
00342 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00343
00344 TestAppender* appender = new TestAppender;
00345 logger->addAppender( appender );
00346
00347 int pre_count = 0;
00348 int post_count = 0;
00349
00350 {
00351 logger->setLevel( log4cxx::Level::getInfo() );
00352 pre_count = appender->info_.size();
00353 ROS_DEBUG("test");
00354 ROS_INFO("test");
00355 ROS_WARN("test");
00356 ROS_ERROR("test");
00357 ROS_FATAL("test");
00358 post_count = appender->info_.size();
00359 EXPECT_EQ(post_count, pre_count + 4);
00360
00361 logger->setLevel( log4cxx::Level::getWarn() );
00362 pre_count = appender->info_.size();
00363 ROS_DEBUG("test");
00364 ROS_INFO("test");
00365 ROS_WARN("test");
00366 ROS_ERROR("test");
00367 ROS_FATAL("test");
00368 post_count = appender->info_.size();
00369 EXPECT_EQ(post_count, pre_count + 3);
00370
00371 logger->setLevel( log4cxx::Level::getError() );
00372 pre_count = appender->info_.size();
00373 ROS_DEBUG("test");
00374 ROS_INFO("test");
00375 ROS_WARN("test");
00376 ROS_ERROR("test");
00377 ROS_FATAL("test");
00378 post_count = appender->info_.size();
00379 EXPECT_EQ(post_count, pre_count + 2);
00380
00381 logger->setLevel( log4cxx::Level::getFatal() );
00382 pre_count = appender->info_.size();
00383 ROS_DEBUG("test");
00384 ROS_INFO("test");
00385 ROS_WARN("test");
00386 ROS_ERROR("test");
00387 ROS_FATAL("test");
00388 post_count = appender->info_.size();
00389 EXPECT_EQ(post_count, pre_count + 1);
00390
00391 logger->setLevel( log4cxx::Level::getOff() );
00392 pre_count = appender->info_.size();
00393 ROS_DEBUG("test");
00394 ROS_INFO("test");
00395 ROS_WARN("test");
00396 ROS_ERROR("test");
00397 ROS_FATAL("test");
00398 post_count = appender->info_.size();
00399 EXPECT_EQ(post_count, pre_count);
00400 }
00401
00402 {
00403 logger->setLevel( log4cxx::Level::getInfo() );
00404 pre_count = appender->info_.size();
00405 ROS_DEBUG_STREAM("test");
00406 ROS_INFO_STREAM("test");
00407 ROS_WARN_STREAM("test");
00408 ROS_ERROR_STREAM("test");
00409 ROS_FATAL_STREAM("test");
00410 post_count = appender->info_.size();
00411 EXPECT_EQ(post_count, pre_count + 4);
00412
00413 logger->setLevel( log4cxx::Level::getWarn() );
00414 pre_count = appender->info_.size();
00415 ROS_DEBUG_STREAM("test");
00416 ROS_INFO_STREAM("test");
00417 ROS_WARN_STREAM("test");
00418 ROS_ERROR_STREAM("test");
00419 ROS_FATAL_STREAM("test");
00420 post_count = appender->info_.size();
00421 EXPECT_EQ(post_count, pre_count + 3);
00422
00423 logger->setLevel( log4cxx::Level::getError() );
00424 pre_count = appender->info_.size();
00425 ROS_DEBUG_STREAM("test");
00426 ROS_INFO_STREAM("test");
00427 ROS_WARN_STREAM("test");
00428 ROS_ERROR_STREAM("test");
00429 ROS_FATAL_STREAM("test");
00430 post_count = appender->info_.size();
00431 EXPECT_EQ(post_count, pre_count + 2);
00432
00433 logger->setLevel( log4cxx::Level::getFatal() );
00434 pre_count = appender->info_.size();
00435 ROS_DEBUG_STREAM("test");
00436 ROS_INFO_STREAM("test");
00437 ROS_WARN_STREAM("test");
00438 ROS_ERROR_STREAM("test");
00439 ROS_FATAL_STREAM("test");
00440 post_count = appender->info_.size();
00441 EXPECT_EQ(post_count, pre_count + 1);
00442
00443 logger->setLevel( log4cxx::Level::getOff() );
00444 pre_count = appender->info_.size();
00445 ROS_DEBUG_STREAM("test");
00446 ROS_INFO_STREAM("test");
00447 ROS_WARN_STREAM("test");
00448 ROS_ERROR_STREAM("test");
00449 ROS_FATAL_STREAM("test");
00450 post_count = appender->info_.size();
00451 EXPECT_EQ(post_count, pre_count);
00452 }
00453
00454 {
00455 logger->setLevel( log4cxx::Level::getInfo() );
00456 pre_count = appender->info_.size();
00457 ROS_DEBUG_NAMED("test_name", "test");
00458 ROS_INFO_NAMED("test_name", "test");
00459 ROS_WARN_NAMED("test_name", "test");
00460 ROS_ERROR_NAMED("test_name", "test");
00461 ROS_FATAL_NAMED("test_name", "test");
00462 post_count = appender->info_.size();
00463 EXPECT_EQ(post_count, pre_count + 4);
00464
00465 logger->setLevel( log4cxx::Level::getWarn() );
00466 pre_count = appender->info_.size();
00467 ROS_DEBUG_NAMED("test_name", "test");
00468 ROS_INFO_NAMED("test_name", "test");
00469 ROS_WARN_NAMED("test_name", "test");
00470 ROS_ERROR_NAMED("test_name", "test");
00471 ROS_FATAL_NAMED("test_name", "test");
00472 post_count = appender->info_.size();
00473 EXPECT_EQ(post_count, pre_count + 3);
00474
00475 logger->setLevel( log4cxx::Level::getError() );
00476 pre_count = appender->info_.size();
00477 ROS_DEBUG_NAMED("test_name", "test");
00478 ROS_INFO_NAMED("test_name", "test");
00479 ROS_WARN_NAMED("test_name", "test");
00480 ROS_ERROR_NAMED("test_name", "test");
00481 ROS_FATAL_NAMED("test_name", "test");
00482 post_count = appender->info_.size();
00483 EXPECT_EQ(post_count, pre_count + 2);
00484
00485 logger->setLevel( log4cxx::Level::getFatal() );
00486 pre_count = appender->info_.size();
00487 ROS_DEBUG_NAMED("test_name", "test");
00488 ROS_INFO_NAMED("test_name", "test");
00489 ROS_WARN_NAMED("test_name", "test");
00490 ROS_ERROR_NAMED("test_name", "test");
00491 ROS_FATAL_NAMED("test_name", "test");
00492 post_count = appender->info_.size();
00493 EXPECT_EQ(post_count, pre_count + 1);
00494
00495 logger->setLevel( log4cxx::Level::getOff() );
00496 pre_count = appender->info_.size();
00497 ROS_DEBUG_NAMED("test_name", "test");
00498 ROS_INFO_NAMED("test_name", "test");
00499 ROS_WARN_NAMED("test_name", "test");
00500 ROS_ERROR_NAMED("test_name", "test");
00501 ROS_FATAL_NAMED("test_name", "test");
00502 post_count = appender->info_.size();
00503 EXPECT_EQ(post_count, pre_count);
00504 }
00505
00506 {
00507 logger->setLevel( log4cxx::Level::getInfo() );
00508 pre_count = appender->info_.size();
00509 ROS_DEBUG_STREAM_NAMED("test_name", "test");
00510 ROS_INFO_STREAM_NAMED("test_name", "test");
00511 ROS_WARN_STREAM_NAMED("test_name", "test");
00512 ROS_ERROR_STREAM_NAMED("test_name", "test");
00513 ROS_FATAL_STREAM_NAMED("test_name", "test");
00514 post_count = appender->info_.size();
00515 EXPECT_EQ(post_count, pre_count + 4);
00516
00517 logger->setLevel( log4cxx::Level::getWarn() );
00518 pre_count = appender->info_.size();
00519 ROS_DEBUG_STREAM_NAMED("test_name", "test");
00520 ROS_INFO_STREAM_NAMED("test_name", "test");
00521 ROS_WARN_STREAM_NAMED("test_name", "test");
00522 ROS_ERROR_STREAM_NAMED("test_name", "test");
00523 ROS_FATAL_STREAM_NAMED("test_name", "test");
00524 post_count = appender->info_.size();
00525 EXPECT_EQ(post_count, pre_count + 3);
00526
00527 logger->setLevel( log4cxx::Level::getError() );
00528 pre_count = appender->info_.size();
00529 ROS_DEBUG_STREAM_NAMED("test_name", "test");
00530 ROS_INFO_STREAM_NAMED("test_name", "test");
00531 ROS_WARN_STREAM_NAMED("test_name", "test");
00532 ROS_ERROR_STREAM_NAMED("test_name", "test");
00533 ROS_FATAL_STREAM_NAMED("test_name", "test");
00534 post_count = appender->info_.size();
00535 EXPECT_EQ(post_count, pre_count + 2);
00536
00537 logger->setLevel( log4cxx::Level::getFatal() );
00538 pre_count = appender->info_.size();
00539 ROS_DEBUG_STREAM_NAMED("test_name", "test");
00540 ROS_INFO_STREAM_NAMED("test_name", "test");
00541 ROS_WARN_STREAM_NAMED("test_name", "test");
00542 ROS_ERROR_STREAM_NAMED("test_name", "test");
00543 ROS_FATAL_STREAM_NAMED("test_name", "test");
00544 post_count = appender->info_.size();
00545 EXPECT_EQ(post_count, pre_count + 1);
00546
00547 logger->setLevel( log4cxx::Level::getOff() );
00548 pre_count = appender->info_.size();
00549 ROS_DEBUG_STREAM_NAMED("test_name", "test");
00550 ROS_INFO_STREAM_NAMED("test_name", "test");
00551 ROS_WARN_STREAM_NAMED("test_name", "test");
00552 ROS_ERROR_STREAM_NAMED("test_name", "test");
00553 ROS_FATAL_STREAM_NAMED("test_name", "test");
00554 post_count = appender->info_.size();
00555 EXPECT_EQ(post_count, pre_count);
00556 }
00557
00558 logger->removeAppender( appender );
00559 }
00560
00561 TEST(RosConsole, changingLevel)
00562 {
00563 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00564
00565 TestAppender* appender = new TestAppender;
00566 logger->addAppender( appender );
00567
00568 logger->setLevel( log4cxx::Level::getError() );
00569 for ( int i = ros::console::levels::Debug; i < ros::console::levels::Count; ++i )
00570 {
00571 ROS_LOG((ros::console::Level)i, ROSCONSOLE_DEFAULT_NAME, "test");
00572 }
00573
00574 EXPECT_EQ((int)appender->info_.size(), 2);
00575
00576 logger->removeAppender( appender );
00577
00578 logger->setLevel( log4cxx::Level::getDebug() );
00579 }
00580
00581 TEST(RosConsole, changingLoggerLevel)
00582 {
00583 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00584
00585 TestAppender* appender = new TestAppender;
00586 logger->addAppender( appender );
00587
00588 logger->setLevel(log4cxx::Level::getDebug());
00589 ros::console::notifyLoggerLevelsChanged();
00590 ROS_LOG(ros::console::levels::Debug, ROSCONSOLE_DEFAULT_NAME, "test");
00591
00592 logger->setLevel(log4cxx::Level::getInfo());
00593 ros::console::notifyLoggerLevelsChanged();
00594 ROS_LOG(ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "test");
00595
00596 logger->setLevel(log4cxx::Level::getWarn());
00597 ros::console::notifyLoggerLevelsChanged();
00598 ROS_LOG(ros::console::levels::Warn, ROSCONSOLE_DEFAULT_NAME, "test");
00599
00600 logger->setLevel(log4cxx::Level::getError());
00601 ros::console::notifyLoggerLevelsChanged();
00602 ROS_LOG(ros::console::levels::Error, ROSCONSOLE_DEFAULT_NAME, "test");
00603
00604 logger->setLevel(log4cxx::Level::getFatal());
00605 ros::console::notifyLoggerLevelsChanged();
00606 ROS_LOG(ros::console::levels::Fatal, ROSCONSOLE_DEFAULT_NAME, "test");
00607
00608 EXPECT_EQ((int)appender->info_.size(), 5);
00609
00610 logger->removeAppender( appender );
00611
00612 logger->setLevel( log4cxx::Level::getDebug() );
00613 }
00614
00615 TEST(RosConsole, longPrintfStyleOutput)
00616 {
00617 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00618
00619 TestAppender* appender = new TestAppender;
00620 logger->addAppender( appender );
00621
00622 std::stringstream ss;
00623 for (int i = 0; i < 100000; ++i )
00624 {
00625 ss << 'a';
00626 }
00627
00628 ROS_INFO("%s", ss.str().c_str());
00629
00630 ASSERT_EQ((int)appender->info_.size(), 1);
00631 EXPECT_STREQ(appender->info_[0].message_.c_str(), ss.str().c_str());
00632
00633 logger->removeAppender( appender );
00634
00635 logger->setLevel( log4cxx::Level::getDebug() );
00636 }
00637
00638 TEST(RosConsole, throwingAppender)
00639 {
00640 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00641
00642 TestAppenderWithThrow* appender = new TestAppenderWithThrow;
00643 logger->addAppender( appender );
00644
00645 try
00646 {
00647 ROS_INFO("Hello there");
00648 }
00649 catch (std::exception& e)
00650 {
00651 FAIL();
00652 }
00653
00654 logger->removeAppender( appender );
00655 logger->setLevel( log4cxx::Level::getDebug() );
00656 }
00657
00658 void onceFunc()
00659 {
00660 ROS_LOG_ONCE(ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00661 }
00662
00663 TEST(RosConsole, once)
00664 {
00665 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00666
00667 TestAppender* appender = new TestAppender;
00668 logger->addAppender(appender);
00669
00670 onceFunc();
00671 onceFunc();
00672
00673 EXPECT_EQ(appender->info_.size(), 1ULL);
00674
00675 logger->removeAppender(appender);
00676 }
00677
00678 void throttleFunc()
00679 {
00680 ROS_LOG_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00681 }
00682
00683 TEST(RosConsole, throttle)
00684 {
00685 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00686
00687 TestAppender* appender = new TestAppender;
00688 logger->addAppender(appender);
00689
00690 ros::Time start = ros::Time::now();
00691 while (ros::Time::now() <= start + ros::Duration(0.5))
00692 {
00693 throttleFunc();
00694 ros::Duration(0.01).sleep();
00695 }
00696
00697 throttleFunc();
00698
00699 EXPECT_EQ(appender->info_.size(), 2ULL);
00700
00701 logger->removeAppender(appender);
00702 }
00703
00704 void delayedThrottleFunc()
00705 {
00706 ROS_LOG_DELAYED_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00707 }
00708
00709 void delayedThrottleFunc2()
00710 {
00711 ROS_LOG_DELAYED_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00712 }
00713
00714 TEST(RosConsole, delayedThrottle)
00715 {
00716 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00717
00718 TestAppender* appender = new TestAppender;
00719 logger->addAppender(appender);
00720
00721 ros::Time start = ros::Time::now();
00722 while (ros::Time::now() <= start + ros::Duration(0.4))
00723 {
00724 delayedThrottleFunc();
00725 ros::Duration(0.01).sleep();
00726 }
00727
00728 EXPECT_EQ(appender->info_.size(), 0ULL);
00729
00730 const int pre_count = appender->info_.size();
00731 start = ros::Time::now();
00732 while (ros::Time::now() <= start + ros::Duration(0.6))
00733 {
00734 delayedThrottleFunc2();
00735 ros::Duration(0.01).sleep();
00736 }
00737
00738 const int post_count = appender->info_.size();
00739
00740 EXPECT_EQ(post_count, pre_count + 1);
00741
00742 logger->removeAppender(appender);
00743 }
00744
00745
00746 void onceStreamFunc()
00747 {
00748 ROS_LOG_STREAM_ONCE(ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00749 }
00750
00751 TEST(RosConsole, onceStream)
00752 {
00753 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00754
00755 TestAppender* appender = new TestAppender;
00756 logger->addAppender(appender);
00757
00758 onceStreamFunc();
00759 onceStreamFunc();
00760
00761 EXPECT_EQ(appender->info_.size(), 1ULL);
00762
00763 logger->removeAppender(appender);
00764 }
00765
00766 void throttleStreamFunc()
00767 {
00768 ROS_LOG_STREAM_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00769 }
00770
00771 TEST(RosConsole, throttleStream)
00772 {
00773 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00774
00775 TestAppender* appender = new TestAppender;
00776 logger->addAppender(appender);
00777
00778 ros::Time start = ros::Time::now();
00779 while (ros::Time::now() <= start + ros::Duration(0.5))
00780 {
00781 throttleStreamFunc();
00782 ros::Duration(0.01).sleep();
00783 }
00784
00785 throttleStreamFunc();
00786
00787 EXPECT_EQ(appender->info_.size(), 2ULL);
00788
00789 logger->removeAppender(appender);
00790 }
00791
00792 void delayedThrottleStreamFunc()
00793 {
00794 ROS_LOG_STREAM_DELAYED_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00795 }
00796
00797 void delayedThrottleStreamFunc2()
00798 {
00799 ROS_LOG_STREAM_DELAYED_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00800 }
00801
00802 TEST(RosConsole, delayedStreamThrottle)
00803 {
00804 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00805
00806 TestAppender* appender = new TestAppender;
00807 logger->addAppender(appender);
00808
00809 ros::Time start = ros::Time::now();
00810 while (ros::Time::now() <= start + ros::Duration(0.4))
00811 {
00812 delayedThrottleStreamFunc();
00813 ros::Duration(0.01).sleep();
00814 }
00815
00816 EXPECT_EQ(appender->info_.size(), 0ULL);
00817
00818 const int pre_count = appender->info_.size();
00819 start = ros::Time::now();
00820 while (ros::Time::now() <= start + ros::Duration(0.6))
00821 {
00822 delayedThrottleStreamFunc2();
00823 ros::Duration(0.01).sleep();
00824 }
00825
00826 const int post_count = appender->info_.size();
00827
00828 EXPECT_EQ(post_count, pre_count + 1);
00829
00830 logger->removeAppender(appender);
00831 }
00832
00833 TEST(RosConsole, basicFilter)
00834 {
00835 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00836
00837 TestAppender* appender = new TestAppender;
00838 logger->addAppender(appender);
00839
00840 BasicFilter trueFilter(true), falseFilter(false);
00841 ROS_LOG_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00842 ROS_LOG_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00843
00844 ASSERT_EQ(appender->info_.size(), 1ULL);
00845 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello");
00846
00847 logger->removeAppender(appender);
00848 }
00849
00850 TEST(RosConsole, basicFilterStream)
00851 {
00852 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00853
00854 TestAppender* appender = new TestAppender;
00855 logger->addAppender(appender);
00856
00857 BasicFilter trueFilter(true), falseFilter(false);
00858 ROS_LOG_STREAM_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00859 ROS_LOG_STREAM_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00860
00861 ASSERT_EQ(appender->info_.size(), 1ULL);
00862 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello");
00863
00864 logger->removeAppender(appender);
00865 }
00866
00867 struct AdvancedFilter : public ros::console::FilterBase
00868 {
00869 AdvancedFilter(bool enabled)
00870 : enabled_(enabled)
00871 , count_(0)
00872 {}
00873
00874 using ros::console::FilterBase::isEnabled;
00875 inline virtual bool isEnabled(ros::console::FilterParams& params)
00876 {
00877 fprintf(stderr, "%s %s:%d:%s\n", params.message, params.file, params.line, params.function);
00878 ++count_;
00879 return enabled_;
00880 }
00881
00882 bool enabled_;
00883 int count_;
00884 };
00885
00886 TEST(RosConsole, advancedFilter)
00887 {
00888 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00889
00890 TestAppender* appender = new TestAppender;
00891 logger->addAppender(appender);
00892
00893 AdvancedFilter trueFilter(true), falseFilter(false);
00894 ROS_LOG_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00895 ROS_LOG_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00896
00897 ASSERT_EQ(appender->info_.size(), 1ULL);
00898 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello");
00899 EXPECT_EQ(trueFilter.count_, 1);
00900 EXPECT_EQ(falseFilter.count_, 1);
00901
00902 logger->removeAppender(appender);
00903 }
00904
00905 TEST(RosConsole, advancedFilterStream)
00906 {
00907 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00908
00909 TestAppender* appender = new TestAppender;
00910 logger->addAppender(appender);
00911
00912 AdvancedFilter trueFilter(true), falseFilter(false);
00913 ROS_LOG_STREAM_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00914 ROS_LOG_STREAM_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00915
00916 ASSERT_EQ(appender->info_.size(), 1ULL);
00917 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello");
00918 EXPECT_EQ(trueFilter.count_, 1);
00919 EXPECT_EQ(falseFilter.count_, 1);
00920
00921 logger->removeAppender(appender);
00922 }
00923
00924 struct ChangeFilter : public ros::console::FilterBase
00925 {
00926 using ros::console::FilterBase::isEnabled;
00927 inline virtual bool isEnabled(ros::console::FilterParams& params)
00928 {
00929 params.out_message = "haha";
00930 params.level = ros::console::levels::Error;
00931 return true;
00932 }
00933 };
00934
00935 TEST(RosConsole, changeFilter)
00936 {
00937 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00938
00939 TestAppender* appender = new TestAppender;
00940 logger->addAppender(appender);
00941
00942 ChangeFilter filter;
00943 ROS_LOG_FILTER(&filter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00944
00945 ASSERT_EQ(appender->info_.size(), 1ULL);
00946 EXPECT_STREQ(appender->info_[0].message_.c_str(), "haha");
00947 EXPECT_EQ(appender->info_[0].level_, log4cxx::Level::getError());
00948
00949 logger->removeAppender(appender);
00950 }
00951
00952 TEST(RosConsole, changeFilterStream)
00953 {
00954 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00955
00956 TestAppender* appender = new TestAppender;
00957 logger->addAppender(appender);
00958
00959 ChangeFilter filter;
00960 ROS_LOG_STREAM_FILTER(&filter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00961
00962 ASSERT_EQ(appender->info_.size(), 1ULL);
00963 EXPECT_STREQ(appender->info_[0].message_.c_str(), "haha");
00964 EXPECT_EQ(appender->info_[0].level_, log4cxx::Level::getError());
00965
00966 logger->removeAppender(appender);
00967 }
00968
00969 TEST(RosConsole, formatToBufferInitialZero)
00970 {
00971 boost::shared_array<char> buffer;
00972 size_t size = 0;
00973 ros::console::formatToBuffer(buffer, size, "Hello World %d", 5);
00974 EXPECT_EQ(size, 14U);
00975 EXPECT_STREQ(buffer.get(), "Hello World 5");
00976 }
00977
00978 TEST(RosConsole, formatToBufferInitialLargerThanFormat)
00979 {
00980 boost::shared_array<char> buffer(new char[30]);
00981 size_t size = 30;
00982 ros::console::formatToBuffer(buffer, size, "Hello World %d", 5);
00983 EXPECT_EQ(size, 30U);
00984 EXPECT_STREQ(buffer.get(), "Hello World 5");
00985 }
00986
00987 TEST(RosConsole, formatToString)
00988 {
00989 std::string str = ros::console::formatToString("Hello World %d", 5);
00990 EXPECT_STREQ(str.c_str(), "Hello World 5");
00991 }
00992
00993 int main(int argc, char **argv)
00994 {
00995 testing::InitGoogleTest(&argc, argv);
00996 ros::Time::init();
00997
00998 ROSCONSOLE_AUTOINIT;
00999 log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME)->removeAllAppenders();
01000 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug());
01001 log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME)->setLevel(log4cxx::Level::getDebug());
01002
01003 return RUN_ALL_TESTS();
01004 }