ROS代码中设置消息日志级别

59 篇文章 0 订阅

参考自ROS机器人程序设计,ch3,example1

#include <ros/ros.h>
#include <ros/console.h>

#define OVERRIDE_NODE_VERBOSITY_LEVEL 1

int main( int argc, char **argv )
{

  ros::init( argc, argv, "example1" );

#if OVERRIDE_NODE_VERBOSITY_LEVEL
  // Se the logging level manually to DEBUG
  ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug);
#endif

//上面Debug,是ros默认设置的消息日志级别,默认有Debug,Info,Warn,Error,Fatal(首字母大写)
//rosrun rqt_console rqt_console 也可见端倪
  ros::NodeHandle n;

  const double val = 3.14;

  ROS_INFO("This is a simple INFO message!")

  ROS_DEBUG( "This is a simple DEBUG message!" );

  ROS_DEBUG( "This is a DEBUG message with an argument: %f", val );

  ROS_DEBUG_STREAM(
    "This is DEBUG stream message with an argument: " << val
  );

  ros::spinOnce();

  return EXIT_SUCCESS;

}

猜猜会输出什么?

[DEBUG] [1504085545.024413724]: This is a simple DEBUG message!
[DEBUG] [1504085545.024485730]: This is a DEBUG message with an argument: 3.140000
[DEBUG] [1504085545.024514779]: This is DEBUG stream message with an argument: 3.14

只有ROS_DEBUG有输出,而ROS_INFO没有输出在屏幕。

对消息日志级别做出更改

#include <ros/ros.h>
#include <ros/console.h>

#define OVERRIDE_NODE_VERBOSITY_LEVEL 1

int main( int argc, char **argv )
{

  ros::init( argc, argv, "example1" );

#if OVERRIDE_NODE_VERBOSITY_LEVEL
  // Se the logging level manually to DEBUGXXXXXX      改成Info
  ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Info);
#endif

//上面Debug,是ros默认的消息日志级别,默认有Debug,Info,Warn,Error,Fatal(首字母大写)
//rosrun rqt_console rqt_console 也可见端倪
  ros::NodeHandle n;

  const double val = 3.14;

  ROS_INFO("This is a simple INFO message!")

  ROS_DEBUG( "This is a simple DEBUG message!" );

  ROS_DEBUG( "This is a DEBUG message with an argument: %f", val );

  ROS_DEBUG_STREAM(
    "This is DEBUG stream message with an argument: " << val
  );

  ros::spinOnce();

  return EXIT_SUCCESS;

}

屏幕输出为

[ INFO] [1504086745.030420437]: This is a simple INFO message!

仅输出ROS_INFO信息。
ROS_INFO() 和ROS_INFO_STREAM()的区别

ROS_INFO 是C –printf 的宏,带格式的输出。如
ROS_INFO(“Positon=(%.2f,%.2f)”,msg.x, msg,y)

ROS_INFO_STREAM()是C++的宏,std::stringsteam 标准字符串流。即使用<< 重载。如
ROS_INFO_STREAM(“HERE is PARKING.”<< msg.x << “—”<< msg.y);

这些代码将字符串内容发送至内部日志系统,即log4xx2(2是平方)

简单的日志消息示例

#include "ros/ros.h"


int main(int argc,char** argv)
{
    ros::init(argc,argv,"cout_and_log");
    ros::NodeHandle n;
    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_WARN_STREAM(i<<" is divisible by 5.");
        }
        if((i%10==0))
        {
        ROS_ERROR_STREAM(i<<" is divisible by 10.");
        }
    rate.sleep();
}
}

输出为:

[ INFO] [1504163381.784508564]: 4101 is divisible by 3.
[ INFO] [1504163382.084547655]: 4104 is divisible by 3.
[ WARN] [1504163382.184822529]: 4105 is divisible by 5.
[ INFO] [1504163382.384422384]: 4107 is divisible by 3.
[ INFO] [1504163382.685165595]: 4110 is divisible by 3.
[ WARN] [1504163382.685391623]: 4110 is divisible by 5.
[ERROR] [1504163382.685491278]: 4110 is divisible by 10.

重点:这是没有第一条Debug日志消息,因为ROS默认输出最小级别为Info
以不同颜色显示在控制台
INFO 白
WARN黄
ERROR红
FATAL红

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值