Note To Self - Conditional Logging
Some times when developing an application it makes life easy if your code spits out information to the console using NSLog. This is known as CaveMan debugging
You don’t want end users to have to endure console Tourettes though, and commenting out the logging statements every time you build for distribution would be a little annoying. What to do?
Here’s a way to have logging statements that don’t appear in release code.
In Project Builder you have different Build Styles for Targets. These Build Styles contain Build Settings that affect the how the Product (your application) is built. The Build Settings add to, or override, the Target’s Build Settings.
With every new Project in Project Builder there are two default Build Styles.
Development

and Deployment.
When you build with the Deployment style the final product will be smaller in file size, but harder to debug. This is because this build style asks the compiler to strip out debugging symbols and optimise the code.
We are going to add a new Build Setting to the Development Style:
The OTHER_CFLAGS key is described in The Project Builder Build Settings Documentation:
“This setting defines flags passed to the compiler for all C and ObjectiveC compiles. This flag can be used cases where there is no other build setting for a particular feature.”
The Value for this build setting: -D_DEBUG_ is the flag we are passing to the compiler (GCC)
man gcc tells us that the -D switch defines a macro with a value of 1. The macro name we have chosen is _DEBUG_
So what the build setting does is define a macro so that whenever _DEBUG_ is found in the source the pre-processor will substitute 1. (And of course this only applies to the Development Build Style.)
Now we can put statements like this into our code:
#if _DEBUG_
NSLog(@”awakingFromNib”);
#endif
So - if we are building with the Development Style - the Logging line gets compiled in also.
Cool.
Comments are closed!