Ask Your Question

Multi-line strings with indentation as input to C++ logging macros

asked 2016-05-06 14:53:11 -0500

spmaniato gravatar image

updated 2016-05-06 14:59:15 -0500

How can I input a multi-line string to one of the C++ logging functions and have it properly ignore the white space? Here's an example:

ROS_INFO("Cost error: %d = \
        (Current: %u - Initial: %u)",
        error_cost, cost, cost_init);

What ends up being printed is a single line (desirable) but with a bunch of spaces (undesirable):

Cost error: 0 =                       (Current: 88 - Initial: 88)

Reading this ( ) I realized that the backslash takes care of the new line but not the indentation. Is there a better way? (I'd rather not define the string beforehand and then pass a variable to ROS_INFO if I can avoid it.)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-05-06 15:00:07 -0500

ahendrix gravatar image

C/C++ implicitly joins string literals that are adjacent, so you can do:

ROS_INFO("Cost error: %d = "
         "(Current: %u - Initial: %u)",
         error_cost, cost, cost_init);
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-05-06 14:53:11 -0500

Seen: 579 times

Last updated: May 06 '16