Nice technique to specify log messages

Just changed jobs, and having to switch back to Objective C for most of my work. I’ve used a selectable macro in the past for log messages:

#if 0 // 0 == no debug, 1 == lots of messages
#define LOG(...) NSLog(__VA_ARGS__)
#else
#define LOG(...)
#endif

You can add this to each source file, and selectively turn messages on or off with one tiny change. But the problem is, you look in console and there are slews of log messages. So how to know which is from what file?

Well, I just found out I can change the above to:

#if 0 // 0 == no debug, 1 == lots of mesages
#define LOG(...) NSLog(@"DBManager: " __VA_ARGS__)
#else
#define LOG(...)
#endif

That is, since Objective C will coalesce strings, it will merge @”DBManager: ” into whatever format string you used.

I suppose the __FILE__ or NSStringFromClass([self class]) might work too – but I didn’t try.

Advertisements