C++ ROS\u信息流不打印

C++ ROS\u信息流不打印,c++,ros,C++,Ros,我试图在一个叠加的try…catch中使用ROS\u INFO\u流,但我只有顶级输出 下面是一组最简单的代码: void failure() { try { // throw std::length_error std::string("abc").substr(10); } catch (...) { ROS_ERROR_STREAM("ROS failure()");

我试图在一个叠加的try…catch中使用ROS\u INFO\u流,但我只有顶级输出

下面是一组最简单的代码:

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_ERROR_流看起来是缓冲的,但作为错误输出,它不应该是缓冲的

我正在运行ROS Groovy

当在ROS节点的某个地方调用了
ROS::shutdown()
时,中的所有宏都将停止工作

我可以想象这样的事情会发生在你身上:在一个自动调用
ros::shutdown()
函数的错误之后,可能会到达main中的catch块

如果您想保持与ROS宏提供的输出格式相同的输出格式,可以使用类似于此的简单代码,但忘记使用颜色或其他内容突出显示代码:

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

std::cout对于
ROS.*
日志语句,您必须事先显式调用
ROS::init(…)
ROS::start(…)
,或者更常见的是,调用ROS::init并初始化
ROS::NodeHandle
。后者将为您调用
ros::start


但是,当最后一个
节点handle
超出范围时,它将调用
ros::shutdown()
,在这一点之后,您将无法使用任何日志宏。

一旦进入,为输出着色其实并不是那么困难(请参阅)。当然,这是另一个是否值得付出努力的问题。
std::cout << "[ INFO] [" << ros::Time::now() << "]: main catch" << std::endl;