IOS 调试日志输出

+(void)logMessageByUUID:(NSString *)UUID{

    

    NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);

    NSFileManager * filemangage =[NSFileManager defaultManager];

    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [paths objectAtIndex:0];

    

    [filemangage changeCurrentDirectoryPath:[documentsDir stringByExpandingTildeInPath]];

    

    NSString *fileName =[NSString stringWithFormat:@"%@.txt",[KBDateTimeUtil longlongyyyyMMddHHmmss:[NSDate date]]];

    

    [filemangage createFileAtPath:fileName contents:nil attributes:nil];

    

    NSString * fullfilename=[documentsDir stringByAppendingPathComponent:fileName];

    

    NSLog(@"%@",fullfilename);

    

    const  char* pcpath = [fullfilename cStringUsingEncoding:NSASCIIStringEncoding];

    

    stderr=fopen(pcpath, "w+");

}

这样就会吧debug写入一个文件,在测试过程中出现bug 把手机中得日志倒出来就可以查看日志了。


当然在plist文件中新加 Application supports iTunes file sharing 设置成YES  


这样就可以调试了。



对于stderr 文件的解释来自


重定向 stderr 到文件,方便查找 bug

为某个外部附件开发应用程序的情况下,开发者无法用 Xcode 进行调试或者用 GDB 查看 NSLog 的输出,定位和查找 bug 变得非常困难。好在我们可以通过重定向,将标准的错误输出stderr定向到文件。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                            NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [paths objectAtIndex:0];
    NSString *logPath = [documentDir stringByAppendingPathComponent:@"decrypt.log"];
    freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "w+", stderr);

    让我们看下这段代码,它的作用就是在程序内部的文件夹 Documents 中新建一个名为 decrypt.log 的文件,并且将所有的 stderr 输入到 decrypt.log
文件中。如此一来所有关于 stderr 的输出都将被重定向到 decrypt.log,在 gdb 中你将无法再看到这些信息。

    选择 Xcode->Windows->Organizer,找到你的设备,在 summary 一栏中找到你的程序,将其展开,点选右边的箭头,这样程序内部的所有文件夹都将导出到你所指定的位置。Organizer 左边一栏的 PROJECTS&SOURCES 展开便能找到这个文件。

关于 printf, fprintf 和 NSLog:

    可能你大量使用了外部的 c 库,它们以 printf 或者 fprintf 来输出信息 ,那么它们的区别又是什么?标准的 printf 是将调试信息输入到标准输出流 stdout,也就是说你用上面提供的函数是无法将此重定向的,你必须

reopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "w+", stdout);

    而 fprintf 则可以指定你所要输出的调试信息的位置,包括 stdout、stderr 甚至是一般的文件。这里我们输出到 stderr

fprintf(stderr, "hello\n");

    NSLog 本质上其实也是将调试信息输出到 stderr,但与 fprintf 不同它还有些不一样的特性。我们在使用 Xcode 进行调试的时候无论采用哪种方式输出错误信息总是第一时间刷新在屏幕上,但定向到文件却有所不同。NSLog 总是能第一时间写到文件,而 printf 或 fprintf 似乎将是信息保存在缓存中,等待某种契机或者信号才会将信息写到文件中。如此一来辛苦准备的调试信息却可能因为程序的崩溃而什么也无法看到。所以我们可以通过 fflush 来强制它进行刷新。如下,我们强制刷新 stderr












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值