I read this article: http://weakreference.wordpress.com/2011/06/22/overriding-nslog-on-ios/.
The idea of the article is to add these two things to the prefix.pch file of your app, so that you can override the behavior of NSLog.
The two things I’m adding are:
#define NSLog(...) customLogger(__VA_ARGS__);
and
void customLogger(NSString *format, ...) {
va_list argumentList;
va_start(argumentList, format);
NSMutableString * message = [[NSMutableString alloc] initWithFormat:format
arguments:argumentList];
[message appendString:@"Our Logger!"]; // Our custom Message!
NSLogv(message, argumentList); // Originally NSLog is a wrapper around NSLogv.
va_end(argumentList);
[message release];
}
xCode throws an error match-o error, that it finds duplicates of customLogger.
Has anyone successfully overridden NSLog?
Thanks!
Edit in response to Rob:
Okay, great. We’re making progress! I moved thing just like you asked. Here’s what we have now:
My custom logger:
void customLogger(NSString *format, ...) {
va_list args;
va_start(args, format);
va_end(args);
[newLogger log:format withArgs:args];
}
//This is a newLogger Method
+ (void) log:(NSString *)format withArgs:(va_list) args{
NSArray *occ = [format componentsSeparatedByString:@"%@"];
NSInteger characterCount = [occ count];
NSArray *stringItems = [format componentsSeparatedByString:@"%@"];
NSMutableString *tmp = [[NSMutableString alloc] initWithFormat: @"%@",[stringItems objectAtIndex:0]];
for( int i = 1; i < characterCount; i++ ) {
NSString *value = va_arg(args, NSString *);
[tmp appendString:value];
[tmp appendString:[stringItems objectAtIndex:i]];
}
// Need to alter the above and actually do something with the args!
[tmp appendString:@"\n"];
[[newLogger sharedInstance].logBuffer appendString:tmp];
if ([newLogger sharedInstance].textTarget){
[[newLogger sharedInstance].textTarget setText:sharedInstance.logBuffer];
}
}
When I call +log, I get a SIBABRT error on Thread 1.
It sounds like you defined
customLoggerin your.pchfile. That means every.mfile includes it, so every.ofile your project creates contains its own copy ofcustomLogger. That’s why you get duplicate symbol definition errors from the linker.You need to just declare
customLoggerin the.pch, like this:And create a
customLogger.mfile containing the definition.