码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 把NSLog打印到文件的方法

时间:2015-04-01 13:09:26      阅读:108      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!