$search
00001 /* 00002 * Copyright (c) 2008, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 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) \ 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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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) \ 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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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) \ 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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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) \ 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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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) \ 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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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_, ros::console::g_level_lookup[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) \ 00330 DEFINE_ONCE_TESTS(name, macro_base, level) \ 00331 DEFINE_THROTTLE_TESTS(name, macro_base, level) \ 00332 DEFINE_FILTER_TESTS(name, macro_base, level) 00333 00334 DEFINE_LEVEL_TESTS(debug, ROS_DEBUG, ros::console::levels::Debug) 00335 DEFINE_LEVEL_TESTS(info, ROS_INFO, ros::console::levels::Info) 00336 DEFINE_LEVEL_TESTS(warn, ROS_WARN, ros::console::levels::Warn) 00337 DEFINE_LEVEL_TESTS(error, ROS_ERROR, ros::console::levels::Error) 00338 DEFINE_LEVEL_TESTS(fatal, ROS_FATAL, ros::console::levels::Fatal) 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 for ( int i = ros::console::levels::Fatal; i >= 0; --i ) 00589 { 00590 logger->setLevel( ros::console::g_level_lookup[i] ); 00591 ros::console::notifyLoggerLevelsChanged(); 00592 ROS_LOG((ros::console::Level)i, ROSCONSOLE_DEFAULT_NAME, "test"); 00593 } 00594 00595 EXPECT_EQ((int)appender->info_.size(), 5); 00596 00597 logger->removeAppender( appender ); 00598 00599 logger->setLevel( log4cxx::Level::getDebug() ); 00600 } 00601 00602 TEST(RosConsole, longPrintfStyleOutput) 00603 { 00604 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00605 00606 TestAppender* appender = new TestAppender; 00607 logger->addAppender( appender ); 00608 00609 std::stringstream ss; 00610 for (int i = 0; i < 100000; ++i ) 00611 { 00612 ss << 'a'; 00613 } 00614 00615 ROS_INFO("%s", ss.str().c_str()); 00616 00617 ASSERT_EQ((int)appender->info_.size(), 1); 00618 EXPECT_STREQ(appender->info_[0].message_.c_str(), ss.str().c_str()); 00619 00620 logger->removeAppender( appender ); 00621 00622 logger->setLevel( log4cxx::Level::getDebug() ); 00623 } 00624 00625 TEST(RosConsole, throwingAppender) 00626 { 00627 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00628 00629 TestAppenderWithThrow* appender = new TestAppenderWithThrow; 00630 logger->addAppender( appender ); 00631 00632 try 00633 { 00634 ROS_INFO("Hello there"); 00635 } 00636 catch (std::exception& e) 00637 { 00638 FAIL(); 00639 } 00640 00641 logger->removeAppender( appender ); 00642 logger->setLevel( log4cxx::Level::getDebug() ); 00643 } 00644 00645 void onceFunc() 00646 { 00647 ROS_LOG_ONCE(ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00648 } 00649 00650 TEST(RosConsole, once) 00651 { 00652 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00653 00654 TestAppender* appender = new TestAppender; 00655 logger->addAppender(appender); 00656 00657 onceFunc(); 00658 onceFunc(); 00659 00660 EXPECT_EQ(appender->info_.size(), 1ULL); 00661 00662 logger->removeAppender(appender); 00663 } 00664 00665 void throttleFunc() 00666 { 00667 ROS_LOG_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00668 } 00669 00670 TEST(RosConsole, throttle) 00671 { 00672 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00673 00674 TestAppender* appender = new TestAppender; 00675 logger->addAppender(appender); 00676 00677 ros::Time start = ros::Time::now(); 00678 while (ros::Time::now() <= start + ros::Duration(0.5)) 00679 { 00680 throttleFunc(); 00681 ros::Duration(0.01).sleep(); 00682 } 00683 00684 throttleFunc(); 00685 00686 EXPECT_EQ(appender->info_.size(), 2ULL); 00687 00688 logger->removeAppender(appender); 00689 } 00690 00691 void onceStreamFunc() 00692 { 00693 ROS_LOG_STREAM_ONCE(ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00694 } 00695 00696 TEST(RosConsole, onceStream) 00697 { 00698 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00699 00700 TestAppender* appender = new TestAppender; 00701 logger->addAppender(appender); 00702 00703 onceStreamFunc(); 00704 onceStreamFunc(); 00705 00706 EXPECT_EQ(appender->info_.size(), 1ULL); 00707 00708 logger->removeAppender(appender); 00709 } 00710 00711 void throttleStreamFunc() 00712 { 00713 ROS_LOG_STREAM_THROTTLE(0.5, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00714 } 00715 00716 TEST(RosConsole, throttleStream) 00717 { 00718 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00719 00720 TestAppender* appender = new TestAppender; 00721 logger->addAppender(appender); 00722 00723 ros::Time start = ros::Time::now(); 00724 while (ros::Time::now() <= start + ros::Duration(0.5)) 00725 { 00726 throttleStreamFunc(); 00727 ros::Duration(0.01).sleep(); 00728 } 00729 00730 throttleStreamFunc(); 00731 00732 EXPECT_EQ(appender->info_.size(), 2ULL); 00733 00734 logger->removeAppender(appender); 00735 } 00736 00737 TEST(RosConsole, basicFilter) 00738 { 00739 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00740 00741 TestAppender* appender = new TestAppender; 00742 logger->addAppender(appender); 00743 00744 BasicFilter trueFilter(true), falseFilter(false); 00745 ROS_LOG_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00746 ROS_LOG_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2"); 00747 00748 ASSERT_EQ(appender->info_.size(), 1ULL); 00749 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello"); 00750 00751 logger->removeAppender(appender); 00752 } 00753 00754 TEST(RosConsole, basicFilterStream) 00755 { 00756 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00757 00758 TestAppender* appender = new TestAppender; 00759 logger->addAppender(appender); 00760 00761 BasicFilter trueFilter(true), falseFilter(false); 00762 ROS_LOG_STREAM_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00763 ROS_LOG_STREAM_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2"); 00764 00765 ASSERT_EQ(appender->info_.size(), 1ULL); 00766 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello"); 00767 00768 logger->removeAppender(appender); 00769 } 00770 00771 struct AdvancedFilter : public ros::console::FilterBase 00772 { 00773 AdvancedFilter(bool enabled) 00774 : enabled_(enabled) 00775 , count_(0) 00776 {} 00777 00778 using ros::console::FilterBase::isEnabled; 00779 inline virtual bool isEnabled(ros::console::FilterParams& params) 00780 { 00781 fprintf(stderr, "%s %s:%d:%s\n", params.message, params.file, params.line, params.function); 00782 ++count_; 00783 return enabled_; 00784 } 00785 00786 bool enabled_; 00787 int count_; 00788 }; 00789 00790 TEST(RosConsole, advancedFilter) 00791 { 00792 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00793 00794 TestAppender* appender = new TestAppender; 00795 logger->addAppender(appender); 00796 00797 AdvancedFilter trueFilter(true), falseFilter(false); 00798 ROS_LOG_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00799 ROS_LOG_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2"); 00800 00801 ASSERT_EQ(appender->info_.size(), 1ULL); 00802 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello"); 00803 EXPECT_EQ(trueFilter.count_, 1); 00804 EXPECT_EQ(falseFilter.count_, 1); 00805 00806 logger->removeAppender(appender); 00807 } 00808 00809 TEST(RosConsole, advancedFilterStream) 00810 { 00811 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00812 00813 TestAppender* appender = new TestAppender; 00814 logger->addAppender(appender); 00815 00816 AdvancedFilter trueFilter(true), falseFilter(false); 00817 ROS_LOG_STREAM_FILTER(&trueFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00818 ROS_LOG_STREAM_FILTER(&falseFilter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello2"); 00819 00820 ASSERT_EQ(appender->info_.size(), 1ULL); 00821 EXPECT_STREQ(appender->info_[0].message_.c_str(), "Hello"); 00822 EXPECT_EQ(trueFilter.count_, 1); 00823 EXPECT_EQ(falseFilter.count_, 1); 00824 00825 logger->removeAppender(appender); 00826 } 00827 00828 struct ChangeFilter : public ros::console::FilterBase 00829 { 00830 using ros::console::FilterBase::isEnabled; 00831 inline virtual bool isEnabled(ros::console::FilterParams& params) 00832 { 00833 params.out_message = "haha"; 00834 params.level = ros::console::levels::Error; 00835 return true; 00836 } 00837 }; 00838 00839 TEST(RosConsole, changeFilter) 00840 { 00841 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00842 00843 TestAppender* appender = new TestAppender; 00844 logger->addAppender(appender); 00845 00846 ChangeFilter filter; 00847 ROS_LOG_FILTER(&filter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00848 00849 ASSERT_EQ(appender->info_.size(), 1ULL); 00850 EXPECT_STREQ(appender->info_[0].message_.c_str(), "haha"); 00851 EXPECT_EQ(appender->info_[0].level_, log4cxx::Level::getError()); 00852 00853 logger->removeAppender(appender); 00854 } 00855 00856 TEST(RosConsole, changeFilterStream) 00857 { 00858 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME); 00859 00860 TestAppender* appender = new TestAppender; 00861 logger->addAppender(appender); 00862 00863 ChangeFilter filter; 00864 ROS_LOG_STREAM_FILTER(&filter, ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, "Hello"); 00865 00866 ASSERT_EQ(appender->info_.size(), 1ULL); 00867 EXPECT_STREQ(appender->info_[0].message_.c_str(), "haha"); 00868 EXPECT_EQ(appender->info_[0].level_, log4cxx::Level::getError()); 00869 00870 logger->removeAppender(appender); 00871 } 00872 00873 TEST(RosConsole, formatToBufferInitialZero) 00874 { 00875 boost::shared_array<char> buffer; 00876 size_t size = 0; 00877 ros::console::formatToBuffer(buffer, size, "Hello World %d", 5); 00878 EXPECT_EQ(size, 14U); 00879 EXPECT_STREQ(buffer.get(), "Hello World 5"); 00880 } 00881 00882 TEST(RosConsole, formatToBufferInitialLargerThanFormat) 00883 { 00884 boost::shared_array<char> buffer(new char[30]); 00885 size_t size = 30; 00886 ros::console::formatToBuffer(buffer, size, "Hello World %d", 5); 00887 EXPECT_EQ(size, 30U); 00888 EXPECT_STREQ(buffer.get(), "Hello World 5"); 00889 } 00890 00891 TEST(RosConsole, formatToString) 00892 { 00893 std::string str = ros::console::formatToString("Hello World %d", 5); 00894 EXPECT_STREQ(str.c_str(), "Hello World 5"); 00895 } 00896 00897 int main(int argc, char **argv) 00898 { 00899 testing::InitGoogleTest(&argc, argv); 00900 ros::Time::init(); 00901 00902 ROSCONSOLE_AUTOINIT; 00903 log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME)->removeAllAppenders(); 00904 log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug()); 00905 log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME)->setLevel(log4cxx::Level::getDebug()); 00906 00907 return RUN_ALL_TESTS(); 00908 }