skip main content

Posts Tagged ‘xcode’

Logging/Debug setup in Xcode 4

posted by Duncan at 9:10 am on May 31st, 2011

Here’s a little snippet of how I manage logging in my applications with Xcode 4. I’m sure it works just fine in 3 too, but this is more of a reminder for me as it’s something I always forget how to do when starting a new project.

Firstly I update the *_Prefix.pch file that lives in the Other Resources directory so it also contains the snippet below. I use the Prefix code posted by Marcus Zarra which seems to work just fine for me:

#ifdef DEBUG
  #define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
  #define ALog(...) [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__]
#else
  #define DLog(...) do { } while (0)
  #ifndef NS_BLOCK_ASSERTIONS
    #define NS_BLOCK_ASSERTIONS
  #endif
  #define ALog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#endif
 
#define ZAssert(condition, ...) do { if (!(condition)) { ALog(__VA_ARGS__); }} while(0)

Next I update my project by adding the DEBUG=1 flag to the preprocessor settings. Click on your application target, and select the Build Settings tab is selected along with All and Combined. I then do a search for ”- preprocessing” which should leave you with the Preprocessor Macros remaining. Now just DEBUG-1 to the correct one, as the photo below shows.

DEBUG flag

Hope that saves someone some time. Oh, and if anyone has any improvements I can make to this, I’d love to here them.


back to the top