ios 打全局的Log日志

具体做法:

 // 将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);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

午夜小学徒丶磊

你得鼓励是我最大的动力,谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值