1

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
1
  • I re-wrote my logging some time ago: link as i prefere a more object-oriented way. This makes it easier to disable loging in specific files using [PxLogger setSilenceFiles:@["yourFileName"]] Commented May 14, 2013 at 15:16

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

4 Comments

NO_LOG is meant to be a file-list, so using it would be #define NO_LOG file1,file2
@JonathanCichon Thanks - I think you are right. What a crap piece of code.
thanks ;), but it is old and so it worked taking arguments from the comandline.
@JonathanCichon Did you write it then? I had no idea. I think the idea of using an inline is bad (there are as many copies of that function as there are implementation files) and it should be logging to a file with date/time, logging levels, thread ids and class and method names.
1

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

4 Comments

Isn't logging the class and method name more useful than logging the file and line number?
well normally your files are named the same as your classes and if you have the line number, you can tell what method its in
I rather want the file, line no btw :D It is much more concrete and you can exactly see at what instruction it crashed
ah lol, well just add #define _FILE_ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) and printf("[%s:%d]", _FILE_, __LINE__); back (or just look at the edit revision history)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.