iOS随笔小记

iOS 保存打印的log日志

2020-01-03  本文已影响0人  七一小月

有些时候测试,需要通过log日志来调式代码,发现问题
可以将打印的log日志保存在APP本地文件中

 //Save NSlog print information to a file in the Document directory
- (void)redirectNSlogToDocumentFolder{
    UIDevice *device = [UIDevice currentDevice];
    if ([[device model] isEqualToString:@"Simulator"]) {
        return;
    }

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory = [paths objectAtIndex:0];

    NSString *fileName = [NSString stringWithFormat:@"test.log"];

    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];

    // Delete existing files
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    [defaultManager removeItemAtPath:logFilePath error:nil];

    //Enter the log into the file
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);

}

通过iTunes下载log文件


保存log日志.png
上一篇 下一篇

猜你喜欢

热点阅读