I have a special logging function, let's call it JLog()
which I have written to keep track of logging data, but only when testing in the simulator and on my test device. I set a DEBUG preprocessor flag in my Xcode project, set only for the development mode, not in production. JLog()
uses this to know whether to output the log message. It looks sort of like this:
void JLog(NSString *message) {
#ifdef DEBUG
NSLog(@"%@", message);
#endif
}
I use this instead of wrapping all of my NSLog()
calls with #ifdef DEBUG
- it makes my code easier to read and less prone to bugs.
The problem is that even though the app doesn't print the log messages in production, it still needs to construct the messages which are passed to JLog()
- which are sometimes expensive and often uses a lot of memory. For example, if I have code like this:
NSError *error = <something>;
JLog([NSString stringWithFormat:@"Error: %@", error);
... then the app is forced to allocate an NSString, insert the NSString-ified version of error into it, and pass it to JLog()
- only to do nothing with it. This seems very wasteful to me.
What I'd like to do is, somehow, instruct Xcode to simply not compile the JLog()
code in production. That way, code such as the above example of constructing an error message which won't end up being printed to the log anyway, won't need to be called at all. How can I do this?