I've wanted to improve the logging in my apps for quite a while. I've searched and found a number of websites that talk through different options. I found these to be quite helpful:
http://stackoverflow.com/questions/969130/nslog-tips-and-tricks
http://cocoaheads.byu.edu/wiki/different-nslog
But I didn't see a "finished" solution that was polished like I wanted it to be. Here is what I wanted:
- Easily turned off; don't run in a distribution build
- Short to use; NSLog short. I don't want anything longer
- Show the method name and line number
The solution I came up with looks pretty good. To use my logger, all I have to do is use a GPLog() statement, just like NSLog(). The cool thing about that it is that it made for an easy find and replace to use it through mode code. Statements like GPLog(@"%d", numberVar); will work just like NSLog does. The statement GPLog(@""); will produce something like this in the log.
2010-12-08 16:19:07.537 GW Mail[8016:207] -[AppDelegate_Phone application:didFinishLaunchingWithOptions:] (33) ::
I put the following in my .pch file so that it would be globally available.
#define kDevMode
#ifdef kDevMode
#define GPLog( s, ... ) { \
NSLog( @"%s (%d) :: %@ \t", __PRETTY_FUNCTION__, __LINE__,\
[NSString stringWithFormat:(s), ##__VA_ARGS__] ); \
}
#warning kDevMode ON!
#endif
#ifndef kDevMode
#define GPLog( s, ... ) { }
#endif
A few things to notice. To turn logging off, all you have to do is put an x or something in front of the #define kDevMode statement.
When the logger is turned on, it throws a warning. This helps you to remember to turn it off before you do your distribution builds.
It's pretty straight forward code. Hopefully this is helpful to someone!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment