NSLog()
函數(shù)在 Release 版本的影響
What will happen to NSLog statements in production?
資源消耗
在Xcode中開(kāi)發(fā)時(shí)懊纳,往往需要利用NSLog()函數(shù)來(lái)打印一些調(diào)試信息蚯嫌。打印這些調(diào)試信息會(huì)消耗一定的系統(tǒng)資源刻像,因?yàn)槟M器使用的是電腦的硬件兄旬,所以在運(yùn)行應(yīng)用時(shí)不會(huì)有什么影響,但如果在真實(shí)的移動(dòng)設(shè)備上也打印大量的調(diào)試信息,會(huì)在很大程度上影響應(yīng)用的性能。所以發(fā)布應(yīng)用的正式版時(shí),應(yīng)屏蔽調(diào)試信息诫惭。
文檔 Technical Note TN2347: Basic debugging using logging for Swift and Objective-C apps. 中有如下的相關(guān)描述:
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).
那么在真機(jī)中NSLog()
函數(shù)具體是執(zhí)行哪些操作從而耗費(fèi)一部分資源呢,NSLog 函數(shù)的文檔指出蔓挖,該函數(shù)會(huì)將錯(cuò)誤信息記錄到 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).
通過(guò)文檔 iOS Debugging Magic 中 Basics -> Seeing Debug Output 部分的描述得知,NSLog()
函數(shù)會(huì)將日志打印到 stderr瘟判,如果在真機(jī)上運(yùn)行日志信息會(huì)重定向到 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()
函數(shù)是 Apple System Log(后面簡(jiǎn)稱(chēng)ASL)的封裝拷获,NSLog 會(huì)向 ASL 寫(xiě) log篮撑,同時(shí)向 Terminal 寫(xiě) log,而且同時(shí)會(huì)出現(xiàn)在 Console.app 中(Mac自帶軟件匆瓜,用NSLog打出的log在其中全部可見(jiàn))赢笨;不僅如此,每一次NSLog 都會(huì)新建一個(gè) ASL client 并向 ASL 守護(hù)進(jìn)程發(fā)起連接驮吱,log 之后再關(guān)閉連接茧妒。所以說(shuō),當(dāng)這個(gè)過(guò)程出現(xiàn) N 次時(shí)左冬,消耗大量資源導(dǎo)致程序變慢也就不奇怪了桐筏。
安全
NSLog()
函數(shù)輸出的信息還有可能暴露應(yīng)用的一些保密數(shù)據(jù),通過(guò) Xcode 中的設(shè)備選項(xiàng)打開(kāi)控制臺(tái)可查看真機(jī)的日志輸出拇砰,而且其輸出的日志信息并不在應(yīng)用的沙盒內(nèi)梅忌,導(dǎo)致可以被其他應(yīng)用獲取到绊袋。
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()
函數(shù)
針對(duì)以上問(wèn)題,我們可以在預(yù)編譯文件Prefix.pch中寫(xiě)一些宏來(lái)控制
#ifdef DEBUG
//__VA_ARGS__代表可變參數(shù)宏
#define DebugLog(format, ...) NSLog(format, ##__VA_ARGS__)
#else
#define DebugLog(format, ...)
#endif
這樣铸鹰,當(dāng)只想在Debug版本中記錄調(diào)試信息,可以使用DebugLog()函數(shù)皂岔。如果想在所有版本中都記錄調(diào)試信息可以直接使用NSLog()函數(shù)蹋笼。
PS:如果想全局替換NSLog()函數(shù),可以在終端輸入命令$ sed -i ".bak" 's/NSLog/DebugLog/' *.m躁垛,另外也可以直接在Xcode中全局替換剖毯。
如果不想使用NSLog輸出的基本信息,也可以自定義輸出的基本信息及其格式
// 獲取系統(tǒng)當(dāng)前毫秒級(jí)時(shí)間
#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