0
$\begingroup$

Rosanswers logo

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!


Originally posted by alextoind on ROS Answers with karma: 217 on 2015-04-03

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by tfoote with karma: 58457 on 2015-04-05

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by alextoind on 2015-04-06:
Thank you, I didn't think about that! Now works as expected.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.