ROS in the log (log) message

Learn to use the log (log) system, ROS doctor do large-scale projects
0

By displaying the running state of the process is good practice, but doing so does not need to determine the impact on operating efficiency and output software of clarity. ROS function log (log) system is to make the process generate some log messages displayed on the screen, sent to a particular topic or a specific log stored in the document, to facilitate debugging, logging, alarm and so on. The following briefly describes how to generate and view log messages.

Log Messages

In ROS, there is a special topic called / rosout, all log messages it carries all nodes. / Rosout message type is rosgraph_msgs / Log:

1

rosgraph_msgs / Log message is used to allow each node release log messages, so to be able to let anyone on the network can see. May be considered / rosout is an enhanced version of the print (): he is not output to the terminal string, strings and metadata into a message may be sent to any person on the network. ROS nodes should publish log messages to / rosout, so these messages can be seen by everyone. rospy client provides several functions to publish rosgraph_msgs / Log message:

1
2
if battery_voltage < 11.0:
rospy.logwarn('Battery voltage low: %f'%(battery_voltage))

rospy.logwarn () function to achieve three things, please:

  1. A formatted string to the output terminal
  2. Outputting a warning to the log in more detail in the document, the document is generally ~ / .ros / log in
  3. Construct and publish a message to / rosout topics, including warnings, and wherein the metadata node

Log level

ROS has five logging standard level, these names are part of the function of the output information, they follow the following syntax:
ROS_<LEVEL>[_<OTHER>]
each message level for different purposes:

  • DEBUG(调试):只在调试时用,此消息不出现在部署的应用中,仅用于测试。
  • INFO(信息):标准消息,说明重要步骤或节点所正在执行的操作。
  • WARN(警告):提醒一些错误,缺失或者不正常,但进程仍能运行。
  • ERROR(错误):提示错误,尽管节点仍可在这里恢复,但对节点的行为设置了一定期望。
  • FATAR(致命):这些消息通常表示阻止节点继续运行的错误。

2

生成基本的日志消息

由五个 C++ 宏来产生日志消息,每个宏对应一个级别:

1
2
3
4
5
ROS_DEBUG_STREAM(message);  
ROS_INFO_STREAM(message);
ROS_WARN_STREAM(message);
ROS_ERROR_STREAM(message);
ROS_FATAL_STREAM(message);

编写如下 C++ 进程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

int (int argc,char **argv)
{
ros::init(argc,argv,"count_and_log");
ros::NodeHandle nh;
ros::Rate.rate(10);
for(int i=1;ros::ok();i++){
ROS_DEBUG_STREAM("Counted?to?"<<i);
if((i%3)==0){
ROS_INFO_STREAM(I<<"?is?divisible?by?3.");
}
if((i%5)==0){
ROS_INFO_STREAM(I<<大专栏  ROS中的日志(log)消息"string">"?is?divisible?by?5.");
}
if((i%10)==0){
ROS_INFO_STREAM(I<<"?is?divisible?by?10.");
}
if((i%20)==0){
ROS_INFO_STREAM(I<<"?is?divisible?by?20.");
}
rate.sleep();
}
}

编译、执行之后结果如下:

3

生成一次性日志消息

ROS 提供了可以仅仅生成一次日志消息的宏:

1
2
3
4
5
ROS_DEBUG_STREAM_ONCE(message);  
ROS_INFO_STREAM_ONCE (message);
ROS_WARN_STREAM_ONCE (message);
ROS_ERROR_STREAM_ONCE (message);
ROS_FATAL_STREAM_ONCE (message);

将上述 C++ 进程中的 log 命令替换一下,得到如下的执行结果:

4

可以看到每个日志只生成了一次。

生成频率受控的日志消息

1
2
3
4
5
ROS_DEBUG_STREAM_THROTTLE(interval, message);  
ROS_INFO_STREAM_THROTTLE(interval, message);
ROS_WARN_STREAM_THROTTLE(interval, message);
ROS_ERROR_STREAM_THROTTLE(interval, messge);
ROS_FATAL_STREAM_THROTTLE(interval, message);

参数 interval 是 double 型,表示相邻日志消息出现的最小时间间隔,以秒为单位。得到如下的执行结果:

5

查看日志消息

日志消息有三个不同的输出目的地,包括屏幕、rosout topic、log 文档。其中发布到 rosout topic 的 msg 类型是 rosgraph_msgs/Log。除了 topic echo,还可以通过 rqt_console 查看日志消息:

2

启用和禁用日志消息

ROS 默认只处理 INFO 或者更高级别消息,DEBUG 级别的消息会被忽略。可以通过命令行设置显示的日志级别:
rosservice call /node-name/set_logger_level package-name level

其中:

  • set_logger_level服务由各个节点自动提供;
  • node-name 期望设置日志级别的节点名称;
  • package-name 拥有这个节点的 package 名称;
  • level 是五个级别中的一个。

另外也可以通过图形接口设置日志级别:
rqt_logger_level

6

图中列出了节点列表、日志记录器列表、日志级别列表。在图中操作与 rosservice 命令的效果一致。
另外,也可以在 C++ 进程中设置日志级别。ROS node 改变自身日志级别最直接的方式是使用 log4cxx 提供的接口:

1
2
3
4
#include <log4cxx/logger.h>
log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME)->setLevel(
ros::console::g_level_lookup[ros::console::levels::Debug]);
ros::console::notifyLoggerLevelsChanged();

Debug which can be replaced with Info, Warn, Error, Fatal.

postscript

ROS for debugging large projects must use to log system, all mature frameworks provide a process of code debugging tools for developers to learn these tools can help us avoid detours largely save time, so we have to be able to take advantage of these aids as right-hand man during development, achieve a multiplier effect.

Guess you like

Origin www.cnblogs.com/lijianming180/p/12014217.html
log
log