| Index | Submit class | Submit snippet | Submission feed | List |

ZNLog

Language: Objective-C, Author: Tony Arnold (tony@tonyarnold.com)
License: Creative Commons Attribution

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 at http://outerlevel.com/blog/2006/12/01/code-review/. It will show the file, parent method and line number of the point where the ZNLog method is called - very tasty!

It’s all licensed under Creative Commons Attribution 2.5 license, so just be sure to include a short attribution back to me in your header/notes/readme.

ZNLog source preview

//
//  ZNLog
//
//  Created by Tony on 24/01/07.
//  Copyright 2007 boomBalada! Productions..
//  Some rights reserved: <http://creativecommons.org/licenses/by/2.5/>
//

//  ZNLog.h
#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


//  ZNLog.m
@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
Download Archive

Compatible with:


Comments

Name

Website

Do you hate spammers? (Answer "Yes")