Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is DEBUG defined in the code?

Tags:

c++

I looked at a sample code to create a log system on my server... And I found this

#if DEBUG
printf("something here");
#endif

I know what it does. It prtins something only if DEBUG has been defiend. But where is DEBUG defined? I looked at the all the header files but I could't find DEBUG..

Also, Could you please provide me a good example or tutorial about designing logging system?

Thanks in advance..

like image 338
user800799 Avatar asked Jun 24 '11 08:06

user800799


1 Answers

Do not use the DEBUG macro it is not defined by C++ standard. C++ Standard defines NDEBUG (No DEBUG), which is used for the standard assert macro and your logging code would go hand in hand with it. DEBUG is compiler dependent. Therefore NDEBUG is ensured by standard to be properly set. Applying it to your example use:

#ifndef NDEBUG
printf("something here");
#endif

But my opinion is: you should not design a logging library around the NDEBUG/DEBUG pair. Logging lib should always be there, just to allow you trace the application's behavior without the need of code recompilation, which in turn involves new deployment of your code and possibility to postpone the bug prone behavior. The following DDJ article by Petru Marginean about design of logging libraries describes how to ensure that fact in a very efficient manner in C++:

Part 1: http://drdobbs.com/cpp/201804215

Part 2: http://drdobbs.com/cpp/221900468

Regarding the logging library take a look at the Boost Logging library at:

http://boost-log.sourceforge.net/libs/log/doc/html/index.html


I was downvoted because NDEBUG is said not to be set without explicit definition of it in the command line. I agree with that fact, but on the other hand here I understand this question so, that compilation in debug mode, should also produce logging output. This fact is going to be better enforced when bundling the behavior to NDEBUG presence.

like image 87
ovanes Avatar answered Sep 22 '22 02:09

ovanes