dNSLog – simple debug only NSLog

Recently read a blog post by Carbon 5 on using Xcode 4 logging breakpoints to do logging.  Thought I’d post what I’ve used for over a decade.  It’s dead simple; no logging levels, no extra output on each log statement (makes log lines too long in my experience and adds noise).  Anyway, I have fancier versions for more control/configurability, but this is the bare-bones simplest version for quick prototyping.  I use it for iOS and Mac OS X.

I define DEBUG=1 in my project preprocessor macros, only for the Debug configuration.

Then I define a macro dNSLog:

#if defined(DEBUG) && (DEBUG == 1)
  #define dNSLog( a, var_args1... ) NSLog( a, ## var_args1 )
  #define dNSLog( a, var_args...)

// this next one is so you can have an else statement for your if statements to tell you when the if fails, in debug only.

#if defined(DEBUG) && (DEBUG == 1)
  #define dElseNSLog( a, var_args1...) else NSLog( a, ## var_args1 )
  #define dElseNSLog( a, var_args1...)

Then use dNSLog(@"foo is: %@", foo); for any non-release log statement. The dElseNSLogversion is useful for when you only want an else clause and log statement in debug builds. It allows you to do things like:

if ( something_unlikely_to_fail )
dElseNSLog(@"got an unexpected error!");

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: