Simple “method got called” debugging

For quick and dirty project debugging, I often find myself using code like:

- (void)someRandomMethod
{
NSLog(@"-someRandomMethod called");
// ...
}

Playing around with precompiled headers in XCode, it occurred to me there’s a much neater way to do this. In the precompiled header (XCode probably created a .pch file for you) add:

#define HELLO NSLog(@"-----TRACE----- %s (%s:%d)", __PRETTY_FUNTION__, __FILE__, __LINE__);

Then in any method in your project you can get easy tracing:

- (void)someOtherMethod
{
HELLO
// ...
}

To prevent trace messages ever showing up in release builds, you could wrap the definition in an #ifdef and make the macro effectively a no-op.

One Response to “Simple “method got called” debugging”

  1. Abizer says:

    For an example of how to wrap up calls so that they are only restricted to debug builds, have a look at http://www.cimgf.com/2009/01/24/dropping-nslog-in-release-builds/

Leave a Reply