Overriding NSLog for project and third parties

157 Views Asked by At

I'm trying to override the NSLog method for the entire project and third parties to add some logic to it, for example - if the logged message has the string "dog" in it, replace it with a dog emoji and then continue.

Is there a way to override the method such that third parties I use will also be affected by that?

Is there a different way to catch console output, modify it and only then print it?

Thanks!

1

There are 1 best solutions below

2
Liolik On

Here is how to do it.

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

void CustomLogger(NSString *format, ...) {
    va_list argumentList;
    va_start(argumentList, format);
    NSMutableString *message = [[NSMutableString alloc] initWithFormat:format arguments:argumentList];
    
    NSRange dogRange = [message rangeOfString:@"dog"];
    if (dogRange.location != NSNotFound) {
        [message replaceCharactersInRange:dogRange withString:@""];
    }
    
    NSLogv(message, argumentList);

    va_end(argumentList);
}