Subscribe to
NSLog(); Header Image

A Better NSLog()

Nothing could be better than this NSLog();, but, well, forget it: I'm unable to make this funny.

MLog improves on Cocoa's NSLog() by adding in the name of the source file and the line number from which the NSLog() call is made. In other words,

MLogString(@"logged!"); produces:
2005-02-13 20:06:07.582 MyApp[1465] main.m:15 logged!

Helpful! Which, sadly, is more than I can say for this NSLog(); most of the time. 🙂

One Response to "A Better NSLog()"

  1. I use a similar function all the time in this one project I'm working on. Instead of using a class and a define to shorten the call to the object, I just use a define. In your development section of the Target Settings, you can set "-DDEBUG_HEAVY" in "Other C Flags". When you build development, these show up but when you build deployment, they don't. Quite handy.

    /* For a debug build, declare the SMLog() function */
    #define SMLog(...) printf("SearchMagic[%u] %s\n", getpid(), [[NSString stringWithFormat:@"%@:%u: %@", [[NSString stringWithCString:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:__VA_ARGS__]] cString]);
    /* For a non-debug build, define it to be a comment so there is no overhead in using it liberally */
    #define SMLog(fmt, ...) /** */