重寫系統NSLog 巨集定義

2021-09-02 15:58:04 字數 816 閱讀 8377

在程式執行過程中,通常會列印一些資訊來幫助我們跟蹤執行定位,但是程式中過多的nslog在執行過程中是非常耗記憶體的,然而使用者在使用時並需要這些追蹤資訊。

弊端:1、消耗手機執行記憶體;

2、不可避免的洩露一些敏感資訊;

//重寫nslog,debug模式下列印日誌和當前行數

#if debug

#define nslog(format, ...) do while (0)

#define nslogrect(rect) nslog(@"%s x:%.4f, y:%.4f, w:%.4f, h:%.4f", #rect, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)

#define nslogsize(size) nslog(@"%s w:%.4f, h:%.4f", #size, size.width, size.height)

#define nslogpoint(point) nslog(@"%s x:%.4f, y:%.4f", #point, point.x, point.y)

#else

#define nslog(format, ...) nil

#define nslogrect(rect) nil

#define nslogsize(size) nil

#define nslogpoint(point) nil

#endif

#endif

【注】在使用上述重寫方式,要確保如下中引數一致

中的"debug"與**中的

"debug"統一。

巨集定義重寫NSLog

define debug 巨集輸出函式 ifdef debug define mylog fmt,fprintf stderr,s d行 s n nsstring stringwithutf8string file lastpathcomponent utf8string line nsstring...

利用巨集定義控制nslog 的輸入輸出

在專案的過程中,經常遇到要在除錯的時候列印log,但是上線或是release 的時候不需要去顯示log 的情況,此時你辛辛苦苦寫了那麼多的log,你要麼就手動注釋掉,要麼就是設乙個開關變數,企圖用這個總開關開啟。其實可以充分利用巨集定義進行設定。步驟比較簡單,只需要 在 projectname pr...

ios開發 巨集定義實現NSLog不列印設定

我們可以在發布版本前先把所有nslog語句注釋掉,等以後要除錯時,再取消這些注釋,這實在是一件無趣而耗時的事!還好,還有更優雅的解決方法,就是在專案的prefix.pch檔案裡加入下面一段 加入後,nslog就只在debug下有輸出,release下不輸出了。在 prefix.pch pch全稱是 ...