#define LOG_MSG (...) for debugging

How does the following code work?

#define ENABLE_DEBUG 1

#if ENABLE_DEBUG
    #define LOG_MSG printf
#else
    #define LOG_MSG(...)
#endif
+5
source share
2 answers

Depending on the value ENABLE_DEBUG, it is LOG_MSGeither defined as an alias for printf(), or defined as a no-op macro. It is understood that you can change the value to 0to disable debugging. This is a common technique, making it easy to switch between debug builds that display a lot of output and release builds that are silent.

#define LOG_MSG printf

This makes it an alias for printf().

#define LOG_MSG(...)    /* empty */

. , , , . , , . ... , . C99, C.

LOG_MSG("file not found\n");

LOG_MSG() , , .

// If ENABLE_DEBUG is non-zero, a debugging printout:
printf("file not found\n");

// If ENABLE_DEBUG is zero, an empty statement:
;

, , , , , ... ( / ), stderr stdout:

#define LOG_MSG(...) fprintf(stderr, __VA_ARGS__)
+8

. ENABLE_DEBUG 1, ,

LOG_MSG("something happened");

printf("something happened");

0 , ( , ).

+1

All Articles