moliyadi
11/19/2015 - 7:18 AM

将iOS的控制台输出存储到文件.md

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);
}