最近在模擬器上面測試SDK,想看看模擬器斷開xcode除錯的日誌輸出,發現Xcode-Window-Devices裡面並不能看到。
把日誌重定向輸出到log檔案,便可以看到實時的log日誌。
重定向日誌輸出程式碼:
1 2 3 4 5 6 7 8 9 10 |
-(void)redirectConsoleLog{ #ifdef DEBUG NSString *documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"documentPath : %@",documentDir); //重定向NSLog NSString* logPath = [documentDir stringByAppendingPathComponent:@"console.log"]; freopen([logPath fileSystemRepresentation], "a+", stderr); #endif } |
然後在AppDelegate裡面新增這個方法。
1 2 3 4 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self redirectConsoleLog]; } |
執行Xcode,得到document的目錄,然後定位到該目錄下面。
1 2 3 |
documentPath : /Users/Monkey/Library/Developer/CoreSimulator/Devices/EC652E7F-BEAD-4153-9F56-7DC025D4050C/data/Containers/Data/Application/3741A5B7-DF67-4872-B0F2-4E6125C27E82/Documents cd /Users/Monkey/Library/Developer/CoreSimulator/Devices/EC652E7F-BEAD-4153-9F56-7DC025D4050C/data/Containers/Data/Application/3741A5B7-DF67-4872-B0F2-4E6125C27E82/Documents |
使用tail –f console.log實時檢視。也可以使用tail –f console.log | grep “xxx”,進行過濾。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
cd /Users/Monkey/Library/Developer/CoreSimulator/Devices/EC652E7F-BEAD-4153-9F56-7DC025D4050C/data/Containers/Data/Application/3741A5B7-DF67-4872-B0F2-4E6125C27E82/Documents tail -f console.log 2016-03-11 11:20:43.430 BugrptSDKDemo[33376:6921912] <WORKFLOW>: sdk catch a exception 2016-03-11 11:20:43.922 BugrptSDKDemo[33376:6921912] <Info> Bugrpt: catch a fatal signal:6 [6,0,0] 2016-03-11 11:20:43.922 BugrptSDKDemo[33376:6921912] <Debug> Bugrpt: crash count is: 1 2016-03-11 11:20:43.922 BugrptSDKDemo[33376:6921912] <Debug> Bugrpt: CrashData is sessionEvent:( "sdk[1.1.2] handle signal:6 info(6,0,0)" ) crashTime:1457666443431 handler:Signal Handler type:NSRangeException(SIGABRT) error:*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2] address:0x108e1a0ae crashThread:1803 lastExceptionInfo: name:(null) arch:(null) uuid:(null) addr:(null) bininfos:(null) crashStackNames:(null) |
Log列印:
NSLog(@”this is my log”);
fprintf(stderr,”%s\n”,”this is my log”);