ArtFeel
ArtFeel

Reputation: 11801

How to filter console output in Xcode

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.

Upvotes: 19

Views: 16650

Answers (4)

david
david

Reputation: 1369

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.

Upvotes: 3

user3370277
user3370277

Reputation:

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.

Upvotes: 0

djromero
djromero

Reputation: 19641

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.

Upvotes: 3

Jano
Jano

Reputation: 63667

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

Upvotes: 5

Related Questions