3

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?

Community
  • 1
  • 1
Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120

2 Answers2

4

Use

#define NSLog //

It will comment all your logs if it's one liner

OR use

#define NSLog(...)

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • How about not ALL, but in a specific class? – Sergey Grischyov Jan 18 '13 at 17:39
  • Put this #define in that file. Don't put it in .pch file – Inder Kumar Rathore Jan 18 '13 at 17:40
  • NO one says that,,, and few people are just against me :( At least We can remove our answers from here... :) – Anoop Vaidya Jan 18 '13 at 18:38
  • 1
    @AnoopVaidya but before down voting one should specify why he/she has down voted. It's not that our answers are always right there may be something wrong with and I'm open to hear that. Anyways this is correct as far as I'm concerned :) – Inder Kumar Rathore Jan 18 '13 at 18:44
  • 1
    Yes I know, Even mine was just similar to you, you have noticed, i also got -1, now i removed. and now i remeber by name 3 people who downvotes, abuses without a proper explanation as they have 30K, 50K points.... and we are in 6K :p – Anoop Vaidya Jan 18 '13 at 18:47
2

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
Jonathan Cichon
  • 4,396
  • 16
  • 19