Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid many #ifdef for logging statements

Tags:

c++

c

logging

In our application, we created our own logging system. There are several different log types, debug, error, warning, communication, performance and ..., in this logging system. There are lots of #ifdef and #endif to disable a specific log type. These #ifdef and #endif make the code hard to read.

We are thinking remove these #ifdef and #endif and have a check before the message write to the file. That means there are lots of "useless" calls to the logging system. These calls will not result in any writing activity.

Is there a better way to turn on/off a log type, without these #ifdef and #endif AND these "useless" calls?

like image 515
5YrsLaterDBA Avatar asked Oct 22 '13 17:10

5YrsLaterDBA


Video Answer


1 Answers

What about the following:

// comment out if not needed
#define ENABLE_LOG

#ifdef ENABLE_LOG
#  define LOG(x) x
#else
#  define LOG(x) (void) 0 
#endif

Later you could just call:

LOG(mylogger.call());

Updated the #else part as Dietrich Epp suggested.

like image 190
Sigroad Avatar answered Sep 30 '22 18:09

Sigroad