Tuesday, June 5, 2012

iOS: Switch Off NSLog Easily

I've tried doing this by implementing a new logging method but for some reason it just didn't work.

In the end, the only solution that worked (and was easiest to implement) is to use a macro to define a function e.g. DLog, and then using DLog throughout the app.

In this way, we just need to change the value of a pre-processor flag to switch NSLog on or off.

Example taken from one of the Stack Overflow answers:

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


References:

No comments:

Post a Comment