iOS 列印日誌的儲存 (一)

來源:互聯網
上載者:User

標籤:style   blog   color   使用   檔案   os   

  當我們真機調試app的時候,作為開發人員的我們可以很方便的通過Xcode的debug area查看相關的列印資訊。而測試人員在對app進行測試的時候,一旦出現了crash,這時我們就需要把相關的列印資訊儲存下來,

以便後面進行查看追蹤crash原因。在這裡我們可以將列印資訊寫入沙箱系統中。不多說了,直接上代碼。

 1 - (void)redirectNSlogToDocumentFolder 2 { 3     if (isatty(STDOUT_FILENO)) 4     { 5         NSLog(@"真機調試,無需將列印資訊寫入檔案.\n"); 6         return; 7     } 8     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 9     NSString *documentDirectory = [paths objectAtIndex:0];10     NSString *fileName = [NSString stringWithFormat:@"PrintfInfo.log"];11     NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];12     // 先刪除已經存在的檔案13     NSFileManager *defaultManager = [NSFileManager defaultManager];14     [defaultManager removeItemAtPath:logFilePath error:nil];15     16     // 將log輸入到檔案17     freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);18     freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);19 }

同時我們在app啟動 didFinishLaunchingWithOptions 這個函數中調用這個方法。

1 //--------------------- 將列印資訊寫入沙箱檔案中 --------------------//2     UIDevice *device = [UIDevice currentDevice];3     if (![[device model] isEqualToString:@"iPhone Simulator"])4     {5         // 開始儲存記錄檔6         [self redirectNSlogToDocumentFolder];7     }8  //--------------------------------------------------------------//

測試人員可以去apple官網下載windows版iTunes,串連電腦後,開啟iTunes後點擊右上方的iPhone。在“應用程式”中找到自己的app,在其對應的文稿中可以找到“PrintfInfo.log”檔案夾。
直接拖出放在案頭使用文本編輯開啟即可查看相應的列印資訊了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.