前言
在平时自己调试的时候,可以直接连接电脑,直接在窗口中查看结果。但是在测试人员测试,或者灰度测试的时候,怎么才能拿到日志呢?最先想到的肯定是输出到本地文件,然后在需要的时候进行上传。
分享一段之前找到的方法,下面的代码提供了两个主要功能:
– 把日志输出到文件中
– 捕捉异常信息
【解析都写在注释中了】
示例代码
- (void)redirectNSLogToDocumentFolder
{
//如果已经连接Xcode调试则不输出到文件
//该函数用于检测输出 (STDOUT_FILENO) 是否重定向 是个 Linux 程序方法
if(isatty(STDOUT_FILENO)) {
return;
}
// 判断 当前是否在 模拟器环境 下 在模拟器不保存到文件中
UIDevice *device = [UIDevice currentDevice];
if([[device model] hasSuffix:@"Simulator"]){
return;
}
//将NSlog打印信息保存到Document目录下的Log文件夹下
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *logDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Log"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL fileExists = [fileManager fileExistsAtPath:logDirectory];
if (!fileExists) {
[fileManager createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil 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 = [logDirectory stringByAppendingFormat:@"/%@.log",dateStr];
// 将log输入到文件
freopen([logFilePath cStringUsingEncoding:NSUTF8StringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSUTF8StringEncoding], "a+", stderr);
//未捕获的Objective-C异常日志
NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
}
之前看的时候,对 NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler) 这个用法一知半解,去翻了一下源码,这个方法是在 Foundation 中。
api 中的定义是Changes the top-level error handler ,Sets the top-level error-handling function where you can perform last-minute logging before the program terminates. 通过替换掉最高级别的 handle 方法,可以在程序终止之前可以获取到崩溃信息,并执行相应的操作,比如保存本地,或者上报。
方法调用为:
void NSSetUncaughtExceptionHandler(NSUncaughtExceptionHandler *);
传入的是一个 NSUncaughtExceptionHandler 的指针。
typedef void NSUncaughtExceptionHandler(NSException *exception);










