views:

288

answers:

4

Possible Duplicate:
Do I need to disable NSLog before release Application?

I wonder if someone could help me setup a number of NSLog statements so they print to console when executing in "Debug Mode" but don't print in "Release Mode". I understand I need to add something like DEBUG = 1 to the debug config in Xcode but I can't find where. Also how do I utilise this in my code?

NSLog(@"Print Always");
if(DEBUG) NSLog(@"Print only in debug");

Is there a simple way of doing this?

EDIT_001: I tried following this but the keys now seem to be only listed under "All Settings", and are presenting as nice names. The one I should be using is GCC_PREPROCESSOR_DEFINITIONS, so I needed to find "Preprocessor Macros", select edit and add DEBUG=1 alt text

When you come to use this its simply a case of adding (see below) or some marco to remove the messy #ifdef / #endif tags.

NSLog(@"You always see me?");
#ifdef DEBUG
NSLog(@"Only in DEBUG");
#endif
+2  A: 

This is a popular solution:

http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog

See comments about using either -DDEBUG=1 or DEBUG=1.

Felixyz
I looked at that, but got an error adding OTHER_CFLAGS (see above)
fuzzygoat
OK sorted that, see the edit above, also thanks for the link, it certainly helped set me on the right track.
fuzzygoat
+3  A: 

The best solution is not to use NSLog in the first place but instead rely on the debugger.

You can set breakpoints that execute debugger commands to print out anything and you can set the breakpoints to execute the debugger commands but not to stop execution. In practice this works just like NSLog.

By using the debugger to do the logging, you don't have to worry about removing the log statements.

TechZen
I can appreciate that and I do more than often use the debugger, but I do like to drop in outputs to the console whilst developing and I don't always want to have to strip all those out for release, especially as they might be useful in developing future upgrades.
fuzzygoat
+1  A: 

Please have a look at the answers of http://stackoverflow.com/questions/969130/nslog-tips-and-tricks. There are some nice macros in there that can be very useful.

Diederik Hoogenboom
+1  A: 

I use this:

-(void)debugWinLog

{

NSUserDefaults * defaultsDebug = [NSUserDefaults standardUserDefaults];
theDebugWin = [defaultsDebug boolForKey:@"logger"];

}

Which is called in the awakeFromNib. It checks the apps plist file for a 1 or 0 for the BOOL entry "logger"

The normal state if off, but when debugging you can then turn it on or off at will in terminal. with the normal defaults write. The NSlog statments look like:

if ( theDebugWin) {NSLog (@"%@", windowState );}
markhunte