开发者

Suppressing NSLog statements for release? [duplicate]

开发者 https://www.devze.com 2022-12-26 07:07 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Do I need to disable NSLog before release Application?
This question already has answers here: Closed 12 years ago.

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 Xco开发者_StackOverflow社区de 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

Suppressing NSLog statements for release? [duplicate]

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


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.


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.


Please have a look at the answers of How to print out the method name and line number and conditionally disable NSLog?. There are some nice macros in there that can be very useful.


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 );}
0

精彩评论

暂无评论...
验证码 换一张
取 消