Do not use a DEBUG definition that is not defined by the C ++ standard. The C ++ standard defines NDEBUG (without DEBUG), which is used for the standard assert macro, and your logging code will go hand in hand with it. DEBUG is compiler dependent. Therefore, NDEBUG provided with the standard for proper installation. Applying it to your example, use:
#ifndef NDEBUG printf("something here"); #endif
But my opinion is this: you should not create a log library around the NDEBUG / DEBUG pair. The logging lib should always be there, only so that you can track the behavior of the application without having to recompile the code, which in turn implies a new deployment of your code and the ability to defer error-prone behavior. The following DDJ article by Petru Marginean on the design of logging libraries describes how to provide this fact in a very efficient way in C ++:
Part 1: http://drdobbs.com/cpp/201804215
Part 2: http://drdobbs.com/cpp/221900468
For the log library, see the Boost Logging library at:
http://boost-log.sourceforge.net/libs/log/doc/html/index.html
I was blocked because NDEBUG is said to not install without explicitly defining it on the command line. I agree with this fact, but on the other hand, I understand this question here, so compiling in debug mode should also generate log output. This fact will be better applied when associating behavior with the presence of NDEBUG .
source share