没有调试器,Stdout重定向在iOS中不起作用 [英] Stdout redirection not working in iOS without debugger

查看:215
本文介绍了没有调试器,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/

这篇关于没有调试器,Stdout重定向在iOS中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆