Thursday, June 28, 2012

Handle NSLog before release

Go to your Build settings and under the Debug configuration add a value to "Preprocessor Macros" value like:      DEBUG_MODE=1
Make sure you only do this for the Debug configuration and not for Beta or Release versions. Then in a common header file you can do something like:



#ifdef DEBUG_MODE
#define DLog( s, ... ) NSLog( @"<%@:(%d)> %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DLog( s, ... )
#endif



Now instead of NSLog use DLog everywhere. When testing and debugging, you'll get debug messages. When you're ready to release a beta or final release, all those DLog lines automatically become empty and nothing gets emitted. This way there's no manual setting of variables or commenting of NSLogs required. Picking your build target takes care of it.