Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Removing NSLog calls from 'Distribution'/production builds?

Update:

More info on this here:

Is it true that one should not use NSLog() on production code?

~~~~~~~~~~~~~~~~~~~~~~~~

Situation

I have some pretty beafy NSLog calls that I use for debugging the more complex parts of my application. However, I just recently learned that these affect runtime performance!

Goal

I would like to remove my NSLog calls during any run where I am not actually performing Product > Run (aka command-R) from within Xcode - ESPECIALLY in situations when this thing is deployed on the App Store, but also when I am running the app when disconnected from Xcode (i.e. just tapping the icon while walking down the street).

Proposed Solution?

Assuming I've created a Preprocessor Macro of VIEW_DEBUG, would the following implementation effectively remove NSLog calls from executing in the cases I've described above?

    <bunch of code>

#ifdef VIEW_DEBUG
    NSLog(@"really complex logs entries");
#endif

    <even more code>

This is a difficult one for me to 'test', so I figured I would appeal to more experienced minds. :)

Xcode Settings (for reference)

xcode settings

like image 619
toblerpwn Avatar asked Dec 06 '22 12:12

toblerpwn


1 Answers

A common solution is to place the following code in your Prefix file (or you may create a dedicated class and #include it as needed):

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

Xcode already defines DEBUG for you when performing a debug build (as shown in your screenshot). VA_ARGS is a way of creating variadic macros that was introduced in C99. the do/while ensures that DebugLog has the same net syntactic effect even when it doesn't do anything — don't worry about the pointless loop, the optimiser will remove it for you.

Then you can just use DebugLog exactly as you'd use NSLog. This will do exactly what you propose with VIEW_DEBUG but without you having to copy and paste the #ifdef condition a thousand times over.

like image 159
3 revs, 2 users 97% Avatar answered Dec 28 '22 23:12

3 revs, 2 users 97%