Search code examples
iphoneiosobjective-ccocoa-touchnslog

Disable NSLog for a specific class


I know how you can disable all NSLog messages from answers like this one and this one.

It's yet unclear to me what should I define in my .pch file to conditionally disable NSLog in a separate class.

Any ideas?


Solution

  • i use the following log macros for similar stuff. You can define NO_LOG to whatever files you want to skip from loging (comma-separated).

    #define MAKESTRING(__VA_ARGS__) #__VA_ARGS__
    #define TOSTRING(...) MAKESTRING(__VA_ARGS__)
    
    static inline void PxReportv(BOOL doLog, char const *file, int line, NSString *prefix, NSString *fmt, va_list argList) {
        if (doLog) {
            NSString *fileNameWithExtension = [[NSString stringWithFormat:@"%s", file] lastPathComponent]; 
    #ifdef NO_LOG
            NSString *fileName = [fileNameWithExtension stringByDeletingPathExtension];
            char *f = TOSTRING(NO_LOG);
            NSArray *comps = [[[NSString alloc] initWithFormat:@"%s", f] componentsSeparatedByString:@","];
            for (NSString *except in comps) {
                if ([except isEqualToString:fileName]) {
                    return;
                }
            }
    #endif
            vprintf([[[NSString alloc] initWithFormat:[[NSString alloc] initWithFormat:@"%@ <%@ [%d]> %@\n", prefix, fileNameWithExtension, line, fmt] arguments:argList] cStringUsingEncoding:NSUTF8StringEncoding], NULL);
        }
    }
    
    static inline void PxReport(BOOL doLog, char const *file, int line, NSString *prefix, NSString *fmt, ...) {
        va_list ap;
        va_start(ap, fmt);
        PxReportv(doLog, file, line, prefix, fmt, ap);
        va_end(ap);
    }
    
    #define PxError(...) PxReport(YES, __FILE__, __LINE__, @"[ERROR]", __VA_ARGS__)
    
    #ifdef DEBUG
        #define PxDebug(...) PxReport(YES, __FILE__, __LINE__, @"[DEBUG]", __VA_ARGS__)
        #define NSLog(...) PxReport(YES, __FILE__, __LINE__, @"", __VA_ARGS__)
    #else
        #define PxDebug(...)
        #define NSLog(...)
    #endif