How to filter console output in Xcode
Asked Answered
S

4

19

In my iOS project, I use one 3rd-party library, which is is incredible spamming into the console output. Can I apply any filtering to debug output.

Satiable answered 26/10, 2011 at 7:54 Comment(3)
It may not be a real solution but MCLog is a XCode plugin enabling output filtering.Hamiltonian
@Hamiltonian Thx, I should accept this comment as best answer, because MCLog do exactly what I need.Satiable
l've copied my comment as an answer then.Hamiltonian
H
3

Works in Xcode 5.0 through 7.3, Apple no longer supports Xcode plug-ins as of Xcode 8.0.

MCLog should be what you're looking for.

This is an XCode plugin.

Hamiltonian answered 4/9, 2015 at 14:26 Comment(1)
yeah... not working anymore. github issues state the same and no one seems to careVoe
B
5

If the library is using NSLog you can redefine it and discard the log message when it comes from the library. Example code:

#define NSLog(args...) [[Logger singleton] debugWithLevel:kDebug line:__LINE__ funcName:__PRETTY_FUNCTION__ message:args];

// poor man's nslog
@interface Logger : NSObject

typedef enum {
    kTrace=0, kDebug=1, kInfo=2, kWarn=3, kError=4, KSilent=5
} LoggerLevel;


// ...

@implementation Logger

+(Logger *)singleton {
    static dispatch_once_t pred;
    static Logger *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[Logger alloc] init];
        shared.logThreshold = kTrace;
    });
    return shared;
}
-(void) debugWithLevel:(LoggerLevel)level 
                  line:(int)line 
              funcName:(const char *)funcName 
               message:(NSString *)msg, ... {

    va_list ap;         
    va_start (ap, msg); 
    msg = [[[NSString alloc] initWithFormat:msg arguments:ap] autorelease];
    va_end (ap);        

     msg = [NSString stringWithFormat:@"%s %50s:%3d - %@", levelName[level], funcName, line, msg];

    // ... filter by class name ...

    fprintf(stdout,"%s\n", [msg UTF8String]);
}
@end

Note that funcName contains the classname and method sending the message. If the library is a good citizen and has classes that start with a prefix, discard the output if the className starts with that. Otherwise you have to load a list of classes from that library and check them before the fprintf line.

This of course, doesn't duplicate the log to syslogd like NSLog does, but who cares. :P

Broucek answered 26/10, 2011 at 9:0 Comment(0)
I
3

It depends if you're using directly the 3rd party library source code in your project or a binary library.

If you're using the source code I'd suggest to check what they are using to log the messages. It may have a way to reduce the verbosity. If they are using plain NSLog the only option would be to redefine NSLog in order to do some filtering, as Jano proposed you.

If they are using low level functions like printf and the like, your best option is to replace them with your own custom logging macro, like:

#ifdef DEBUG_3P
    #define LOG_3P(str) NSLog(@"%s", str)
#else
    #define LOG_3P(str) /* nothing */
#endif

Then, replace printf("a c string message") with LOG_3P("a c string message"). You'll need to customize the solution, adjust macro parameters or even add several macros for your case. And make a few search and replace until it works.

When you want to see the 3rd party library logs, just define DEBUG_3P in your build settings as C flags: -D DEBUG_3P, otherwise it will be mute.

If you're using a binary library you can just build it with its release configuration, disabling or reducing the logs verbosity to its minimum.

Infrared answered 26/10, 2011 at 11:29 Comment(2)
I use source code library written in C, and it uses printf(), vprintf() and puts() for logging.Satiable
In that case, unless you replace them with your own logging macro, I'm afraid you can't easily intercept the output. See my updated answer.Infrared
H
3

Works in Xcode 5.0 through 7.3, Apple no longer supports Xcode plug-ins as of Xcode 8.0.

MCLog should be what you're looking for.

This is an XCode plugin.

Hamiltonian answered 4/9, 2015 at 14:26 Comment(1)
yeah... not working anymore. github issues state the same and no one seems to careVoe
B
0

For Swift, I wrote a wrapper around print() that does just this. See here: https://github.com/SebastianMecklenburg/TagLog

It works by adding tags to debug messages and then filter the output by those tags. It works all in code and doesn't need an Xcode plugin.

Boiled answered 5/6, 2016 at 7:38 Comment(2)
Will this filter out 3rd party text that's sent to the console via the print() command?Tetragrammaton
Github link is a 404 nowCaroncarotene

© 2022 - 2024 — McMap. All rights reserved.