Skip to content

Instantly share code, notes, and snippets.

@lihnux
Created August 28, 2014 07:59
Show Gist options
  • Save lihnux/ee78a25677afc6a154ed to your computer and use it in GitHub Desktop.
Save lihnux/ee78a25677afc6a154ed to your computer and use it in GitHub Desktop.
Redirecting NSLog to a file
- (BOOL)redirectNSLog {
// Create log file
[@"" writeToFile:@"/NSLog.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
id fileHandle = [NSFileHandle fileHandleForWritingAtPath:@"/NSLog.txt"];
if (!fileHandle) {
NSLog(@"Opening log failed");
return NO;
}
[fileHandle retain];
// Redirect stderr
int err = dup2([fileHandle fileDescriptor], STDERR_FILENO);
if (!err) {
NSLog(@"Couldn't redirect stderr");
return NO;
}
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment