[ROS Q&A] 141 – How to Modify Logger Level in ROS (C++)
In this video we are going to see how to modify the Logger Level in ROS using C++.
This is a video trying to answer the following question posted at the ROS answers forum: https://answers.ros.org/question/98521/problems-with-logger-levels/
RELATED LINKS
▸ Original question: https://answers.ros.org/question/98521/problems-with-logger-levels/
▸ ROS Development Studio (RDS)
▸ Robot Ignite Academy
Step 1. Create a project in ROS Development Studio(ROSDS)
ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here.
Step 2. Create package and file
Let’s create a package for testing in RDS by typing the following command.
cd ~/catkin_ws/src catkin_create_pkg logs_test roscpp
Then we create a source file under the logs_test/src directory called logs.cpp with the following content.
#include <ros/ros.h> #include <ros/console.h> #include <stdlib.h> int main(int argc, char** argv) { if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug)) { ros::console::notifyLoggerLevelsChanged(); } ros::init(argc,argv,"log_demo"); ros::NodeHandle nh; ros::Rate loop_rate(0.5); // We create a Rate object of 2Hz while (ros::ok()) //Endless loop until Ctrl+c { ROS_DEBUG("This is a DEBUG message"); ROS_INFO("This is a INFO message"); ROS_WARN("This is a WARN message"); ROS_ERROR("This is a ERROR message"); ROS_FATAL("This is a FATAL message"); loop_rate.sleep(); ros::spinOnce(); } return 0; }
In the CMakeLists.txt, please change the following content in build part.
... ## Declare a C++ executable ## With catkin_make all packages are built within a single CMake context ## The recommended prefix ensures that target names across packages don't collide add_executable(logs_test src/logs.cpp) ... ## Add cmake target dependencies of the executable ## same as for the library above add_dependencies(logs_test ${logs_test_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) ## Specify libraries to link a library or executable target against target_link_libraries(logs_test ${catkin_LIBRARIES} ) ...
Then we can compile the code with the following command
cd ~/catkin_ws catkin_make
While waiting for compilation, let’s create a launch file to launch the code much easier. Please create a launch folder under the logs_test directory and create a file called logs_test.launch under it with the following code.
<launch> <node pkg = "logs_test" type="logs_test" name="log_demo" output="screen" /> </launch>
Then we can run the following command in shell to launch the node.
roslaunch logs_test logs_test.launch
Since we set the lowest level logger in our code(DEBUG) we’ll see all 5 levels logs are printing. If you change it(e.g. to ERROR) and compile again, you should only see the ERROR and FATAL message.
Edit by: Tony Huang
Feedback
Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.