2ko3la
Last Updated: February 25, 2016
·
389
· iamleeg

Logging the current method in Objective-C

I've recently seen macros that look something like this:

#define DLog(msg, ...) do { \
  NSString *dLogString = [NSString stringWithFormat:@"[%@ %@]: %@", NSStringFromClass[self class], NSStringFromSelector(_cmd), msg]; \
  NSLog(@"%@", __VA_ARGS__); \
} while(0)

All of that substitution can be replaced with __PRETTY_FUNCTION__, which also works in C and C++ contexts.

#define DLog(msg, ...) do { \
  NSString *dLogString = [NSString stringWithFormat:@"%s: %@", __PRETTY_FUNCTION__, msg]; \
  NSLog(@"%@", __VA_ARGS__); \
} while (0)