ios 打全局的Log日志
2016-05-10 09:28
519 查看
具体做法:
// 将NSlog打印信息保存到Document目录下的文件中
- (void)redirectNSlogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"dr.log"];// 注意不是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);
}
在appdelegate.m中应用:
#if !TARGET_IPHONE_SIMULATOR
- (void)redirectNSLogToDocumentFolder{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
#endif
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息
NSString *reason = [exception reason];//非常重要,就是崩溃的原因
NSString *name = [exception name];//异常类型
NSString *crashLogInfo = [NSString stringWithFormat:@"exception type : %@ \n crash reason : %@ \n call stack info
: %@", name, reason, arr];
NSString *urlStr = [NSString stringWithFormat:@"mailto://1101023516@qq.com?subject=bug报告&body=感谢您的配合!"
"错误详情:%@",crashLogInfo];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
NSLog(@"crash : %@ \n",crashLogInfo);
}
在- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中:
// objective-c捕获异常
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
// 将NSlog打印信息保存到Document目录下的文件中
- (void)redirectNSlogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"dr.log"];// 注意不是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);
}
在appdelegate.m中应用:
#if !TARGET_IPHONE_SIMULATOR
- (void)redirectNSLogToDocumentFolder{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
#endif
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息
NSString *reason = [exception reason];//非常重要,就是崩溃的原因
NSString *name = [exception name];//异常类型
NSString *crashLogInfo = [NSString stringWithFormat:@"exception type : %@ \n crash reason : %@ \n call stack info
: %@", name, reason, arr];
NSString *urlStr = [NSString stringWithFormat:@"mailto://1101023516@qq.com?subject=bug报告&body=感谢您的配合!"
"错误详情:%@",crashLogInfo];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
NSLog(@"crash : %@ \n",crashLogInfo);
}
在- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中:
// objective-c捕获异常
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
相关文章推荐
- iOS开发XCODE5 SVN配置 使用办法 (转) 收藏一下
- 在iOS App中实现地理位置定位的基本方法解析
- Code Sign error: ad hoc code signing not allowed with SDK 'iOS 9.3'
- iOS开发————通信方式之KVO
- 【代码笔记】iOS-平面化的饼图
- 关于iOS截屏和保存图片
- iOS CGRectGetMaxX/Y 使用
- iOS应用崩溃日志分析
- iOS中的懒加载
- iOS单元测试
- iOS 终端统计Xcode工程代码量
- iOS中利用 runtime 一键改变字体
- IOS开发 清空数组正确方法
- IOS开发-当遇到tableView整体上移时的解决方案
- iOS中AVFoundation的简单使用—音乐的播放
- iOS自定义年月日
- [10秒学会] - iOS 3种常用形式block
- iOS中的触摸事件
- iOS中的push
- iOS中的手势