NSLog() with shorter output

At BYU CocoaHead, you can find good implementation of logging function.

Why is such log function is necessary for Cocoa/Objective-C environment? I like to open a debug console which shows gdb environment. The Xcode’s default variable viewer, watch etc feel worse than that of the Visual C++. Its variable view window doesn’t filter things to show variables which should be the main interest like local, user-selected watch list.

So, the gdb console is way to go, but NSLog() puts too much information to the gdb console, so you should make the gdb console window wider.

So, I decided to write a new NSLog() which prints less-but-necessary information, and I found the BYU CocoaHead article.
One problem is that it is only for 10.5 Leopard and later. So, I added to custom methods to the NSString to make same to those of Leopard NSString class.

#ifdef DEBUG
	#warning "DEBUG is defined"
	void QuietLog( NSString *format, ...);
	#define JALog( FORMAT_STRING, ...) \
	{ \
		NSString *file = [[NSString alloc] initWithUTF8String:__FILE__]; \
		printf("%s:%d - ", [[file lastPathComponent] UTF8String], __LINE__ ); \
		[file release]; \
		QuietLog( FORMAT_STRING, ##__VA_ARGS__ ); \
	#warning "RELEASE mode"
	#define JALog( FORMAT_STRING, ...)

[Source 1] JALog.h

#import <Cocoa/Cocoa.h>

#ifndef NSAppKitVersionNumber10_4
#warning "For Mac OS X 10.4 and earlier"
@interface NSString (LeopardLikeNSString)
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
										withString:(NSString *)replacement;
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
										withString:(NSString *)replacement

@implementation NSString (LeopardLikeNSString)
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
										withString:(NSString *)replacement
	NSString * newString = [self stringByReplacingOccurrencesOfString:target														            
							                                                 range:NSMakeRange(0, [target length])];

	return newString;


- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
							withString:(NSString *)replacement
	NSMutableString *newString = [self mutableCopy];
	int howmany;

	howmany = [newString replaceOccurrencesOfString:target 

	return [newString autorelease];


#ifdef DEBUG
void QuietLog( NSString *format, ... )
	if( format == nil )
		printf("JALog: format is nil\n");

	va_list argList;
	va_start( argList, format );

	NSString *s = [[NSString alloc] initWithFormat:format 
	printf("%s\n", [[s stringByReplacingOccurrencesOfString:@"%%"
								        withString:@"%%%%"] UTF8String] );
	[s release];

	va_end( argList );

[Source 2] JALog.m

I used replaceOccurrencesOfString: withString: options: range: of NSMutableSring. It looks like that Apple people also used the NSMutable method to implement those for the NSString.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: