ROS_INFO_STREAM 不打印

Zer*_*ore 5 c++ ros

我正在尝试在一个复杂的 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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

ROS calling
ROS failure()
cout failure()
cout call function
Run Code Online (Sandbox Code Playgroud)

我的猜测是 ROS_ERROR_STREAM 看起来是缓冲的,但作为错误输出,它不应该是。

我正在运行 ROS Groovy

ale*_*ind 2

当在 ROS 节点中的某处调用时, rosconsole中的所有宏都会停止工作。ros::shutdown()

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

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

std::cout << "[ INFO] [" << ros::Time::now() << "]: main catch" << std::endl;
Run Code Online (Sandbox Code Playgroud)