Friday 4 April 2014

Redirect NSLog to file.


Step 1: Include following function in AppDelegate:


- (void)redirectNSLogToDocumentFolder
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *fileName =[NSString stringWithFormat:@"NSLogFile"];
    
    NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
    NSLog(@"Redirecting NSLog to file");
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
    

}




Step 2: Call this function at the start of function applicationDidFinishLaunchingWithOptions...


Thats it, Every NSLog() will now get redirected to this console.log file, which you can find in the documents directory.

No comments:

Post a Comment