Search code examples
objective-ccocoanslog

Enable and Disable NSLog in DEBUG mode


I want to enable NSLog when I am in debug and disable it otherwise. A very simple thing is:

#ifdef DEBUG
NSLog(@"My log");
#endif

But all this #ifdef and #endif is borring... :( So I try other thing: (.pch is good place to put it)

#ifdef DEBUG
#   define NSLog(text) NSLog(text);
#else 
#   define NSLog(text) 
#endif

This work very fine (isn't recursive). But the problem is that NSLog have infinite arguments.

void NSLog(NSString *format, ...)

How I solve this to work in preprocessor mode?

-- Edit --

This code make your NSLog better:

#ifdef DEBUG
    #define NSLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
    #define NSLog(...)
#endif

Solution

  • This should do the trick:

     #ifdef DEBUG
     #   define NSLog(...) NSLog(__VA_ARGS__)
     #else 
     #   define NSLog(...) (void)0
     #endif