Created
August 13, 2010 12:59
-
-
Save tadelv/522835 to your computer and use it in GitHub Desktop.
Awesome debug
This file contains 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
//Debug.h | |
#import <Foundation/Foundation.h> | |
#ifdef DEBUG | |
#define DebugLog( xx, ...) _DebugLog(@"%s(%d): " xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) | |
#else | |
#define DebugLog( s, ... ) ((void)0) | |
#endif | |
void _DebugLog(NSString *format, ...); | |
void DEBUGChangeOutputToFile(NSString *filename); |
This file contains 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
//Debug.m | |
#import "Debug.h" | |
void _DebugLog(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); | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
[formatter setDateFormat:@"yyyy-MM-dd@HH:mm:ss"]; | |
NSString *dateString = [formatter stringFromDate:[NSDate date]]; | |
fprintf(stderr,"[%s]%s",[dateString UTF8String] ,[body UTF8String]); | |
fflush(stderr); | |
[body release]; | |
[formatter release]; | |
} | |
void DEBUGChangeOutputToFile(NSString *filename) { | |
//change stderr output to file | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:filename]; | |
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment