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& 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& event, log4cxx::helpers::Pool& 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 onceStreamFunc()
00705 {
00706 ROS_LOG_STREAM_ONCE(ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00707 }
00708
00709 TEST(RosConsole, onceStream)
00710 {
00711 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00712
00713 TestAppender* appender = new TestAppender;
00714 logger->addAppender(appender);
00715
00716 onceStreamFunc();
00717 onceStreamFunc();
00718
00719 EXPECT_EQ(appender->info_.size(), 1ULL);
00720
00721 logger->removeAppender(appender);
00722 }
00723
00724 void throttleStreamFunc()
00725 {
00726 ROS_LOG_STREAM_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00727 }
00728
00729 TEST(RosConsole, throttleStream)
00730 {
00731 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00732
00733 TestAppender* appender = new TestAppender;
00734 logger->addAppender(appender);
00735
00736 ros::Time start = ros::Time::now();
00737 while (ros::Time::now() <= start + ros::Duration(0.5))
00738 {
00739 throttleStreamFunc();
00740 ros::Duration(0.01).sleep();
00741 }
00742
00743 throttleStreamFunc();
00744
00745 EXPECT_EQ(appender->info_.size(), 2ULL);
00746
00747 logger->removeAppender(appender);
00748 }
00749
00750 TEST(RosConsole, basicFilter)
00751 {
00752 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00753
00754 TestAppender* appender = new TestAppender;
00755 logger->addAppender(appender);
00756
00757 BasicFilter trueFilter(true), falseFilter(false);
00758 ROS_LOG_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00759 ROS_LOG_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00760
00761 ASSERT_EQ(appender->info_.size(), 1ULL);
00762 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello");
00763
00764 logger->removeAppender(appender);
00765 }
00766
00767 TEST(RosConsole, basicFilterStream)
00768 {
00769 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00770
00771 TestAppender* appender = new TestAppender;
00772 logger->addAppender(appender);
00773
00774 BasicFilter trueFilter(true), falseFilter(false);
00775 ROS_LOG_STREAM_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00776 ROS_LOG_STREAM_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00777
00778 ASSERT_EQ(appender->info_.size(), 1ULL);
00779 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello");
00780
00781 logger->removeAppender(appender);
00782 }
00783
00784 struct AdvancedFilter : public ros::console::FilterBase
00785 {
00786 AdvancedFilter(bool enabled)
00787 : enabled_(enabled)
00788 , count_(0)
00789 {}
00790
00791 using ros::console::FilterBase::isEnabled;
00792 inline virtual bool isEnabled(ros::console::FilterParams& params)
00793 {
00794 fprintf(stderr, "%s %s:%d:%s\n", params.message, params.file, params.line, params.function);
00795 ++count_;
00796 return enabled_;
00797 }
00798
00799 bool enabled_;
00800 int count_;
00801 };
00802
00803 TEST(RosConsole, advancedFilter)
00804 {
00805 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00806
00807 TestAppender* appender = new TestAppender;
00808 logger->addAppender(appender);
00809
00810 AdvancedFilter trueFilter(true), falseFilter(false);
00811 ROS_LOG_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00812 ROS_LOG_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00813
00814 ASSERT_EQ(appender->info_.size(), 1ULL);
00815 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello");
00816 EXPECT_EQ(trueFilter.count_, 1);
00817 EXPECT_EQ(falseFilter.count_, 1);
00818
00819 logger->removeAppender(appender);
00820 }
00821
00822 TEST(RosConsole, advancedFilterStream)
00823 {
00824 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00825
00826 TestAppender* appender = new TestAppender;
00827 logger->addAppender(appender);
00828
00829 AdvancedFilter trueFilter(true), falseFilter(false);
00830 ROS_LOG_STREAM_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00831 ROS_LOG_STREAM_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2");
00832
00833 ASSERT_EQ(appender->info_.size(), 1ULL);
00834 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello");
00835 EXPECT_EQ(trueFilter.count_, 1);
00836 EXPECT_EQ(falseFilter.count_, 1);
00837
00838 logger->removeAppender(appender);
00839 }
00840
00841 struct ChangeFilter : public ros::console::FilterBase
00842 {
00843 using ros::console::FilterBase::isEnabled;
00844 inline virtual bool isEnabled(ros::console::FilterParams& params)
00845 {
00846 params.out_message = "haha";
00847 params.level = ros::console::levels::Error;
00848 return true;
00849 }
00850 };
00851
00852 TEST(RosConsole, changeFilter)
00853 {
00854 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00855
00856 TestAppender* appender = new TestAppender;
00857 logger->addAppender(appender);
00858
00859 ChangeFilter filter;
00860 ROS_LOG_FILTER(&filter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00861
00862 ASSERT_EQ(appender->info_.size(), 1ULL);
00863 EXPECT_STREQ(appender->info_[0].message_.c_str(), "haha");
00864 EXPECT_EQ(appender->info_[0].level_, log4cxx::Level::getError());
00865
00866 logger->removeAppender(appender);
00867 }
00868
00869 TEST(RosConsole, changeFilterStream)
00870 {
00871 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00872
00873 TestAppender* appender = new TestAppender;
00874 logger->addAppender(appender);
00875
00876 ChangeFilter filter;
00877 ROS_LOG_STREAM_FILTER(&filter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello");
00878
00879 ASSERT_EQ(appender->info_.size(), 1ULL);
00880 EXPECT_STREQ(appender->info_[0].message_.c_str(), "haha");
00881 EXPECT_EQ(appender->info_[0].level_, log4cxx::Level::getError());
00882
00883 logger->removeAppender(appender);
00884 }
00885
00886 TEST(RosConsole, formatToBufferInitialZero)
00887 {
00888 boost::shared_array<char> buffer;
00889 size_t size = 0;
00890 ros::console::formatToBuffer(buffer, size, "Hello World %d", 5);
00891 EXPECT_EQ(size, 14U);
00892 EXPECT_STREQ(buffer.get(), "Hello World 5");
00893 }
00894
00895 TEST(RosConsole, formatToBufferInitialLargerThanFormat)
00896 {
00897 boost::shared_array<char> buffer(new char[30]);
00898 size_t size = 30;
00899 ros::console::formatToBuffer(buffer, size, "Hello World %d", 5);
00900 EXPECT_EQ(size, 30U);
00901 EXPECT_STREQ(buffer.get(), "Hello World 5");
00902 }
00903
00904 TEST(RosConsole, formatToString)
00905 {
00906 std::string str = ros::console::formatToString("Hello World %d", 5);
00907 EXPECT_STREQ(str.c_str(), "Hello World 5");
00908 }
00909
00910 int main(int argc, char **argv)
00911 {
00912 testing::InitGoogleTest(&argc, argv);
00913 ros::Time::init();
00914
00915 ROSCONSOLE_AUTOINIT;
00916 log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME)->removeAllAppenders();
00917 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug());
00918 log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME)->setLevel(log4cxx::Level::getDebug());
00919
00920 return RUN_ALL_TESTS();
00921 }