首页 > 移动平台 > 详细

iOS 把NSLog打印到文件的方法

时间:2015-04-01 13:07:24      阅读:280      评论:0      收藏:0      [点我收藏+]

使用以下代码可以把console打印处理的log都保存到文件

 

+ (void)redirectNSLogToDocumentFolder{
    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);
}

nslog输出都是stderr

还可以把输出stdout, 这个可以输出printf()打印的内容

需要在调NSLog之前调用上面这段代码才能将输出的内容保存到沙盒Document文件夹里面

iOS 把NSLog打印到文件的方法

原文:http://www.cnblogs.com/huangzizhu/p/4383336.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!