17
Apr
09

Snippet: Debug Macros

[EDIT: Revised to highlight the drama!]

Here’s a couple handy tips for your collection of Objective-C debugging macros which made it to the front page of iPhoneKicks where we noticed it, but it appears — see comments below! — were lifted wholesale from this post a month earlier, and the apparent real author is on a mission to set the Internet straight. Heh. That’s kinda amusing in a goths-on-LJ kind of way, isn’t it now? But seriously, this does look like a rather tacky case of blog plagiarism, so we’re happy to spotlight the controversy. Stay tuned for rebuttals!

That said, wherever credit is properly due, these are still worth appropriating for yourself:

Tip #1: __PRETTY_FUNCTION__ will provide the Objective-C class name and method call.

#define ALog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);
#define MARK	ALog(@"%s", __PRETTY_FUNCTION__);  
Use like
ALog ( @"My iPhone is an %@, v %@",
    [[UIDevice currentDevice] model],
    [[UIDevice currentDevice] systemVersion]);
and you get nicely readable output like this
2008-24-12 12:34:56.789 MyApp00000:000] -[MyAppDelegate applicationDidFinishLaunching:]:My iPhone is an iPhone Simulator, v 2.2

Tip #2: Easy timer integration with NSTimeInterval.

#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
#define END_TIMER(msg) 	NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; ALog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]);
Use like
  START_TIMER;
  // do time intensive stuff
  END_TIMER(@"did time intensive stuff");
and get a log message like
2008-24-12 12:34:56.789 MyApp[00000:000] -[MyAppDelegate myFunction:]:did time intensive stuff Time = 1.2345678
Also check out  The Evolution of a Replacement for NSLog for another take on handy macro tricks!

h/t: iPhoneKicks!


2 Responses to “Snippet: Debug Macros”


  1. 1 Stephan Burlot Apr 18th, 2009 at 5:24 am

    These macros have just been copied from my blog: http://blog.coriolis.ch/2009/01/05/macros-for-xcode/ and published one month before the one you mention.

  2. 2 Alex Apr 18th, 2009 at 7:52 am

    Heh. Sure does look that way, doesn’t it Stephan? That’s rather tacky of that fellow, yes.

Leave a Reply