I’ve seen a few different versions of this, so today I’ll post what I use to help me debug code execution order and debug info.
The code gets removed for non-debug builds, and you’ll have to define DEBUG in your debug build settings. Here it is:
#if DEBUG==1 #define AGLog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]); #define MARK AGLog(@"%s", __PRETTY_FUNCTION__); #else #define AGLog(format, ...) #define MARK #endif
Usage is the same as NSLog:
CGRect myRect = CGRectMake(10,10,30,30); AGLog(@"MyRect: %f %f %f %f", myRect.origin.x, myRect.origin.y, myRect.size.width, myRect.size.height);
Output will be something like the following:
2011-02-16 00:47:49.078 Punch![19047:307] -[PTestAppDelegate application:didFinishLaunchingWithOptions:]:MyRect: 10.0000001 0.000000 30.000000 30.000000
It shows you the class, the method, and your debug info. Very handy!
As you can see, for non-debug builds, the macros get defined out, so you dont need to worry about NSLog’s slowing down your release or distribution builds (and they do slow things down).
One thing to keep in mind is that you will get compiler warnings in your release/distribution build if you are declaring a variable that is only used for debugging purposes.
Using the above example, if you were to write something like this:
CGRect myRect = CGRectMake(10,10,30,30); AGLog(@"MyRect: %f %f %f %f", myRect.origin.x, myRect.origin.y, myRect.size.width, myRect.size.height);
…then everything would be fine for a debug build, but since the log statements get defined out (so they dont exist in the release/distribution builds), the myRect line would cause a compiler warning of an unused variable if that is the only place that it is accessed.
The MARK macro is useful in tracking execution order and whether a method is being called or not. I usually stick these in dealloc’s to track whether or not it gets called and if I missed any releases:
- (void)dealloc
{
MARK;
[coverPlayer release];
[super dealloc];
}
The debug output in your console will look like this:
2011-02-16 00:47:52.254 Punch![19047:307] -[AGMovieView dealloc]:-[AGMovieView dealloc]
Hope you find it handy.