iOS安全–实时查看模拟器的日志输出

最近在模拟器上面测试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”);

本文链接:http://www.blogfshare.com/redirect-console-log.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>