通过自定义NSLog可以控制打印的格式
,以及可以实现 Debug 模式下打印输出,Release 模式不打印输出。
以下方式可加入打印的文件名
、函数名
、打印位置的行数
。
#if DEBUG
#define NSLog(FORMAT, ...) NSLog(@"File:%s Function:%s Line:%d ### %@\n", __FILE__,__FUNCTION__, __LINE__, [NSString stringWithFormat:FORMAT, ##__VA_ARGS__])
#else
#define NSLog(FORMAT, ...)
#endif
接下来我们把他应用到全局。首先项目中创建NSLog-Prefix.pch文件。将上面的代码复制到文件中。
#ifndef NSLog_Prefix_pch
#define NSLog_Prefix_pch
#if DEBUG
#define NSLog(FORMAT, ...) NSLog(@"File:%s Function:%s Line:%d ### %@\n", __FILE__,__FUNCTION__, __LINE__, [NSString stringWithFormat:FORMAT, ##__VA_ARGS__])
#else
#define NSLog(FORMAT, ...)
#endif
#endif /* NSLog_Prefix_pch */
在Build Setting
中设置
Precompile Prefix Header
为Yes,预编译后的 PCH 文件会被缓存起来,可以提高编译速度。
Prefix Header
填入项目名称/PCH 文件名
,然后运行即可。
image.png
Release模式不打印,Debug模式打印的效果如下。
image.png