Simple debug and release logging

I like to use NSLog() in my apps for simple debugging and controlling application flow.

There’s a slightly better way, which heeds the DEBUG flag and won’t use NSLog() if the product is packaged for release.

In your App-Prefix.pch, add the following lines and use DLog() for “debug” logging and ALog() for “always” logging just as you would have used NSLog(). In this way, your DLog() statements won’t make it into release.

#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#   define DLog(...)
#endif

// ALog always displays output regardless of the DEBUG setting
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

There are better solutions, but this is a simple one that works.

2014   iOS
Popular