IOS寫日誌檔案並儲存到Documents

2021-10-05 01:20:06 字數 4404 閱讀 6696

unity開發ios版的時候常要借助xcode除錯**

情景1情景2

情景。。。

方法一

說明

在objective-c開發程式的時候,有專門的日誌操作類nslog,它將指定的輸出,輸出到(stderr),我們可以利用xcode的日誌輸出視窗,那麼既然是要記錄到具體日誌檔案,我們就想輸出日誌寫入到具體的日誌檔案即可。

**

1、  巨集定義(下面是我在程式中常用到的日誌巨集,用debug開關管理,

也就是說只有在debug模式下才讓日誌輸出 :)

#ifdef debug  

#  define log(fmt, ...) do  while(0)  

#  define log_method nslog(@"%s", __func__)  

#  define log_cmethod nslog(@"%@/%@", nsstringfromclass([self class]), nsstringfromselector(_cmd))  

#  define count(p) nslog(@"%s(%d): count = %d\n", __func__, __line__, [p retaincount]);  

#  define log_trace(x) do  while (0)  

#else  

#  define log(...)  

#  define log_method  

#  define log_cmethod  

#  define count(p)  

#  define log_trace(x)  

#endif

可以看到,除了標準的使用者定義輸出外,我還加入了許多有用的資訊,

比如源程式檔案位置,行號,類名,函式名等。具體的應用可以在具體的開發過程中新增、刪除。

2、  應用:

- (void)redirectnslogtodocumentfolder  

// 真機測試時儲存日誌  

if ([cdeviceinfo getmodeltype] != simulator)    }

真機測試的時候,可以利用freopen將標準錯誤輸出儲存到指定的檔案當中,

這樣就可以在問題發生後分析日誌檔案。

3、 設定debug標誌是否正確定義

xcode 一般會在 debug 執行配置項裡面已經定義號了debug 標誌,如果沒定義我們就自己寫上,以我的 xcode 4 為例,在專案get info中找到 preprocessor macros 這個屬性,對於 debug 配置我們給他寫上 debug,而在 release 配置中把它留空。 這樣我們剛才那段預處理命令就可以根據這個標誌來判斷我們編譯的時除錯版本還是發布版本,從而控制 nslog 的輸出。 (因為 xcode 4會把 debug/release 兩個配置項同時對比展現出來,而 3.x 版本的只能分別設定,如果你用的時xcode 3.x 開發工具, 那麼就分別對 debug/release 都檢查一下)。

其他

iphone開發技巧之日誌儲存教程

iphone應用根據debug和release狀態變化來遮蔽日誌輸出

方法二ios - nslog、uncaughtexception日誌儲存到檔案

對於真機,日誌沒法儲存,不好分析問題。所以有必要將日誌儲存到應用的docunment目錄下,方便取出分析。

首先是日誌輸出,分為c的printf和標準的nslog輸出,printf會向標準輸出(sedout)列印,而nslog則是向標準出錯(stderr),我們需要同時讓他們都將日誌列印到乙個檔案中。 其次是crash問題;crash分為兩種,一種是由exc_bad_access引起的,原因是訪問了不屬於本程序的記憶體位址,有可能是訪問已被釋放的記憶體;另一種是未**獲的objective-c異常(n***ception),導致程式向自身傳送了sigabrt訊號而崩潰。其實對於未捕獲的objective-c異常,我們是有辦法將它記錄下來的,如果日誌記錄得當,能夠解決絕大部分崩潰的問題。

//連線xcode時可以從監視器中看日誌 沒連線時log日誌會輸出到檔案中,  

[self redirectnslogtodocumentfolder];  

- (void)redirectnslogtodocumentfolder  

uidevice *device = [uidevice currentdevice];  

if([[device model] hassuffix:@"simulator"])  

//將nslog列印資訊儲存到document目錄下的log資料夾下  

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);  

nsfilemanager *filemanager = [nsfilemanager defaultmanager];  

bool fileexists = [filemanager fileexistsatpath:logdirectory];  

if (!fileexists)   

nsdateformatter *formatter = [[nsdateformatter alloc] init];  

[formatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"zh_cn"]];  

[formatter setdateformat:@"yyyy-mm-dd hh:mm:ss"]; //每次啟動後都儲存乙個新的日誌檔案中  

nsstring *datestr = [formatter stringfromdate:[nsdate date]];  

// 將log輸入到檔案  

freopen([logfilepath cstringusingencoding:nsasciistringencoding], "a+", stdout);  

freopen([logfilepath cstringusingencoding:nsasciistringencoding], "a+", stderr);  

//未捕獲的objective-c異常日誌  

nssetuncaughtexceptionhandler (&uncaughtexceptionhandler);  

}    

void uncaughtexceptionhandler(n***ception* exception)  

//將crash日誌儲存到document目錄下的log資料夾下  

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);  

nsfilemanager *filemanager = [nsfilemanager defaultmanager];  

if (![filemanager fileexistsatpath:logdirectory])   

nsdateformatter *formatter = [[nsdateformatter alloc] init];  

[formatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"zh_cn"]];  

[formatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];  

nsstring *datestr = [formatter stringfromdate:[nsdate date]];  

nsstring *crashstring = [nsstring stringwithformat:@"<- %@ ->[ uncaught exception ]\r\nname: %@, reason: %@\r\n[ fe symbols start ]\r\n%@[ fe symbols end ]\r\n\r\n", datestr, name, reason, strsymbols];  

//把錯誤日誌寫到檔案中  

if (![filemanager fileexistsatpath:logfilepath]) else  

//把錯誤日誌傳送到郵箱  

錯誤詳情:

%@",crashstring ];  

//    nsurl *url = [nsurl urlwithstring:[urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];  

}  

Android輸出日誌Log類並儲存到檔案中

android.util.log常用的方法有以下5個 log.v log.d log.i log.w 以及 log.e 根據首字母分別對應verbose,debug,info,warn,error。1 log.v 的除錯顏色為黑色的,任何訊息都會輸出,這裡的v代表verbose囉嗦的意思,平時使用就...

Android輸出日誌Log類並儲存到檔案中

android.util.log常用的方法有以下5個 log.v log.d log.i log.w 以及 log.e 根據首字母分別對應verbose,debug,info,warn,error。1 log.v 的除錯顏色為黑色的,任何訊息都會輸出,這裡的v代表verbose囉嗦的意思,平時使用就...

Scrapy爬取並儲存到TXT檔案

在建立完成專案並建立爬蟲的基礎上,編寫儲存到txt的專案 1.將 robotstxt obey 設定為false 2.將 item pipelines 開啟 item是scrapy提供的類似於字典型別的資料容器,它與字典最大的區別在於它規定了統一的資料規格樣式,即具有統一性與結構性。這樣既方便資料的...