Customizing NSLog Function on iPhone

2019-07-18 06:39发布

问题:

I know that it's possible to do method swizzling for Selectors and Methods in Objective C. Is it possible to swizzle functions like NSLog to our custom function. I wanted to add some extra functionality along with NSLog in the custom function.

EDIT:

I finally ended up using another function which will call NSLog internally.

#define NSLog(...) CustomLogger(__VA_ARGS__);

void CustomLogger(NSString *format, ...) {
    va_list argumentList;
    va_start(argumentList, format); 
    NSMutableString * message = [[NSMutableString alloc] initWithFormat:format 
                                                arguments:argumentList];


    [message appendString:@"Our Logger!"];
    NSLogv(message, argumentList);

    va_end(argumentList);
    [message release];
}

回答1:

It seems possible, but entirely unsupported, and can change at any time.

See How do I redirect all errors, including uncaught exceptions, NSLog calls, and other logs, to a log file on Mac OS X?