example.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 ********************************************************************/
34 
35 #include "ros/console.h"
36 #include <log4cxx/logger.h>
37 
38 void print(ros::console::Level level, const std::string& s)
39 {
40  ROS_LOG(level, ROSCONSOLE_DEFAULT_NAME, "%s", s.c_str());
41 }
42 
43 int main(int /*argc*/, char** /*argv*/)
44 {
45  // This needs to happen before we start fooling around with logger levels. Otherwise the level we set may be overwritten by
46  // a configuration file
48 
49  log4cxx::LoggerPtr my_logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
50 
51  // Set the logger for this package to output all statements
53 
54  // All these should print
55  ROS_DEBUG("This is a debug statement, and should print");
56  ROS_INFO("This is an info statement, and should print");
57  ROS_WARN("This is a warn statement, and should print");
58  ROS_ERROR("This is an error statement, and should print");
59  ROS_FATAL("This is a fatal statement, and should print");
60 
61  // This should also print
62  print(ros::console::levels::Debug, "Hello, this should print");
63 
65  // This will STILL print, because the logger's enabled state has been cached
66  print(ros::console::levels::Debug, "Hello, this will also print");
67 
68  // Calling notifyLoggerLevelsChanged() will force a reevaluation of which logging statements are enabled
70  // Which will cause this to not print
71  print(ros::console::levels::Debug, "Hello, this will NOT print");
72 
73  log4cxx::LoggerPtr ros_logger = log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME);
74 
75  my_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Debug]);
76  ros_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Error]);
77  ROS_DEBUG("This will still print, because the child logger's level overrides its ancestor loggers' levels");
78 
79  log4cxx::LoggerPtr test_logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME".test");
80  test_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Error]);
81  ROS_INFO_NAMED("test", "This will not print, because the ros.rosconsole.test logger has been set to Error verbosity");
82  test_logger->setLevel(ros::console::g_level_lookup[ros::console::levels::Debug]);
83  ROS_INFO_NAMED("test", "Now everything sent to the ros.rosconsole.test logger will be printed (including this)");
84 
85  return 0;
86 }
log4cxx::LevelPtr g_level_lookup[levels::Count]
#define ROS_FATAL(...)
#define ROS_INFO_NAMED(name,...)
ROSCONSOLE_DECL void notifyLoggerLevelsChanged()
Tells the system that a logger&#39;s level has changed.
Definition: rosconsole.cpp:665
#define ROSCONSOLE_AUTOINIT
Initializes the rosconsole library. Usually unnecessary to call directly.
Definition: console.h:324
int main(int, char **)
Definition: example.cpp:43
#define ROS_WARN(...)
#define ROSCONSOLE_ROOT_LOGGER_NAME
Definition: console.h:299
#define ROS_INFO(...)
#define ROS_LOG(level, name,...)
Log to a given named logger at a given verbosity level, with printf-style formatting.
Definition: console.h:572
#define ROS_ERROR(...)
#define ROSCONSOLE_DEFAULT_NAME
Definition: console.h:301
void print(ros::console::Level level, const std::string &s)
Definition: example.cpp:38
#define ROS_DEBUG(...)


rosconsole
Author(s): Josh Faust
autogenerated on Mon Feb 28 2022 23:30:41