ios日志采集

ios日志采集,第1张

stdio(standard input &output)(标准输入输出)。iOS中,我们可以使用stdio类的方法来实现日志采集功能。

1.日志收集

logFilePath为自定义的文件名称路径,在保存文件时,因判断目录中是否存在同名文件,存在则先删除,以防重名。freopen函数用于重定向输入输出流,stdout(Standardoutput)表示标准输出,stderr(Standarderror)表示洞肢标准错误闭颤李。

2.自定义打印日志的NSLog

3使用DSLog打印日轿迟志

4.在AppDelegate.m的didFinishLaunchingWithOptions方法里调用logcollection方法,运行程序,则工程里的所有运行到的DSLog打印日志都会自动写入创建的.log文件中

5.获取log日志文件

在日常的开发中,由于某些原因在真机测试中日志无法保存,从而导致某些崩溃问题,又或者某些算法没有达到自己的预期,从而导致出现的问题巧瞎拍不好分析或者神坦无从下手。所以这时候就有必要将日志保存到应用的Docunment目录下,并设置成共享文件,这样有利于我们分析问题所在的原因。

例如:

#if TARGET_IPHONE_SIMULATOR

    NSLog(@"SIMULATOR DEVICE")

#else

    //export log to file

    freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout)//c printf

    freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr)//oc  NSLog

#endif

具体做法如下:

- ( void )startSaveLog {

    //file path

    NSString *documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES ) objectAtIndex:0]

    NSString*logPath = [documentDirPathstringByAppendingPathComponent:LogFileName]

    NSString*lastLogPath = [documentDirPathstringByAppendingPathComponent:LastLogFileName]

    NSError*error

    if ([[NSFileManager defaultManager] fileExistsAtPath:lastLogPath]) {

        [[NSFileManager defaultManager] removeItemAtPath:lastLogPath error: nil ]

    }

    if ([[NSFileManager defaultManager] fileExistsAtPath:logPath]) {

        if (![[NSFileManagerdefaultManager]moveItemAtPath:logPathtoPath:lastLogPatherror:&error]) {

            //delete exisist file

            [[NSFileManager defaultManager] removeItemAtPath:logPath error: nil ]

        }

    }

#if TARGET_IPHONE_SIMULATOR

    NSLog(@"SIMULATOR DEVICE")

#else

    //export log to file

    freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout)//c printf

    freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr)//oc  NSLog

#endif

}

此函数要在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

中调用,这个函数在AppDelegate.m中实现的。

        在应用程序的Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES。孝羡将您希望共享的文件放在应用程序的Documents目录。一旦设备插入到用户计算机,iTunes 9.1就会在选中设备的Apps标签中显示一个File Sharing区域。此后,用户就可以向该目录添加文件或者将文件移动到桌面计算机中。如果应用程序支持文件共享,当文件添加到Documents目录后,应用程序应该能够识别并做出适当响应。例如说,应用程序可以将新文件的内容显示界面上。请不要向用户展现目录的文件列表并询问他们希望对文件执行什么 *** 作。      

 就是说,一旦设备连接上电脑,可以通过iTune查看指定应用程序的共享文件夹,将文件拷贝到你的电脑上看。

当然也可以通过UIActivityViewController进行保存具体 *** 作如下:

- ( void )shareFileTxt {

    NSString *documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES ) objectAtIndex:0]

    NSString*logPath = [documentDirPathstringByAppendingPathComponent:LogFileName]

    NSString*lastLogPath = [documentDirPathstringByAppendingPathComponent:LastLogFileName]

    NSArray*fileURLArray

    if ([[NSFileManager defaultManager] fileExistsAtPath:logPath] &&[[NSFileManager defaultManager] fileExistsAtPath:lastLogPath]) {

        fileURLArray =@[[NSURLfileURLWithPath:logPath], [NSURLfileURLWithPath:lastLogPath]]

    } else if ([[NSFileManager defaultManager] fileExistsAtPath:logPath]) {

        fileURLArray =@[[NSURLfileURLWithPath:logPath]]

    } else if ([[NSFileManagerdefaultManager]fileExistsAtPath:lastLogPath]) {

        fileURLArray =@[[NSURLfileURLWithPath:lastLogPath]]

    } else {

        return

    }

    UIActivityViewController*activityController = [[UIActivityViewController alloc]initWithActivityItems:fileURLArrayapplicationActivities: nil ]   

 UIPopoverPresentationController*popover = activityController.popoverPresentationController

   if (popover) {

        popover.barButtonItem= sender

        popover.permittedArrowDirections = UIPopoverArrowDirectionUp

    }

  [ self presentViewController:activityController animated: YES completion: nil ]

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12386090.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存