Stop using NSLog.

Monday 8 February 2010, 2:15AM

And start using DebugLog!

In your _prefix file (Dayta_Prefix.pch for example), add in this piece of code:

#define DEBUG_MODE

#ifdef DEBUG_MODE

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

#else

#define DebugLog( s, ... )

#endif

It’s miles better than NSLog because it lists the file, the line number, as well as being quite easy to disable (just comment out the #define DEBUG_MODE and it’s ready to go!). No longer do you have to worry about finding and deleting all of your rogue NSLogs before you submit.

To use it: just replace any old NSLog with DebugLog. Easy!

  • Praveen
    #define DEBUG_1
    #ifdef DEBUG_1
    #define DebugLog( s, ... ) NSLog( @"<%p %@:(%d) %@> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, \
    [NSString stringWithUTF8String:__PRETTY_FUNCTION__],\
    [NSString stringWithFormat:(s), ##__VA_ARGS__] )
    #else
    #define DebugLog( s, ... )
    #endif

    This Macro will print the name of the function too........
  • seventoes
    I've been using this. CFShow is part of Core Foundation and shows debug messages in stdout, without all the garbage that NSLog adds.

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Debug
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #define DEBUG_ON // comment to hide all debug code

    #ifdef DEBUG_ON
    #define MSLog(format, ...) CFShow([[NSString stringWithFormat:@"<%@:%d> ",[[NSString stringWithUTF8String:__FILE__] lastPathComponent],__LINE__] stringByAppendingFormat:format, ## __VA_ARGS__])
    #else
    #define MSLog(format, ...)
    #endif

    #define MSLogRect(rect) MSLog(@"%s x:%.4f, y:%.4f, w:%.4f, h%.4f", #rect, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)
    #define MSLogSize(size) MSLog(@"%s w:%.4f, h:%.4f", #size, size.width, size.height)
    #define MSLogPoint(point) MSLog(@"%s x:%.4f, y:%.4f", #point, point.x, point.y)

    Plus the last couple util macros have come in handy often! MSLog functions just like NSLog now but without garbge info!
  • Wonderful tip. I agree with Antoine - keep 'em comin'!
  • im using this:


    #define DEBUG 1
    //#define RELEASE 1

    #ifdef DEBUG

    // Debug definitions
    #define DEBUG_MODE
    #define XLog(fmt, ...) NSLog(@"%s " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__)

    #else

    #ifndef RELEASE
    #error DEBUG or RELEASE need to be #defined
    #endif

    // Release definitions
    #define RELEASE_MODE
    #define XLog(...)

    #endif


    :)
  • Thanx for the tip!!! Keep those coming.
  • David
    Hi all,

    Ive tried the DebugLog but i get compile errors saying that DebugLog hasnt been defined anywhere do i have to included the pre-compiled header every where i have DebugLog??? ill i did was added the DebugLog stuff to the pre-compiled header, and renamed NSLog and tried to build.

    cheers
    David
blog comments powered by Disqus