Skip to content

Instantly share code, notes, and snippets.

@siqin
Created July 10, 2012 06:30
Show Gist options
  • Save siqin/3081582 to your computer and use it in GitHub Desktop.
Save siqin/3081582 to your computer and use it in GitHub Desktop.
log of objective-c
#pragma mark -
#define DEFAULT_LOG_FILENAME @"log.log"
- (void)log:(NSString *)log
{
#ifdef DEBUG
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *baseDir = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *logPath = [baseDir stringByAppendingPathComponent:DEFAULT_LOG_FILENAME];
// dispatch_once needed ?
if (![[NSFileManager defaultManager] fileExistsAtPath:logPath]) {
[[NSFileManager defaultManager] createFileAtPath:logPath contents:nil attributes:nil];
}
FILE *file = fopen([logPath UTF8String], "ab");
fwrite([log UTF8String], strlen([log UTF8String]), 1, file);
fclose(file);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment