ROS学习之 cpp日志记录

来源:互联网 发布:刺客信条4优化方案 编辑:程序博客网 时间:2024/05/16 04:44
wiki链接: http://wiki.ros.org/roscpp/Overview/Logging

参考: rosconsole  wiki: http://wiki.ros.org/rosconsole
    ros有基于话题的输出消息机制,被称作rosout.可以从节点输出记录信息.
    这些记录消息是用户可读的字符串消息,显示了一个节点的状态信息.
        也可以使用rqt_console GUI应用来浏览这些信息

一.日志打印语句

    roscpp使用rosconsole程序包来提供客户端API.这些API以一系列的ROS_宏
    rosconsole提供4种不同的记录输出语句,以5种不同的冗长等级,这些都支持printf和流式格式化.

    基本版本:简单打印输出一条消息
        #include<ros/console.h>
        ROS_DEBUG(...); ROS_DEBUG_STREAM(args);
        这会输出到一个名叫ros.pkg_name的文件
    命名版本:指定输出的文件名字
        #include<ros/console.h>
        ROS_DEBUG_NAMED("m_name",...); ROS_DEBUG_STREAM_NAMED("m_name",args);
        这会输出到一个名叫ros.pkg_name.m_name的文件
    条件版本:指定输出的条件,只有当条件true时才会打印.
        #include <ros/console.h>
        ROS_DEBUG_COND(x < 0, "Uh oh, x = %d, this is bad", x);
        ROS_DEBUG_STREAM_COND(x < 0, "Uh oh, x = " << x << ", this is bad");

    条件加命名版本:上面的组合
        #include <ros/console.h>
        ROS_DEBUG_COND_NAMED(x < 0, "test_only", "Uh oh, x = %d, this is bad", x);
        ROS_DEBUG_STREAM_COND_NAMED(x < 0, "test_only", "Uh oh, x = " << x << ", this is bad");
    一次打印版本: 只打印一次
        #include <ros/console.h>
        for (int i = 0; i < 10; ++i)
        {
            ROS_DEBUG_ONCE("This message will only print once");
        }
    Throttle版本:周期打印,以period参数为周期
        while (true)
        {
            ROS_DEBUG_THROTTLE(60, "This message will print every 60 seconds");
        }

    延时Throttle版本:周期打印,以period参数为周期且延迟一个周期,即地一个周期什么也不打印
        while (!ros::service::waitForService("add_two_ints", ros::Duration(0.1)) && ros::ok())
        {
          // This message will print every 10 seconds.
          // The macro will have no effect the first 10 seconds.
          // In other words, if the service is not available, the message will be
          // printed at times 10, 20, 30, ...
          ROS_DEBUG_DELAYED_THROTTLE(10, "Waiting for service 'add_two_ints'");
        }

    过滤版本:以ros::console::FilterBase类对象指针作为参数,满足过滤器的才打印
        ROS_DEBUG_FILTER(filter, ...)

        ROS_DEBUG_STREAM_FILTER(filter, args)

        ROS_DEBUG_FILTER_NAMED(filter, name, ...)

        ROS_DEBUG_STREAM_FILTER_NAMED(filter, name, args)

    5种不同的冗长等级: DEBUG,INFO,WARN.ERROR,FATAL.默认的是INFO级.

二.输出

    记录消息有4个默认的终点,取决于冗长等级.
    1. stdout DEBUG和INFO消息会到达stdout. roslaunch/XML/node标签的output="log|screen"(optional)
    2. stderr WARN,ERROR,FATAL消息将到达stderr
    3. 节点记录文件 ~/.ros/log目录下 使用roslaunch-logs查看当前的记录文件目录
    4. /rosout 话题

                           Debug    Info    Warn    Error    Fatal
    stdour                 v            v         -            -            -
    stderr                   -            -         v           v            v
    log file                v             v        v           v           v
    /rosout                v             v        v           v           v

    这个表格只适用于roscpp.
0 0