About ZNLog

How many times have you found yourself coding some “sick cocoa code… gosh” (excuse the Napoleon Dynamite reference there), and thought - “Gee whiz Myself, NSLog sure is sorta limp in the actual informative-ness stakes, ain’t it?”

Me? All the time. So I did a bit of hunting about and devised the following logging class based upon comments by Scott Morrison:

#import <Cocoa/Cocoa.h>

@interface ZNLog : NSObject {}

+(void)file:(char*)sourceFile function:(char*)functionName lineNumber:(int)lineNumber format:(NSString*)format, ...;

#define ZNLog(s,...) [ZNLog file:__FILE__ function: (char *)__FUNCTION__ lineNumber:__LINE__ format:(s),##__VA_ARGS__]

@end

@implementation ZNLog

+ (void)file:(char*)sourceFile function:(char*)functionName lineNumber:(int)lineNumber format:(NSString*)format, ...
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  va_list ap;
  NSString *print, *file, *function;
  va_start(ap,format);
  file = [[NSString alloc] initWithBytes: sourceFile length: strlen(sourceFile) encoding: NSUTF8StringEncoding];

  function = [NSString stringWithCString: functionName];
  print = [[NSString alloc] initWithFormat: format arguments: ap];
  va_end(ap);
  NSLog(@"%@:%d %@; %@", [file lastPathComponent], lineNumber, function, print);
  [print release];
  [file release];
  [pool release];
}

@end

License

ZNLog is licensed under the Creative Commons Attribution 2.5

Checking ZNLog out of the repository

svn co http://svn.tonyarnold.com/cocoa/ZNLog/trunk

Downloads

/workspace/upload/projects/ZNLog.m.zip