One way to solve this problem is to configure a macro system that makes it easy to disable printfs without having to remove them in the code. I am using something like this:
#define LOGMESSAGE(LEVEL, ...) logging_messagef(LEVEL, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); #define FATALMESSAGE(...) LOGMESSAGE(LOG_FATAL, __VA_ARGS__); #define EMERGMESSAGE(...) LOGMESSAGE(LOG_EMERG, __VA_ARGS__); #define ALERTMESSAGE(...) LOGMESSAGE(LOG_ALERT, __VA_ARGS__); #define CRITMESSAGE(...) LOGMESSAGE(LOG_CRIT, __VA_ARGS__); #define ERRMESSAGE(...) LOGMESSAGE(LOG_ERR, __VA_ARGS__); #define WARNMESSAGE(...) LOGMESSAGE(LOG_WARNING, __VA_ARGS__); #define NOTICEMESSAGE(...) LOGMESSAGE(LOG_NOTICE, __VA_ARGS__); #define INFOMESSAGE(...) LOGMESSAGE(LOG_INFO, __VA_ARGS__); #define DBGMESSAGE(...) LOGMESSAGE(LOG_DEBUG, __VA_ARGS__); #if defined(PAINFULLY_VERBOSE) # define PV_DBGMESSAGE(...) LOGMESSAGE(LOG_DEBUG, __VA_ARGS__); #else # define PV_DBGMESSAGE(...) ((void)0); #endif
logging_messagef() is a function defined in a separate .c file. Use the XMESSAGE (...) macros in the code depending on the purpose of the message. The best thing about this setting is that it works for debugging and logging at the same time, and the logging_messagef() function can be changed to perform several different actions (printf to stderr, to a log file, using syslog or another system log object, etc.), and messages below a certain level can be ignored in logging_messagef() when you do not need them. PV_DBGMESSAGE() intended for those complex debugging messages that you will definitely want to disable during production.
Tim schaeffer
source share