Removing NSLogs on Release Builds
Removing NSLogs on Release Builds
Removing NSLogs on Release Builds
Wednesday, 25 March 2009
Objective-C developers frequently use NSLog to output strings to the console, particularly for debugging purposes. However, when you build a release version you don’t usually want any console output. This is especially true if you are building iPhone applications, where console output is more costly than on desktop OS X and can slow your app down. I’ve seen a few versions of tricks to help you remove NSLog output on release builds (for example, this one), but they all have two problems in common. Firstly, in most you have to edit your project settings and manually define a DEBUG constant. I find this rather annoying, as it just becomes a hassle to remember to go in and edit the project settings every time you start a new application. Secondly, they normally result in defining an alternative output method, such as DLog instead of NSLog, so you have to remember to start using the new version instead, and would have to go back and replace NSLogs in all your old code.
I came up with this small amendment that you put in your app’s prefix header, which I believe is a little neater than the other solutions I’ve seen...
#ifndef __OPTIMIZE__
# define NSLog(...) NSLog(__VA_ARGS__)
#else
# define NSLog(...) {}
#endif
As __OPTIMIZE__ is normally defined for release builds, you no longer have to go into your project settings and add any new definitions every time you create a new app. Furthermore, by overriding NSLog rather than providing an alternative definition, you can keep using NSLog normally instead of having to use an alternatively named alias. Therefore, I think this is the best solution, as all you have to do is copy and paste the above into your prefix header.