user2179694
user2179694

Reputation: 153

NSLog disable from certain classes and DEBUG

Hello guys I have found this code that is used to create a different NSLog (without data and timestamps) that displays the class where the log was made and the line number. I have read that is possible to disable the logging only for certain classes with NO_LOG but there was not explained how to use it exactly, I am quite new to obj-c and I appreciate an explanation on how to disable logging for certain classes and how to activate and deactivate the debugging. thanks

#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

Upvotes: 1

Views: 281

Answers (2)

Fonix
Fonix

Reputation: 11597

that is quite a verbose solution, i made one that is a lot neater than that

#ifndef DebugLog_h
#define DebugLog_h

#if DEBUG

#define DLog(...)   do{\
    printf("[%s:%d]", __FUNCTION__, __LINE__);\
    NSString *_S_ =  [NSString stringWithFormat:__VA_ARGS__];\
    printf(" %s\n",[_S_ cStringUsingEncoding:NSUTF8StringEncoding]);\
    }while(0);

#else

    #define DLog(...)

#endif

#endif

that will print the line number, class and function it came from

eg:

Dlog(@"hello %d", 123);

[-[SomeViewController viewWillAppear:]:91] hello 123

edit: if you add the file to your projectname-Prefix.pch file, then you can use it without having to include it everywhere

and it will automatically be taken out of release builds, because DEBUG is defined as a project definition automatically when its in debug mode

Upvotes: 1

trojanfoe
trojanfoe

Reputation: 122458

Add this:

#define NO_LOG 1

before #importing the file you've shown above.

BTW a better implementation would define PxDebug() and NSLog() to nothing if NO_LOG was defined...

Upvotes: 1

Related Questions