Mixing #define and #if

Hi everyone,

I need to write conditional debug program which inserts debug statements depending on debug levels. I have the following code. However this results in syntax error with gcc.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>

#define DEFAULT_DEBUG_LEVEL 1

#define log_inline(level,format,...)
    #if level <= DEFAULT_DEBUG_LEVEL\
        printf(format, ##__VA_ARGS__)
    #endif

int main()
{
    log_inline(1,"TEST");
}


Kindly let me know your thoughts on the code. Please let me know if you have alternatives.

It is possible to do like this:

1
2
3
#define log_inline(level,format,...)\
    if(level <= DEFAULT_DEBUG_LEVEL)\
        printf(format, ##__VA_ARGS__); 


However I do not want the stray if condition here for every log statement.

Well, the problem is, besides the fact that preprocessor directives don't nest, that the value of level is only known at run time. If you want to do it like this, you'll have no choice but to make the check every time.

Otherwise, what you could do is
1
2
3
4
5
#if CURRENT_DEBUG_LEVEL >= HIGH
#define log_inline_high(format,...) printf(format, ##__VA_ARGS__)
#else
#define log_inline_high(format,...)
#endif 
Hi helios,

Even I thought the same way but felt may be missing something. Thank you for the response.
Topic archived. No new replies allowed.