ROS2 日志管理
日志信息对于 ROS 节点程序的开发和调试有着重要作用,ROS 2 中为我们提供了完善的日志消息接口和管理工具。本节我们将学习在 ROS 2 中如何打印日志,以及使用 rqt_console 工具对日志进行管理和可视化。
日志等级
在 ROS2 中,一共有五个日志级别,级别自高到低分别是:
级别 | 中文 | 描述 |
---|---|---|
Fatal | 致命级 | 描述系统为了自我保护即将终止的消息 |
Error | 错误级 | 描述非致命但是会阻碍程序运行的消息 |
Warn | 警告级 | 描述不损坏功能运行但是预期之外的行为的消息 |
Info | 信息级 | 描述系统正常运行时事件和状态消息 |
Debug | 调试级 | 描述系统一步一步运行的详细消息 |
ROS2 中默认开启的日志级别是 Info,会自动显示 info 级别以上的所有日志,包括 Info、Warn、Error、Fatal。
当运行 ROS 节点时,你可以实时修改日志级别,以达到动态控制日志输出的目的。例如,只显示 Warn 级别以上的日志,可 以执行如下命令:
ros2 run turtlesim turtlesim_node --ros-args --log-level WARN
此时,启动的仿真器就只会向外发布 Warn 级别以上的日志,Info 和 Debug 级别的日志都将被忽略。
C++ API 接口
常规接口
#define RCLCPP_DEBUG(logger, ...)
#define RCLCPP_INFO(logger, ...)
#define RCLCPP_WARN(logger, ...)
#define RCLCPP_ERROR(logger, ...)
#define RCLCPP_FATAL(logger, ...)
多种模式
// Log a message with severity DEBUG.
#define RCLCPP_DEBUG(logger, ...)
// Log a message with severity DEBUG with the following conditions: All subsequent log calls except the first one are being ignored.
#define RCLCPP_DEBUG_ONCE(logger, ...)
// Log a message with severity DEBUG with the following conditions: Log calls are being ignored when the expression evaluates to false.
#define RCLCPP_DEBUG_EXPRESSION(logger, expression, ...)
Log a message with severity DEBUG with the following conditions: Log calls are being ignored when the function returns false.
#define RCLCPP_DEBUG_FUNCTION(logger, function, ...)
Log a message with severity DEBUG with the following conditions: The first log call is being ignored but all subsequent calls are being processed.
#define RCLCPP_DEBUG_SKIPFIRST(logger, ...)
Log a message with severity DEBUG with the following conditions: Log calls are being ignored if the last logged message is not longer ago than the specified duration.
#define RCLCPP_DEBUG_THROTTLE(logger, clock, duration, ...)
Log a message with severity DEBUG with the following conditions: The first log call is being ignored but all subsequent calls are being processed. Log calls are being ignored if the last logged message is not longer ago than the specified duration.
#define RCLCPP_DEBUG_SKIPFIRST_THROTTLE(logger, clock, duration, ...)
Log a message with severity DEBUG.
#define RCLCPP_DEBUG_STREAM(logger, stream_arg)
Log a message with severity DEBUG with the following conditions: All subsequent log calls except the first one are being ignored.
#define RCLCPP_DEBUG_STREAM_ONCE(logger, stream_arg)
Log a message with severity DEBUG with the following conditions: Log calls are being ignored when the expression evaluates to false.
#define RCLCPP_DEBUG_STREAM_EXPRESSION(logger, expression, stream_arg)
Log a message with severity DEBUG with the following conditions: Log calls are being ignored when the function returns false.
#define RCLCPP_DEBUG_STREAM_FUNCTION(logger, function, stream_arg)
Log a message with severity DEBUG with the following conditions: The first log call is being ignored but all subsequent calls are being processed.
#define RCLCPP_DEBUG_STREAM_SKIPFIRST(logger, stream_arg)
Log a message with severity DEBUG with the following conditions: Log calls are being ignored if the last logged message is not longer ago than the specified duration.
#define RCLCPP_DEBUG_STREAM_THROTTLE(logger, clock, duration, stream_arg)
Log a message with severity DEBUG with the following conditions: The first log call is being ignored but all subsequent calls are being processed. Log calls are being ignored if the last logged message is not longer ago than the specified duration.
#define RCLCPP_DEBUG_STREAM_SKIPFIRST_THROTTLE(logger, clock, duration, stream_arg)