example.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of Willow Garage, Inc. nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 ********************************************************************/
00034 
00035 #include "ros/console.h"
00036 #include <log4cxx/logger.h>
00037 
00038 void print(ros::console::Level level, const std::string& s)
00039 {
00040   ROS_LOG(level, ROSCONSOLE_DEFAULT_NAME, "%s", s.c_str());
00041 }
00042 
00043 int main(int /*argc*/, char** /*argv*/)
00044 {
00045   // This needs to happen before we start fooling around with logger levels.  Otherwise the level we set may be overwritten by
00046   // a configuration file
00047   ROSCONSOLE_AUTOINIT;
00048 
00049   log4cxx::LoggerPtr my_logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
00050 
00051   // Set the logger for this package to output all statements
00052   my_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Debug]);
00053 
00054   // All these should print
00055   ROS_DEBUG("This is a debug statement, and should print");
00056   ROS_INFO("This is an info statement, and should print");
00057   ROS_WARN("This is a warn statement, and should print");
00058   ROS_ERROR("This is an error statement, and should print");
00059   ROS_FATAL("This is a fatal statement, and should print");
00060 
00061   // This should also print
00062   print(ros::console::levels::Debug, "Hello, this should print");
00063 
00064   my_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Error]);
00065   // This will STILL print, because the logger's enabled state has been cached
00066   print(ros::console::levels::Debug, "Hello, this will also print");
00067 
00068   // Calling notifyLoggerLevelsChanged() will force a reevaluation of which logging statements are enabled
00069   ros::console::notifyLoggerLevelsChanged();
00070   // Which will cause this to not print
00071   print(ros::console::levels::Debug, "Hello, this will NOT print");
00072 
00073   log4cxx::LoggerPtr ros_logger = log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME);
00074 
00075   my_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Debug]);
00076   ros_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Error]);
00077   ROS_DEBUG("This will still print, because the child logger's level overrides its ancestor loggers' levels");
00078 
00079   log4cxx::LoggerPtr test_logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME".test");
00080   test_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Error]);
00081   ROS_INFO_NAMED("test", "This will not print, because the ros.rosconsole.test logger has been set to Error verbosity");
00082   test_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Debug]);
00083   ROS_INFO_NAMED("test", "Now everything sent to the ros.rosconsole.test logger will be printed (including this)");
00084 
00085   return 0;
00086 }


rosconsole
Author(s): Josh Faust
autogenerated on Fri Aug 28 2015 12:33:01