Power corrupts. Absolute power is kind of neat.
Power corrupts. Absolute power is kind of neat.
Better LoggingNSLog is nice, but we can do better (no, not that NSLog, though it’s good, too). I came across A Better NSLog a few weeks ago and loved the result so much I came up with a variant in use, and I’m sure you can come up with others. The deal is, would you rather have:
Or:
The answer is obvious. To do this, I made a new Cocoa class for my apps called DLog, for, obviously, Debug Log. The code is below and the classes are attached to this post. == #import <Cocoa/Cocoa.h>
#define DLog(s,...) [DLog logFile:FILE lineNumber:LINE format:(s),##__VA_ARGS__] #define DFLog(s,...) [DLog logFile:FILE lineNumber:LINE function:(char*)__FUNCTION__ format:(s),##__VA_ARGS__]
== When you import this header into your code, two macros are defined for you. The C preprocessor replaces such macros with the macro content before compiling the code, so the Speaking of methods: ==
#import “DLog.h”
static BOOL __DEBUG=NO;
if(strcmp(env, "YES") <span style="color: #0000ff">0</span>)</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>__DEBUG = <span style="color: #760f50">YES</span>;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">}</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px"><br /></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">+ (<span style="color: #760f50">void</span>) logFile: (<span style="color: #760f50">char</span> *) sourceFile</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-converted-space">Â </span>lineNumber: (<span style="color: #760f50">int</span>) lineNumber</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-converted-space">Â </span>format: (NSString *) format, ...;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco">{</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>va_list ap;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>NSString *print, *file;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span><span style="color: #760f50">if</span>(__DEBUG NO) return; va_start(ap, format); file = [NSString stringWithCString:sourceFile]; print = [[NSString alloc] initWithFormat:format arguments:ap]; va_end(ap);
NSLog(@"%s:%d %@", [[file lastPathComponent] UTF8String], lineNumber, print);
[print release]; }
+ (void) logFile: (char *) sourceFile  lineNumber: (int) lineNumber function: (char *) functionName  format: (NSString *) format, ...; { va_list ap; NSString *print, *file, *function; if(__DEBUG <span style="color: #760f50">NO</span>)</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #760f50"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span></span>return<span style="color: #000000">;</span></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>va_start(ap,format);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>file = [NSString stringWithCString:sourceFile];</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>function = [NSString stringWithCString:functionName];</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>print = [[NSString alloc] initWithFormat:format arguments:ap];</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>va_end(ap);</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px"><span class="Apple-tab-span" style="white-space:pre"> </span></p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco"><span class="Apple-tab-span" style="white-space:pre"> </span>NSLog(<span style="color: #891315">”%s:%d in %@ %@”, [[file lastPathComponent] UTF8String], lineNumber, function, print);
[print release]; }
+ (void) setLogOn: (BOOL) logOn { __DEBUG=logOn; }
@end == Now the really great thing about this solution is that it’s a drop-in replacement for NSLog. Replace all instances of NSLog in other files with DLog to get the file and line, or DFLog to get the function as well. Now this won’t work by default. You’ll need to add the environment variable DEBUG=YES to your executable in Xcode. What this means, also, is that you can tell your users to run with that flag and then get a level of debugging output. Perhaps you could setup a DFLog1 to DFLog9 and get logging levels. You get the idea. |
|
Post new comment