Skip to content

Instantly share code, notes, and snippets.

@moliyadi
Last active November 19, 2015 07:51
Show Gist options
  • Select an option

  • Save moliyadi/d949a705bf22e6e85c6c to your computer and use it in GitHub Desktop.

Select an option

Save moliyadi/d949a705bf22e6e85c6c to your computer and use it in GitHub Desktop.

Just add this block of code in applicationdidFinishLaunchingWithOptionslaunchOptions method in app delegate file, and it will create a log file in app Document Directory on iphone which logs all console log events. You need to import this file from itunes to see all console events.

if (!isatty(STDERR_FILENO)) {
        //判断控制台是否已连接,http://stackoverflow.com/questions/9619708/nslog-to-both-console-and-file
        //将控制台内容写入文件,http://stackoverflow.com/questions/2634929/objective-c-iphone-can-we-view-console-log-on-device
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        
        NSString *documentsDirectory = [paths objectAtIndex:0];
        
        NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
        
        NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
        
        freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment