5

I'm trying to use ROS_INFO_STREAM inside an imbricated try...catch but I only have the top-level output

Here's a minimal bunch of code:

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;
}

output:

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

My guess would be that ROS_ERROR_STREAM looks buffered but as an error output it shouldn't be.

I'm running ROS Groovy

Aracthor
  • 5,757
  • 6
  • 31
  • 59
Zermingore
  • 743
  • 2
  • 11
  • 28

2 Answers2

2

All the macros in rosconsole stop working when ros::shutdown() has been called somewhere in the ROS node.

I can imagine that something like that happens to you: the catch block in the main is probably reached after an error which calls automatically the ros::shutdown() function.

If you would like to maintain the same output format like the one provided by ROS macros, you can use a simple code like this one, but forget to get the code highlighted with colors or other stuff:

std::cout << "[ INFO] [" << ros::Time::now() << "]: main catch" << std::endl;
alextoind
  • 1,143
  • 1
  • 13
  • 20
  • 1
    Coloring the output is actually not that difficult, once you got into it (see [this answer](http://stackoverflow.com/a/2616912/2095383)). It's another question whether it is worth the effort, of course. – luator Sep 24 '15 at 09:23
0

For ROS_* logging statements to work you have to have either explicitly called ros::init(...) and ros::start(...) beforehand, or as is more common, call ros::init and initialize a ros::NodeHandle. The latter will call ros::start for you.

However, when the last NodeHandle goes out of scope it will call ros::shutdown() and after this point, again you won't be able to use any of the logging macros.

bergercookie
  • 2,542
  • 1
  • 30
  • 38