2014/03/26

IOS 將 console 寫成 file


在 app 測試時,常會出現一些無法重現的問題。這時工程師會習慣查看 console,但總不能把手機一直接著電腦查看 console吧!若等到問題發生在馬上接電腦查看,也可能因為 log 過多被洗版而失去重要資訊。此時,若能將 console 寫成 file,來協助工程師 debug,對工程師來說將會輕鬆許多,也可讓一些無法重現的 bug 留下線索!

重新定義 NSLog


#define NSLog(FORMAT, ...) NSLog(@"%s %d %@",__FILE__, __LINE__, [NSString stringWithFormat:FORMAT, ##__VA_ARGS__]);NSLog
//印出範例:/ full package name / full class name  行數  訊息

write file


/**
 * 將 NSLog 寫成 file,存在 doc 下 (請用 iTools 開啟)
 */
- (void) redirectConsoleLogToDocumentFolder

{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate* date = [[NSDate alloc] init];
    NSString *fileName = [NSString stringWithFormat:@"%@%@", [dateFormatter stringFromDate:date], @".log" ];  //檔名為 yyyy-MM-dd.log
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *logPath = [documentsDirectory stringByAppendingPathComponent:fileName];//@"console.log"
    freopen([logPath fileSystemRepresentation],"a+",stderr);  //印出 NSLog,a代表讀取文字檔案將附加資料在檔案最後
    freopen([logPath fileSystemRepresentation],"a+",stdout);  //印出 print,a代表讀取文字檔案將附加資料在檔案最後
}
在 AppDelegate 的 didFinishLaunchingWithOptions 中呼叫

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
 [self redirectConsoleLogToDocumentFolder];  //呼叫寫入 log method
 //... other code ...
 return YES;
}
※ freopen 參數可參考 C 語言標準函數庫分類導覽 - stdio.h freopen()
※ 注意!若您將 console 寫入 file,那在 xcode 中,可能會看不到 console

執行結果

  • 執行前

2014-03-26 19:39:40.698 kokola[1847:60b] data.updateEmo:0
2014-03-26 19:39:40.703 kokola[1847:60b] loadStartupData OK
2014-03-26 19:39:41.026 kokola[1847:60b] responseString={"contact":{"genTime":"2014-03-26 19:39:39","grouplist":[],"userlist":[],"uglist":[]},"resultcode":"200","resultdesc":"Success."}
2014-03-26 19:39:41.050 kokola[1847:60b] 寫入公司通訊錄最後更新時間至UserDefaults:2014-03-26 19:39:39
2014-03-26 19:39:56.743 kokola[1847:60b] [ContactViewController]viewWillDisappear
2014-03-26 19:39:56.778 kokola[1847:60b] -[ChatHistoryViewController viewWillAppear:]
2014-03-26 19:39:58.721 kokola[1847:60b] didSelectRowAtIndexPath
2014-03-26 19:39:58.779 kokola[1847:60b] setSendBtn;
2014-03-26 19:39:59.025 kokola[1847:60b] [MainViewController]viewWillDisappear
2014-03-26 19:39:59.026 kokola[1847:60b] [ChatHistoryViewController]viewWillDisappear
2014-03-26 19:39:59.030 kokola[1847:60b] genChats
2014-03-26 19:39:59.045 kokola[1847:60b] unreadMkeys count:0
2014-03-26 19:39:59.070 kokola[1847:60b] setAllRead:4
  • 執行後

2014-03-26 19:33:49.917 kokola[1830:60b] /Users/james/sourcetree/kokola/kokola/business/impl/StartupBusinessImpl.m 124 data.updateEmo:0
2014-03-26 19:33:49.926 kokola[1830:60b] /Users/james/sourcetree/kokola/kokola/util/LongPollingClient.m 348 loadStartupData OK
2014-03-26 19:33:49.986 kokola[1830:60b] /Users/james/sourcetree/kokola/kokola/util/HttpUtil.m 290 responseString={"contact":{"genTime":"2014-03-26 19:33:49","grouplist":[],"userlist":[],"uglist":[]},"resultcode":"200","resultdesc":"Success."}
2014-03-26 19:33:50.012 kokola[1830:60b] /Users/james/sourcetree/kokola/kokola/business/impl/OrgContactBusinessImpl.m 189 寫入公司通訊錄最後更新時間至UserDefaults:2014-03-26 19:33:49
2014-03-26 19:33:50.173 kokola[1830:60b] Could not load the "btn_topbar1_pressed.png" image referenced from a nib in the bundle with identifier "com.maxkit.kokola"
2014-03-26 19:33:50.251 kokola[1830:60b] /Users/james/sourcetree/kokola/kokola/view/ContactViewController.m 178 [ContactViewController]viewWillAppear
2014-03-26 19:33:50.313 kokola[1830:60b] /Users/james/sourcetree/kokola/kokola/view/LoginViewController.m 410 app.longPollingClient=
2014-03-26 19:33:51.921 kokola[1830:60b] /Users/james/sourcetree/kokola/kokola/view/ContactViewController.m 207 [ContactViewController]viewWillDisappear
2014-03-26 19:33:51.956 kokola[1830:60b] /Users/james/sourcetree/kokola/kokola/view/ChatHistoryViewController.m 86 -[ChatHistoryViewController viewWillAppear:]

References

沒有留言:

張貼留言