没有调试器,Stdout重定向在iOS中不起作用
问题描述:
我正在尝试重定向输出,以便可以通过网络发送它.由于某种原因,如果您在调试器附加时运行代码,则它可以完美运行.以正常方式启动应用程序后,代码将冻结在读取函数上,并且永远不会返回.如果有人有任何指点,我将不胜感激.
I am trying to redirect output so I can send it over the network. For some reason if you run the code while debugger attached it works perfectly. Once you start the application in normal way the code freezes on the read function and never returns. If someone has any pointers I will highly appreciate it.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void) {
static int pipePair[2];
if ( pipe(pipePair) != 0) {
return;
}
dup2(pipePair[1],STDOUT_FILENO);
while (true) {
char * buffer = calloc(sizeof(char), 1024);
ssize_t readCount = read(pipePair[0],buffer,1023);
if (readCount > 0) {
buffer[readCount] = 0;
NSString * log = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
//sent it over network
}
if (readCount == -1) {
return;
}
}
});
答
显然,在iOS 5.1中,不允许向stdout写入数据. http://spouliot.wordpress.com/2012/03/13/ios-5-1-vs-stdout/
Apparently in iOS 5.1 writing to stdout was disallowed. http://spouliot.wordpress.com/2012/03/13/ios-5-1-vs-stdout/