tags:

views:

735

answers:

5

Hello all ,

I am just about to submit my first iPhone app.

I have put lots of NSLog statements to test and debug the application .

So my question is " Is it ok to keep the NSLog statements in the source code "

and I have a doubt that whether the nslog statements slows down the total execution time or not .

thanks.

+1  A: 

The three apps I have on the AppStore are loaded with NSLogs so I guess it's okay...

fraca7
+1  A: 

I've never got rid of mine :)

Some people recommend conditional compiling / compiler macros so you can make a build without any NSLog at all but I think that's too much effort for very little reward.

However, NSLogs do slow the app down (slightly) so I'd remove any ones that are directly affecting performance. Also, you might want to remove some debugging logging information to hide how you have implemented your app etc.

Hope that's helpful,

Sam

deanWombourne
Good answer but I'm not sure about the "I think that's too much effort for very little reward." Once you have done this once it's not that hard to implement again (takes 5 minutes at most) when you set up a new project. I personally have a DebugLog defined that is conditionally compiled away to nothing for release builds, leaving me NSLog to use for really important stuff that I would like to show up in logs.
Kevin
Depending on the number and content of NSLogs the slow down can be considerably. If you log the description of each object in a table you will likely witness poor scroll performance.
Felix
@KevinBlair - Very true but I'd have to write the macro and then the team I'm on would all need to adopt the macro - I'm taking the path of least resistance :) (It would be nice if there was a pre-defined macro built into every new project)
deanWombourne
The macro takes about 2 seconds to write - so simple. You shouldn't have debug logging in production code.
Hunter
+3  A: 

An app I was working on perviously had many NSLogs dumping debug to the console. We found that they hindered performance drastically on the iPhone 3G. The performance hit was noticeable on a 3GS, but not as much.

My advice would be to use a preprocessor macro and a debug preprocessor definition to conditionally use NSLogs in debug builds but not in release builds.

Jasarien
+2  A: 

I used this all the time (found it somewhere):

// DLog is almost a drop-in replacement for NSLog to turn off logging for release build
// 
// add -DDEBUG to OTHER_CFLAGS in the build user defined settings
//
// Usage:
//
// DLog();
// DLog(@"here");
// DLog(@"value: %d", x);
// Unfortunately this doesn't work DLog(aStringVariable); you have to do this instead DLog(@"%@", aStringVariable);
//

#ifdef DEBUG
#   define DLog(__FORMAT__, ...) NSLog((@"%s [Line %d] " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#   define DLog(...) do {} while (0)
#endif
// ALog always displays output regardless of the DEBUG setting
#define ALog(__FORMAT__, ...) NSLog((@"%s [Line %d] " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

The three20 project from Facebook has more complex way to debug or logging.

Jesse Armand
can you tell where you have found it so I can better understand it
hib
I'm sorry. I forgot where it's.But, the source code is descriptive enough. You can google it. If I could found it, you could.
Jesse Armand
It's from this blog - http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog
deanWombourne
A: 

I simply use a "useDebugMode" singleton int set to 0 or 1 that is stored in userPrefs (plist). And, depending on the UUID of the iphone, I add a "Use Debug Mode: on/off" toggle to the settings screen so i can debug problems in the future with production versions. This allows me to debug data problems (with downloaded XML) if people complain about issues with the program in the future. I then wrap all "NSLog"s and "printf"s in

if (useDebugLog == 1)
{
     NSLog(@"debug statement");
}

This works for me...Of course, the:

 if (useDebugLog == 1)

causes some overhead that I am willing to deal with.

Jann