Skip to content

Instantly share code, notes, and snippets.

@sbrocket
Created May 27, 2011 21:55
Show Gist options
  • Save sbrocket/996267 to your computer and use it in GitHub Desktop.
Save sbrocket/996267 to your computer and use it in GitHub Desktop.
// Use debugLog() or debugLogPretty() to log messages to the console when the "Debug" configuration
// is used to build the project - these logging messages won't appear in a Release build.
//
// Use assertLog() for more serious conditions. On Debug builds, assertLog() will throw an assertion
// and on Release builds it will turn into a standard NSLog (in case it comes to digging through a user's
// device logs to fix something)
#ifdef DEBUG
#define _D(...) __VA_ARGS__
#define debugLog(fmt, ...) NSLog(fmt, ##__VA_ARGS__)
#define debugLogPretty(fmt, ...) NSLog(@"%s (%@:%d)\n%@", \
__PRETTY_FUNCTION__, \
[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
__LINE__, \
[NSString stringWithFormat:(fmt), ##__VA_ARGS__])
#define assertLog(fmt, ...) [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] \
file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] \
lineNumber:__LINE__ \
description:[NSString stringWithFormat:(fmt), ##__VA_ARGS__]]
#else
#define _D(...)
#define debugLog(fmt, ...)
#define debugLogPretty(fmt, ...)
#define assertLog(fmt, ...) NSLog(@"%s (%@:%d)\n%@", \
__PRETTY_FUNCTION__, \
[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
__LINE__, \
[NSString stringWithFormat:(fmt), ##__VA_ARGS__])
#endif
// Additionally, the verboseLog() or verboseLogPretty() macros can also be used to only log messages when
// the "Verbose" build configuration is selected.
#ifdef VERBOSE
#define verboseLog(fmt, ...) debugLog(fmt, ##__VA_ARGS__)
#define verboseLogPretty(fmt, ...) debugLogPretty(fmt, ##__VA_ARGS__)
#else
#define verboseLog(fmt, ...)
#define verboseLogPretty(fmt, ...)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment