博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS——生成智能调试输出
阅读量:7054 次
发布时间:2019-06-28

本文共 1557 字,大约阅读时间需要 5 分钟。

hot3.png

对于基本的log输出,NSLog函数很方便,但它既不是有条件的,又不太容易定制。我们将创建一个log输出函数,它能对每次输出惊醒精准控制,还可以在非调试的配制下关闭输出。

多余的log输出会影响性能,还会减少设备的固态硬盘的寿命,

具体代码如下:

XYDebug.h

#import 
#ifdef XYDEBUG#define XYLog(format...) XYDebug(__FILE__, __LINE__, format)#else#define XYLog(format...)#endifvoid XYDebug(const char *fileName, int lineNumber, NSString *fmt, ...);

XYDebug.m

#import "XYDebug.h"void XYDebug(const char *fileName, int lineNumber, NSString *fmt, ...){        va_list args;    va_start(args, fmt);        static NSDateFormatter *debugFormatter = nil;    if (debugFormatter == nil) {        debugFormatter = [[NSDateFormatter alloc] init];        [debugFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    }        NSString *msg = [[NSString alloc] initWithFormat:fmt arguments:args];    NSString *filePath = [[NSString alloc] initWithUTF8String:fileName];    NSString *timestamp = [debugFormatter stringFromDate:[NSDate date]];        NSDictionary *info = [[NSBundle mainBundle] infoDictionary];    NSString *appName = [info objectForKey:(NSString *)kCFBundleNameKey];    fprintf(stdout, "%s %s[%s:%d] %s\n",            [timestamp UTF8String],            [appName UTF8String],            [[filePath lastPathComponent] UTF8String],            lineNumber,            [msg UTF8String]);        va_end(args);    [msg release];    [filePath release];}
使用方式:

在预编译文件中加入#import "XYDebug"

在build setting中设置如下图

将Debug设置成-DXYDEBUG。这个标志只添加在Debug配置中,而没有添加到Release配置中,这个标志会激活相应的XYLog宏定义,从而激活或禁用log输出。如果使用“Other C Flags”设定项目,一定要加上“-D”。

原文件  ——

转载于:https://my.oschina.net/orangef/blog/145063

你可能感兴趣的文章
做一个座右铭工具每天激励自己
查看>>
Jenkins安装配置
查看>>
vmware12下对虚拟机ubuntu14.10系统所在分区sda1进行磁盘扩容
查看>>
EJB到底是什么,真的那么神秘吗??
查看>>
UI开发工具
查看>>
广义表 (五)
查看>>
Swift中NSTimer定时器的使用
查看>>
本周游戏推荐:暗影格斗2:shandow fight 2
查看>>
/var/log目录下的20个Linux日志文件功能详解
查看>>
我的友情链接
查看>>
去除中国菜刀密码的方法
查看>>
PHP下载断点续传 转
查看>>
【新手】【转】如何学习java程序设计
查看>>
企业邮箱发送不到sina、hotmail等解决
查看>>
如果finalize()抛出异常会怎样?
查看>>
自己的微博
查看>>
php取整函数ceil,floor,round,intval函数的区别
查看>>
NH5-Nhibernate映射中的三种关系
查看>>
SpringBoot项目启动时自动执行指定方法
查看>>
设计模式(行为型模式)——命令模式(Command)
查看>>