NSLog()
函数在 Release 版本的影响
What will happen to NSLog statements in production?
资源消耗
在Xcode中开发时,往往需要利用NSLog()函数来打印一些调试信息。打印这些调试信息会消耗一定的系统资源,因为模拟器使用的是电脑的硬件,所以在运行应用时不会有什么影响,但如果在真实的移动设备上也打印大量的调试信息,会在很大程度上影响应用的性能。所以发布应用的正式版时,应屏蔽调试信息。
文档 Technical Note TN2347: Basic debugging using logging for Swift and Objective-C apps. 中有如下的相关描述:
Note:
NSLog
can take time to execute and this will add additional overhead to the runtime of your application (especially if you add lots and lots ofNSLog
statements to your app). During testing, this normally isn't a problem. But, for your final shipping product, it is better to remove all of the logging so your customers can enjoy faster performance without the logging going on. Because of this, it is a common practice among developers to use the DEBUG preprocessor macro to conditionally compile your logging code so that it only appears in your debug builds (and does not ship to your final customers).
那么在真机中NSLog()
函数具体是执行哪些操作从而耗费一部分资源呢,NSLog 函数的文档指出,该函数会将错误信息记录到 Apple System Log facility。
NSLog outputs messages to the Apple System Log facility or to the Console app (usually prefixed with the time and the process id).
通过文档 iOS Debugging Magic 中 Basics -> Seeing Debug Output 部分的描述得知,NSLog()
函数会将日志打印到 stderr,如果在真机上运行日志信息会重定向到 system.log,system.log 保存在 /var/log/system.log 文件中。
If you launch a GUI application as it would be launched by a normal user, the system redirects any messages printed on stderr to the system log. You can view these messages using the techniques described earlier.
所以NSLog()
函数是 Apple System Log(后面简称ASL)的封装,NSLog 会向 ASL 写 log,同时向 Terminal 写 log,而且同时会出现在 Console.app 中(Mac自带软件,用NSLog打出的log在其中全部可见);不仅如此,每一次NSLog 都会新建一个 ASL client 并向 ASL 守护进程发起连接,log 之后再关闭连接。所以说,当这个过程出现 N 次时,消耗大量资源导致程序变慢也就不奇怪了。
安全
NSLog()
函数输出的信息还有可能暴露应用的一些保密数据,通过 Xcode 中的设备选项打开控制台可查看真机的日志输出,而且其输出的日志信息并不在应用的沙盒内,导致可以被其他应用获取到。
On the iPhone, data passed to the NSLog function is logged by Apple System Log (ASL) and the data remains on the log until the device is rebooted. Also, Error logs are not bounded by the application sandbox. Which means error logs generated by one application can be read by other applications. Therefore, if an application logs sensitive data, a malicious application can actively query for this data and send it to a remote server.
Error logs on the iPhone can be viewed directly using Console app. The Console app is available in the AppStore. Error logs can also be viewed using iPhone configuration utility or by syncing the device with iTunes and looking at CrashReporter folder
禁用NSLog()
函数
针对以上问题,我们可以在预编译文件Prefix.pch中写一些宏来控制
#ifdef DEBUG
//__VA_ARGS__代表可变参数宏
#define DebugLog(format, ...) NSLog(format, ##__VA_ARGS__)
#else
#define DebugLog(format, ...)
#endif
这样,当只想在Debug版本中记录调试信息,可以使用DebugLog()函数。如果想在所有版本中都记录调试信息可以直接使用NSLog()函数。
PS:如果想全局替换NSLog()函数,可以在终端输入命令$ sed -i ".bak" 's/NSLog/DebugLog/' *.m,另外也可以直接在Xcode中全局替换。
如果不想使用NSLog输出的基本信息,也可以自定义输出的基本信息及其格式
// 获取系统当前毫秒级时间
#define logTime ({\
struct timeb currentTime;\
ftime(¤tTime);\
char secondLevelStr[9];\
strftime(secondLevelStr, 9, "%H:%M:%S", localtime(¤tTime.time));\
char millisecondLevelStr[13];\
sprintf(millisecondLevelStr, "%s.%03d", secondLevelStr, currentTime.millitm);\
millisecondLevelStr;\
})
#ifdef DEBUG
#define DebugLog(format, ...) fprintf(stderr, "%s %s[%d] %s\n", logTime, [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:format, ##__VA_ARGS__] UTF8String])
#else
#define DebugLog(format, ...)
#endif