Skip to content

Instantly share code, notes, and snippets.

@osmszk
Last active August 29, 2015 13:55
Show Gist options
  • Save osmszk/8726500 to your computer and use it in GitHub Desktop.
Save osmszk/8726500 to your computer and use it in GitHub Desktop.
Write console log as File output
+ (NSString*)consoleLogFileWithDate
{
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setDateFormat:@"yyyyMMddHHmmss"];
[inputDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"US"]];
NSString *dateStr = [inputDateFormatter stringFromDate:[NSDate date]];
return [NSString stringWithFormat:@"console%@.log",dateStr];
}
static FILE *consoleLog;
- (void)writeToFileConsoleLog
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *oldConsoleFilename = CONSOLELOG_OLD_FILE;
NSString *lastConsoleFilename = [self consoleLogFileWithDate];//CONSOLELOG_FILE;
NSString *path = [documentsDirectory stringByAppendingPathComponent:oldConsoleFilename];
NSString *lastPath = [documentsDirectory stringByAppendingPathComponent:lastConsoleFilename];
NSError *err;
[[NSFileManager defaultManager] removeItemAtPath:path error:&err ];
[[NSFileManager defaultManager] moveItemAtPath:lastPath toPath:path error:&err ];
consoleLog = freopen([lastPath cStringUsingEncoding:NSASCIIStringEncoding], "w", stderr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment