Search code examples
androidobjective-cloggingiphone-4

Objective-C equivalent of if(D)Log.w(TAG,"Message")


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!


Solution

  • 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.