Customizing NSLog Function on iPhone

2019-07-18 07:05发布

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条回答
走好不送
2楼-- · 2019-07-18 07:09
登录 后发表回答