Created
December 8, 2008 18:14
-
-
Save mkhl/33543 to your computer and use it in GitHub Desktop.
Objective-C Logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: http://blog.mbcharbonneau.com/post/56581688/better-logging-in-objective-c | |
#define DebugLog(format, ...) NSLog(@"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(format), ##__VA_ARGS__]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In Leopard, NSLog logs to console.log unconditionally... | |
// Source: http://lapcatsoftware.com/blog/2008/01/06/logging-in-leopard/ | |
#ifdef __OBJC__ | |
#import <Cocoa/Cocoa.h> | |
#ifdef JJLOGGING | |
#define JJLog(...) (void)printf("%s:%i %s: %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]) | |
#else | |
#define JJLog(...) | |
#endif | |
#endif | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)respondsToSelector:(SEL)theSelector | |
{ | |
NSLog(@"Asked if I respond to %@", NSStringFromSelector(theSelector)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Log the current function's signature | |
NSLog(@"%s", __PRETTY_FUNCTION__); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSXLogger : NSObject | |
{ | |
} | |
+ (void)logFile:(char *)sourceFile lineNumber:(int)lineNumber format:(NSString*)format, ...; | |
+ (void)setLogOn:(BOOL)logOn; | |
@end | |
#define NSXLog(string, ...) \ | |
[NSXLogger logFile:__FILE__ lineNumber:__LINE__ format:(string), ##__VA_ARGS__] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static BOOL __NSXLogOn = NO; | |
@implementation NSXLogger | |
+ (void)initialize | |
{ | |
char *env = getenv("NSXLogOn"); | |
if (strcmp(env == NULL ? "" : env, "NO") != 0) | |
__NSXLogOn = YES; | |
} | |
+ (void)logFile:(char *)sourceFile lineNumber:(int)lineNumber format:(NSString*)format, ...; | |
{ | |
va_list ap; | |
if (__NSXLogOn == NO) | |
return; | |
va_start(ap, format); | |
NSString *file = [[NSString alloc] initWithBytes:sourceFile length:strlen(sourceFile) encoding:NSUTF8StringEncoding]; | |
NSString *print = [[NSString alloc] initWithFormat:format arguments:ap]; | |
va_end(ap); | |
// NSLog handles synchronization issues | |
NSLog(@"%s:%d %@", [[file lastPathComponent] UTF8String], lineNumber, print); | |
[print release]; | |
[file release]; | |
} | |
+ (void)setLogOn:(BOOL)logOn | |
{ | |
__NSXLogOn = logOn; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: http://zathras.de/angelweb/blog-uk-helper-macros.htm | |
#define UKLog(args...) NSLog(@"%s: %@", __PRETTY_FUNCTION__, [NSString stringWithFormat: args]) | |
// Alternatively: | |
#define UKLog(format, ...) NSLog(@"%s: %@", __PRETTY_FUNCTION__, [NSString stringWithFormat: format, ##__VA_ARGS__]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: http://tonyarnold.com/projects/znlog/ | |
#import <Cocoa/Cocoa.h> | |
@interface ZNLog : NSObject {} | |
+(void)file:(char*)sourceFile function:(char*)functionName lineNumber:(int)lineNumber format:(NSString*)format, ...; | |
#define ZNLog(s,...) [ZNLog file:__FILE__ function: (char *)__FUNCTION__ lineNumber:__LINE__ format:(s),##__VA_ARGS__] | |
@end | |
@implementation ZNLog | |
+ (void)file:(char*)sourceFile function:(char*)functionName lineNumber:(int)lineNumber format:(NSString*)format, ... | |
{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
va_list ap; | |
NSString *print, *file, *function; | |
va_start(ap,format); | |
file = [[NSString alloc] initWithBytes: sourceFile length: strlen(sourceFile) encoding: NSUTF8StringEncoding]; | |
function = [NSString stringWithCString: functionName]; | |
print = [[NSString alloc] initWithFormat: format arguments: ap]; | |
va_end(ap); | |
NSLog(@"%@:%d %@; %@", [file lastPathComponent], lineNumber, function, print); | |
[print release]; | |
[file release]; | |
[pool release]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment