ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Use ROS_ macro to output std::vector data

asked 2015-04-03 16:40:54 -0600

alextoind gravatar image

If I'm dealing with std::vector data in C++ and I'd like to print it on the output stream (e.g. for debugging), I'm going to do something like this:

std::cout << "Data Retrieved: \n";
std::copy(data.begin(), data.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;

How to reproduce this behaviour using (for example) ROS_DEBUG_STREAM() macro? I wish to include logger level also in the output of vector data.

The only way I found to achieve this result is something as follows:

log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(ROSCONSOLE_DEFAULT_NAME);
if (logger && logger->getLevel() == log4cxx::Level::getDebug()) {
  // debugging code
  std::cout << "Data Retrieved: \n";
  std::copy(data.begin(), data.end(), std::ostream_iterator<double>(std::cout, " "));
  std::cout << std::endl;
}

In which case I need to link the proper library:

find_library(LOG4CXX_LIBRARY log4cxx)
target_link_libraries(some_target ${LOG4CXX_LIBRARY})

(I found these useful info here)

Someone knows if there is a better way? Thank you!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2015-04-05 15:26:47 -0600

tfoote gravatar image

A typical solution is to create a std::stringstream object and build the output in a for loop. If you want this to be done conditionally make a function that does this and use it inside the ROS_DEBUG_STREAM call so it will be conditionally evaluated.

Note: Your solution is does not achieve the same result, It only goes to console. The rosconsole macros can go to both console as well as rosout.

edit flag offensive delete link more

Comments

Thank you, I didn't think about that! Now works as expected.

alextoind gravatar image alextoind  ( 2015-04-06 15:11:25 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-03 16:40:54 -0600

Seen: 2,215 times

Last updated: Apr 05 '15