Mark Damon Hughes Cocoa logging [Parental Advisory: Explicit Lyrics] [about]
Cocoa logging
Sat, 2007Sep01 16:06:11 PDT
in Software by kamikaze

I've finally worked out what I think is a reasonably robust logging system for my debugging code, which I can disable when I'm ready for production:


In the project's Debug build configuration, set preprocessor macro DEBUG=1

In foo_Prefix.pch:

#ifdef DEBUG
// DLOG takes a format argument and 0 or more args:
// DLOG(@"");
// DLOG(@"%d", x);
#define DLOG(fmt, ...) NSLog(@"%s: " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__)
#else
#define DLOG(...)
#endif

Now I can use DLOG(@"foo"); instead of NSLog(@"foo"), and the console contains:

2009-02-03 04:05:06.789 AwesomeApp[6109:a0f] -[AwesomeAppDelegate someMethod]: foo

__PRETTY_FUNCTION__ inserts the current classname and method selector.

##__VA_ARGS__ removes the preceding comma if there are no arguments.

Feedback  | Key: ] =local file, * =off-site link  | Copyright © 2003-2010 by Mark Damon Hughes | Subscribe with RSS 2.0