tags:

views:

390

answers:

3

For some reason, during my development cycle I find myself deleting NSLog statements that I had inserted to aid in debugging. I don't really know why I have this habit, I just do it.

On occasion, during development I'll find that I'll run into a problem I've had before, and then wind up re-adding an old NSLog statement. And then deleting it again later.

Is there any good reason to delete NSLog statements? From my experience leaving one or two in hasn't caused any app rejection. And since, to my knowledge, they don't log anything anywhere when an app is in distribution (please correct me if I am wrong on this), it doesn't seem like they are harming anything. Is there a performance hit that I should be worried about?

+2  A: 

There's a (imo) nice post about that with a possible solution included.

Vladimir
+9  A: 

What I do is add a macro that does the logging only when I am in Debug mode. Put this in your <APP_NAME>_Prefix.pch file

#ifdef DEBUG
#define DebugLog( s, ... ) NSLog( @"<%p %@:%d (%@)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__,  NSStringFromSelector(_cmd), [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DebugLog( s, ... ) 
#endif

As a bonus to the regular log, you get filename, method name, and line number.

Then in the project info, add this under the debug build only. It goes in the User-defined sections under GCC_PREPROCESSOR_DEFINITIONS:

DEBUG

Then replace any of the NSLog's you have in your project with DebugLog (it takes the same args as NSLog) and you won't have to worry about releasing debug statements live.

In answer to your question, logging can slow down the performance of an app and unless you require them for help debugging in the wild, I would leave them out.

coneybeare
Thanks coney, sort of as a follow up then - when you say I can use logging for help debugging in the wild, as far as I knew NSLog didn't actually do anything in a distributed app. The only logs I can get from the wild are Apple crash reports, right? Of course, I am asking about getting logging information without having to roll my own code to send logfiles back to me, or something.
bpapa
NSLog statements get printed to the console. If you have a nasty bug, and a cooperative user or user/developer, then retrieving contents from the log is possible. Apple, by default, does not collect console contents for you.
coneybeare
A: 

If you have a large amount of logs, you application could be more slowly. Log to console consume some processing.

Completing the answers give, here one big collection of debug constants that we use. Enjoy.

// Uncomment the defitions to show additional info.

//  #define DEBUG

//  #define DEBUGWHERE_SHOWFULLINFO

//  #define DEBUG_SHOWLINES
//  #define DEBUG_SHOWFULLPATH
//  #define DEBUG_SHOWSEPARATORS
//  #define DEBUG_SHOWFULLINFO


// Definition of DEBUG functions. Only work if DEBUG is defined.
#ifdef DEBUG 

    #define debug_separator() NSLog( @"────────────────────────────────────────────────────────────────────────────" );

    #ifdef DEBUG_SHOWSEPARATORS
     #define debug_showSeparators() debug_separator();
    #else
     #define debug_showSeparators()
    #endif

    /// /// /// ////// ///// 

    #ifdef DEBUG_SHOWFULLPATH
     #define debug_whereFull() debug_showSeparators(); NSLog(@"Line:%d : %s : %s", __LINE__,__FILE__,__FUNCTION__); debug_showSeparators(); 
    #else
     #define debug_whereFull() debug_showSeparators(); NSLog(@"Line:%d : %s : %s", __LINE__,[ [ [ [NSString alloc] initWithBytes:__FILE__ length:strlen(__FILE__) encoding:NSUTF8StringEncoding] lastPathComponent] UTF8String ] ,__FUNCTION__); debug_showSeparators(); 
    #endif

    /// /// /// ////// ///// 

    #define debugExt(args,...) debug_separator(); debug_whereFull(); NSLog( args, ##__VA_ARGS__); debug_separator();

    /// /// /// ////// ///// Debug Print Macros

    #ifdef DEBUG_SHOWFULLINFO
     #define debug(args,...) debugExt(args, ##__VA_ARGS__);
    #else
     #ifdef DEBUG_SHOWLINES
      #define debug(args,...) debug_showSeparators(); NSLog([ NSString stringWithFormat:@"Line:%d : %@", __LINE__, args ], ##__VA_ARGS__); debug_showSeparators();
     #else
      #define debug(args,...) debug_showSeparators(); NSLog(args, ##__VA_ARGS__); debug_showSeparators();
     #endif
    #endif

    /// /// /// ////// ///// Debug Specific Types

    #define debug_object( arg ) debug( @"Object: %@", arg );
    #define debug_int( arg ) debug( @"integer: %i", arg );
    #define debug_float( arg ) debug( @"float: %f", arg );
    #define debug_rect( arg ) debug( @"CGRect ( %f, %f, %f, %f)", arg.origin.x, arg.origin.y, arg.size.width, arg.size.height );
    #define debug_point( arg ) debug( @"CGPoint ( %f, %f )", arg.x, arg.y );
    #define debug_bool( arg )  debug( @"Boolean: %@", ( arg == YES ? @"YES" : @"NO" ) );

    /// /// /// ////// ///// Debug Where Macros

    #ifdef DEBUGWHERE_SHOWFULLINFO
     #define debug_where() debug_whereFull(); 
    #else
     #define debug_where() debug(@"%s",__FUNCTION__); 
    #endif

    #define debug_where_separators() debug_separator(); debug_where(); debug_separator();

    /// /// /// ////// /////

#else
    #define debug(args,...) 
    #define debug_separator()  
    #define debug_where()   
    #define debug_where_separators()  
    #define debug_whereFull()   
    #define debugExt(args,...)
    #define debug_object( arg ) 
    #define debug_int( arg ) 
    #define debug_rect( arg )  
    #define debug_bool( arg )  
    #define debug_point( arg )
    #define debug_float( arg )
#endif
SEQOY Development Team