iOS 将NSLog写入到文件中

AppDelegate.m的** application: didFinishLaunchingWithOptions**中:

#if (DEBUG == 1 || TARGET_OS_SIMULATOR)
#else
#ifdef FILELOG_SUPPORT
    [self redirectNSlogToDocumentFolder];
#endif
#endif```

// 将NSlog打印信息保存到Document目录下的文件中

  • (void)redirectNSlogToDocumentFolder
    {
    //document文件夹
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    //
    NSString *foldPath = [documentDirectory stringByAppendingFormat:@"/appLog"];

//文件保护等级
NSDictionary *attribute = [NSDictionary dictionaryWithObject:NSFileProtectionNone
forKey:NSFileProtectionKey];
[[NSFileManager defaultManager] createDirectoryAtPath:foldPath withIntermediateDirectories:YES attributes:attribute error:nil];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //每次启动后都保存一个新的日志文件中
NSString *dateStr = [formatter stringFromDate:[NSDate date]];
NSString *logFilePath = [foldPath stringByAppendingFormat:@"/%@.log",dateStr];

[Utils checkFlieProtection:logFilePath];
// 将log输入到文件
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);

}


pragma mark -File Handel

  • (void)checkFlieProtection:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *pathSqlite = path;
    NSDictionary *attributeSql = [fileManager attributesOfItemAtPath:pathSqlite error:nil];
    if ([[attributeSql objectForKey:NSFileProtectionKey] isEqualToString:NSFileProtectionComplete]) {
    NSDictionary *attribute = [NSDictionary dictionaryWithObject:NSFileProtectionCompleteUntilFirstUserAuthentication
    forKey:NSFileProtectionKey];
    [fileManager setAttributes:attribute ofItemAtPath:pathSqlite error:nil];
    NSLog(@"改变文件权限 %@ : %@",path,attribute);
    }
    }

### Tips
- 文件保护等级属性列表  

NSFileProtectionNone //文件未受保护,随时可以访问 (Default)
NSFileProtectionComplete //文件受到保护,而且只有在设备未被锁定时才可访问
NSFileProtectionCompleteUntilFirstUserAuthentication //文件收到保护,直到设备启动且用户第一次输入密码
NSFileProtectionCompleteUnlessOpen //文件受到保护,而且只有在设备未被锁定时才可打开,不过即便在设备被锁定时,已经打开的文件还是可以继续使用和写入


- freopen
[freopen()函数](http://c.biancheng.net/cpp/html/2508.html)用于文件流的的重定向,一般是将 stdin、stdout 和 stderr 重定向到文件。
所谓重定向,就是改变文件流的源头或目的地。stdout(标准输出流)的目的地是显示器,printf()是将流中的内容输出到显示器;可以通过freopen()将stdout 的目的地改为一个文件(如output.txt),再调用 printf(),就会将内容输出到这个文件里面,而不是显示器。 freopen()函数的原型为:    FILE *freopen(char *filename, char *type, FILE *stream);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • __block和__weak修饰符的区别其实是挺明显的:1.__block不管是ARC还是MRC模式下都可以使用,...
    LZM轮回阅读 8,685评论 0 6
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,307评论 19 139
  • 史上最全的iOS面试题及答案 迷途的羔羊--专为路痴量身打造的品牌。史上最精准的定位。想迷路都难!闪电更新中......
    南虞阅读 5,415评论 0 8
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,178评论 2 33
  • 通过这一段时间对于马克思原理的学习,我明白到了许多以前未曾想到的东西。马克思主义是由一系列的基本理论、基本观点和基...
    Enfield阅读 3,060评论 0 6