ROS_INFO_STREAM无法打印信息

5
我将尝试在嵌套的try...catch中使用ROS_INFO_STREAM,但我只能得到顶层输出。
以下是一小段最简代码:
void failure()
{
    try
    {
      // throw std::length_error
      std::string("abc").substr(10);                    
    }
    catch (...)
    {
      ROS_ERROR_STREAM("ROS failure()");          // print OK
      std::cout << "cout failure()" << std::endl; // print OK
      throw; // re-throw the exception
    }
}


int main()
{
  try
  {
    ROS_ERROR_STREAM("ROS calling"); // print OK
    failure(); // will throw
  }
  catch (...)
  {
    ROS_ERROR_STREAM("ROS call function"); // <-- NO print
    std::cout << "cout call function" << std::endl; // print OK
  }

  return 0;
}

输出:

ROS calling
ROS failure()
cout failure()
cout call function

我的猜测是ROS_ERROR_STREAM看起来是带缓冲的,但作为错误输出它不应该是这样的。我正在运行ROS Groovy。
2个回答

2

当在ROS节点的某个位置调用了ros::shutdown()函数时,rosconsole中的所有宏都将停止工作。

我可以想象发生了这样的事情:主函数中的catch块可能在错误后被触发,自动调用ros::shutdown()函数。

如果您想保持与ROS宏提供的相同的输出格式,您可以使用类似于以下简单代码,但不能使用颜色或其他东西来突出显示代码:

std::cout << "[ INFO] [" << ros::Time::now() << "]: main catch" << std::endl;

1
着色输出实际上并不难,一旦你进入了它(参见这个答案)。当然,问题是是否值得这样做。 - luator

0
为了让ROS_*日志语句正常工作,必须显式地调用ros::init(...)ros::start(...),或者更常见的是调用ros::init并初始化一个ros::NodeHandle。后者将为您调用ros::start
然而,当最后一个NodeHandle超出作用域时,它将调用ros::shutdown(),在此之后,您将无法使用任何日志记录宏。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接