2018年6月12日 星期二

nslog to file

- (void)redirectLogToDocuments {
    NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [allPaths objectAtIndex:0];
    NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"logFile.txt"];
  
    if ([[NSFileManager defaultManager] fileExistsAtPath:pathForLog]) {
        NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:pathForLog error:&error];
        if (error!=nil) {
            NSLog(@"%@", error.localizedDescription);
        }
    }
    freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}

沒有留言:

張貼留言