Disable NSLog for a specific class

I know how you can disable all messages NSLog

from replies like this and this one .

It's not clear to me yet what I should define in my .pch

file to conditionally disable NSLog

in a separate class.

Any ideas?

+3


source to share


2 answers


I use the following log macros for stuff like this. You can define NO_LOG

for any files you want to skip from login (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

      

+2


source


Using

#define NSLog //

He will comment on all your magazines if it's one liner



OR use

#define NSLog(...)

+3


source







All Articles