Ask Your Question

Why does ROS use macros for logs?

asked 2016-07-12 01:33:04 -0500

Robocop87 gravatar image

This is purely an academic question, to sate my curiosity.

What were the advantages gained by using macros as the logging interface for C++, and not some ros::logError() function? Is there a reason this approach was chosen? As I understand, most people in the C++ community hate macros with a passion, so it seems odd to see them used for something that seems trivially accomplished through other means.


edit retag flag offensive close merge delete



I don't know for sure, but I guess the fact that fi the various THROTTLE versions insert a static variable at the site of invocation, and access to __LINE__ and __FILE__ constants in all of them have something to do with it.

gvdhoorn gravatar image gvdhoorn  ( 2016-07-12 02:28:37 -0500 )edit

Interesting observation! And since __LINE__ and __FILE__ are read by the preprocessor, something like an inline function wouldn't be able to use them.

Robocop87 gravatar image Robocop87  ( 2016-07-12 03:19:51 -0500 )edit

Maybe there is a workaround, but I guess that would be line and file of the inline function and not the caller.

dornhege gravatar image dornhege  ( 2016-07-12 03:45:29 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-08-11 17:46:00 -0500

tfoote gravatar image

Macros both have the ability to take advantage of the preprocessor functions like __LINE__ and __FILE__ as well as the macros can be compiled out of the code for complete optimization if the use case demands high performance.

This means that you can choose to have no runtime penalty for keeping the debug outputs in your code. See

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


Asked: 2016-07-12 01:33:04 -0500

Seen: 353 times

Last updated: Aug 11 '17