dNSLog – simple debug only NSLog

by

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 )
#else
  #define dNSLog( a, var_args...)
#endif

// 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 )
#else
  #define dElseNSLog( a, var_args1...)
#endif

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 )
{
  do_something_real();
}
dElseNSLog(@"got an unexpected error!");
About these ads

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 )

Google+ photo

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

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 1,166 other followers

%d bloggers like this: