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 dElseNSLog
version 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!");