7

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.

harshalb
  • 6,012
  • 13
  • 56
  • 92
  • 1
    See http://stackoverflow.com/questions/300673/is-it-true-that-one-should-not-use-nslog-on-production-code – Vladimir Feb 05 '10 at 14:19

7 Answers7

9

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
  • 1,842
  • 3
  • 17
  • 26
6

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
  • 58,279
  • 31
  • 157
  • 188
3

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

fraca7
  • 1,178
  • 5
  • 11
2

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
  • 38,189
  • 13
  • 98
  • 110
  • 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 Feb 05 '10 at 14:50
  • 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 Lamouroux Feb 05 '10 at 15:01
  • @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 Feb 05 '10 at 15:55
  • The macro takes about 2 seconds to write - so simple. You shouldn't have debug logging in production code. – Hunter Feb 05 '10 at 18:50
  • NSLogs can crash your app (ie accessing arguments in nil objects). Adding the precompiler directives takes two seconds as stated. It is not a must and I admit I've released apps to the store with NSLogs in it - but that was when I was young and stupid. Protect yourselves ;-) – Jonny Nov 16 '10 at 02:08
2

XCode4 offers a pretty neat solution to this - use breakpoints. select action 'log a message' and set val = @val@ as the message, then tick the 'automatically continue after evaluating actions' box.

Won't fit every use case, but it gives you the benefit of easy 'inspecting' of values and guarantees they won't make it into a release.

Rasputnik
  • 313
  • 2
  • 4
1

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
  • 2,214
  • 3
  • 28
  • 45
0

Its not a problem of using NSLog in Release. But if you put any valuable data in log other user(developer) can see that data. Like some download link etc..

Naveen Shan
  • 9,192
  • 3
  • 29
  • 43