Created
August 28, 2014 07:59
-
-
Save lihnux/ee78a25677afc6a154ed to your computer and use it in GitHub Desktop.
Redirecting NSLog to a file
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
- (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