# Why does ROS use macros for logs?

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.

Thanks!

edit retag close merge delete

1

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.

( 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.

( 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.

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

Sort by » oldest newest most voted

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 http://wiki.ros.org/rosconsole#Compil...

more