0

In my Android programs, I frequently used :

private static final boolean D = true;

Then all of my calls to Log were prepended with if(D)

if(D)Log.w("Tag", "message");

This helped easily clean up code by setting the D value to false to remove all logging.

2 Questions: Do calls to "NSLog" in objective C have any release product overhead?

What would be the best equivalent of the if(D) logic above?

Right now I'm trying the

#ifdef macro
NSLog(@"%@",@"Some debug info");
#endif

Does this remove the code in question from the compilation unit?

Thank you!

Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • http://stackoverflow.com/questions/300673/is-it-true-that-one-should-not-use-nslog-on-production-code/302246#302246 – morningstar Oct 27 '11 at 06:07

2 Answers2

3

Yes, calls to NSLog have overhead. Every call is a function call that formats a string and writes it somewhere.

Yes, the #ifdef macro removes the NSLog call entirely, if macro is not defined.

However, it might be simpler for you to do something like this:

// Use this to enable debug logging
#define D_NSLog(...) NSLog(__VA_ARGS__)

// Use this to disable debug logging
#define D_NSLog(...) do {} while(0)

and use D_NSLog in place of NSLog.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

NSLog does not have any release product overhead. My fault/misconception here, I thought that by what he said he meant if he had to call release on it or not.

The best equivalent would probably be:

BOOL D = YES;

if (D)
{
    NSLog("Tag: %@", tag);
    NSLog("String: %@", string);
}

alernatively:

#define D YES

if (D)
{
    NSLog("Tag: %@", tag);
    NSLog("String: %@", string);
}

whichever way will work... if I understand your question correctly.

esqew
  • 42,425
  • 27
  • 92
  • 132
  • NSLog does not have overhead? Really? – mattyohe Oct 27 '11 at 02:24
  • Sometimes i have to put logging statements into very frequently iterated loops. This is why as a little performance boost, i'd like to remove the check for the boolean entirely. If I remember correctly, the java compiler upon seeing static const D = false; does not include or does not execute the code, because it can never be ran. By "release" I meant: does the NSLog even work in a release product? I thought that stuff like NSAssert was for debug only. Am I wrong here? – Alex Stone Oct 27 '11 at 18:30