Stdout redirection not working in iOS without debugger

允我心安 提交于 2019-12-08 06:15:32

问题


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;
                }
            }
        });

回答1:


Apparently in iOS 5.1 writing to stdout was disallowed. http://spouliot.wordpress.com/2012/03/13/ios-5-1-vs-stdout/



来源:https://stackoverflow.com/questions/10884913/stdout-redirection-not-working-in-ios-without-debugger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!