2

I am using Visual Studio Code v1.14.1.

In my program, I got a lot's of printf or some debug function.

I knew that I can put something like

#define DEBUG_PRINT  1

#ifdef DEBUG_PRINT
    printf(*****************);
#endif 

But the problem is there got thousand of printf, I don't want manually add the preprocessor define. Do we got any method can automatically wrapped the target function around.

JD_Newbie
  • 53
  • 2

1 Answers1

2

Assuming you want every call to printf to be covered by the debug flag, you can do this:

#ifdef DEBUG_PRINT
#define printf(...) printf(__VA_ARGS__)
#else
#define printf(...) 0
#endif

With this macro, if the flag is defined then the call to printf is unchanged. If it is not, the call is replaced with the value 0. The value is needed in case the return value of printf is used anyplace.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Is it workable for every function ? Example I got another function like print_log(), the same method will do ? – JD_Newbie Dec 24 '19 at 05:40
  • @JD_Newbie Yes it will work. The only thing you would need to change is that the value in the non-debug case needs to match the return type of the function. If the function's return type is `void`, make that case `(void)0`. – dbush Dec 24 '19 at 05:43