Brenwell

Bigger Better logging in Xcode

For those of you who have come from web development to iOS development, one of the things that may have disappointed or even surprised you is that the Xcode debugger is not as useful as say chrome’s, when it comes to logging.

Additionally, on that magnificent day when you submit your first app, you will sit at home waiting for the confirmation email that your app has been rejected. The fine print will tell you that you can not print any NSLog statements in your app. Then you have the fun experience of finding any of the remaining NSLog statements and deleting or commenting them out.

I would like to offer an alternative to NSLog -> BBlog. This is not some radical new discovery I am sharing, lots of people do it, I just found it helpful and thought it would make for a great new post.

// Debugging 
#define DEBUG_MODE 
#ifdef DEBUG_MODE 
#define BBLog( s, ... ) NSLog( @"%@ @ %d :: %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] ) 
#else 
#define BBLog( s, ... ) 
#endif

Its has 2 big benefits:

  1. It logs the file and line number of every BBLog call.
  2. By removing the
    #define DEBUG_MODE
    you will have all the logging removed without changing the code making Apple happy again.

Installation:

Simply add the above code to your AppName-Prefix.pch file 

Usage:

Replace all your

NSLog(@"Hello %@,World")

statements with

BBLog(@"Hello %@,World")