获取沙盒日志

日志

920 发布: 2018/5/22 02:21 本文总阅读量

取沙盒日志的方法

1.手机连接Mac电脑,打开Xcode

2.选择顶栏的 Windows 下的 Devices

3.在 Devices 栏下先选择当前连接的手机设备。

4.在 Installed Apps 栏下选择你开发的App(需要取沙盒文件的App)

5.点击底部 齿轮图案 的按钮,选择 Download Container…

将输出日志(NSLog)写入沙盒文件

#pragma mark - 自定义方法,将nslog的输出信息写入到NSLog.txt文件中
- (void)nslogToFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"NSLog.txt"];// 注意不是NSData!
    
    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
    // 先删除已经存在的文件
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    [defaultManager removeItemAtPath:logFilePath error:nil];
    
    // 将log输入到文件
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
    
    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
    
}

补充[2021-8-4] Log 写入文件【此方式控制台打印+控制台输出】

上述nslogToFile调用后,控制台将不能打印相关日志:

注意引入头文件 #import <sys/time.h>


+(void)writeToFile:(NSString *)name andInfo:(id )info {
    RKWriteToFile(name, info);
}

void RKWriteToFile(NSString *name, NSString *format, ...) {
    va_list L;
    va_start(L, format);
    NSString *message = [[NSString alloc] initWithFormat:format arguments:L];
    message = [name stringByAppendingFormat:@" => %@",message];
    NSLog(@"%@", message);
    // 开启异步子线程,将打印写入文件
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSFileHandle *output = RKLogFileHandle();
        if (output != nil) {
            time_t rawtime;
            struct tm timeinfo;
            char buffer[64];
            time(&rawtime);
            localtime_r(&rawtime, &timeinfo);
            struct timeval curTime;
            gettimeofday(&curTime, NULL);
            int milliseconds = curTime.tv_usec / 1000;
            strftime(buffer, 64, "%Y-%m-%d %H:%M", &timeinfo);
            char fullBuffer[128] = { 0 };
            snprintf(fullBuffer, 128, "%s:%2d.%.3d ", buffer, timeinfo.tm_sec, milliseconds);
            [output writeData:[[[NSString alloc] initWithCString:fullBuffer encoding:NSASCIIStringEncoding] dataUsingEncoding:NSUTF8StringEncoding]];
            [output writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
            static NSData *returnData = nil;
            if (returnData == nil)
                returnData = [@"\n" dataUsingEncoding:NSUTF8StringEncoding];
            [output writeData:returnData];
        }
    });
    va_end(L);
}
static NSFileHandle *RKLogFileHandle(){
    static NSFileHandle *fileHandle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];;
        NSString *currentFilePath = [documentsDirectory stringByAppendingPathComponent:@"application-0.log"];
        NSString *oldestFilePath = [documentsDirectory stringByAppendingPathComponent:@"application-60.log"];
        
        //60文件存在,删除
        if ([fileManager fileExistsAtPath:oldestFilePath]){
            [fileManager removeItemAtPath:oldestFilePath error:nil];
        }
        //遍历文件,将文件编号往后移动,新增第0的一个文件
        for (int i = 60 - 1; i >= 0; i--) {
            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"application-%d.log", i]];
            NSString *nextFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"application-%d.log", i + 1]];
            if ([fileManager fileExistsAtPath:filePath]) {
                [fileManager moveItemAtPath:filePath toPath:nextFilePath error:nil];
            }
        }
        [fileManager createFileAtPath:currentFilePath contents:nil attributes:nil];
        fileHandle = [NSFileHandle fileHandleForWritingAtPath:currentFilePath];
        [fileHandle truncateFileAtOffset:0];
    });
    return fileHandle;
}