Skip to content

Instantly share code, notes, and snippets.

@JRG-Developer
Last active December 27, 2015 19:39
Show Gist options
  • Save JRG-Developer/7377982 to your computer and use it in GitHub Desktop.
Save JRG-Developer/7377982 to your computer and use it in GitHub Desktop.
DebugLog
// Example Usage
- (void)exampleMethod
{
DebugMethod(self, _cmd); // _cmd refers to the selector for the method you're in
DebugLog(@"This message will only be logged on DEBUG");
}
/*
* DebugLog.m
*
* Created by Karl Kraft on 3/22/09.
* http://www.karlkraft.com
*
* "DebugMethod" added by Joshua Greene (jrg.developer@gmail.com)
*
*/
#ifdef DEBUG
#define DebugLog(args...) _DebugLog(__FILE__,__LINE__,__PRETTY_FUNCTION__,args)
#define DebugMethod(class, selector) _DebugMethod(class, selector)
#else
#define DebugLog(x...)
#define DebugMethod(x...)
#endif
void _DebugLog(const char *file, int lineNumber, const char *funcName, NSString *format,...);
void _DebugMethod(NSObject *object, SEL selector);
/*
* DebugLog.m
*
* Created by Karl Kraft on 3/22/09.
* http://www.karlkraft.com
*
* "DebugMethod" added by Joshua Greene (jrg.developer@gmail.com)
*
*/
#include "DebugLog.h"
void _DebugLog(const char *file, int lineNumber, const char *funcName, NSString *format,...) {
va_list ap;
va_start (ap, format);
if (![format hasSuffix: @"\n"]) {
format = [format stringByAppendingString: @"\n"];
}
NSString *body = [[NSString alloc] initWithFormat: format arguments: ap];
va_end (ap);
const char *threadName = [[[NSThread currentThread] name] UTF8String];
NSString *fileName=[[NSString stringWithUTF8String:file] lastPathComponent];
if (threadName) {
fprintf(stderr,"%s/%s (%s:%d) %s",threadName,funcName,[fileName UTF8String],lineNumber,[body UTF8String]);
} else {
fprintf(stderr,"%p/%s (%s:%d) %s",[NSThread currentThread],funcName,[fileName UTF8String],lineNumber,[body UTF8String]);
}
}
void _DebugMethod(NSObject *object, SEL selector) {
DebugLog(@"Running %@ '%@'", object, NSStringFromSelector(selector));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment