Objective c 如何让NSFileHandle在命令行应用程序中报告可用数据?
我正在尝试创建一个Objective-C版本的东西,它的工作原理或多或少类似于TCL expect 这个进程启动一个子进程(在我的例子中是web服务器),然后我串行执行一系列选择器,这些选择器与我的web服务器交互。我的测试程序和HTTP服务器都会登录stdout和std err,因为我将我的Objective c 如何让NSFileHandle在命令行应用程序中报告可用数据?,objective-c,cocoa,nsfilehandle,Objective C,Cocoa,Nsfilehandle,我正在尝试创建一个Objective-C版本的东西,它的工作原理或多或少类似于TCL expect 这个进程启动一个子进程(在我的例子中是web服务器),然后我串行执行一系列选择器,这些选择器与我的web服务器交互。我的测试程序和HTTP服务器都会登录stdout和std err,因为我将我的NSTask设置如下: [launchServerNSTask setStandardOutput:[NSFileHandle fileHandleWithStandardOutput]]; [launch
NSTask
设置如下:
[launchServerNSTask setStandardOutput:[NSFileHandle fileHandleWithStandardOutput]];
[launchServerNSTask setStandardError:[NSFileHandle fileHandleWithStandardError]];
我对该程序唯一的不满是,我向sleep(1)发送了足够的调用,以便在运行测试之前启动服务器。理想情况下,我希望在STDOUT上发布服务器启动后开始运行测试。
为此,我为我的命令行程序创建了一个文件句柄,并要求它[pipeFileHandle waitForDataInBackgroundAndNotify]编码>并将其设置为:
NSFileHandle *pipeFileHandle
= [[NSFileHandle alloc] initWithFileDescriptor:[[pipe fileHandleForWriting] fileDescriptor]];
[launchServer setStandardOutput:pipeFileHandle];
[launchServer setStandardError:pipeFileHandle];
pipeFileHandle setReadabilityHandler:^(NSFileHandle *f){
NSError *internalError;
NSLog(@"WRITING TO PIPE FILE HANDLE");
/*** DETECT `Server started.` ***/
/*** Repipe to Stderr and Stdout ***/
}];
[launchServerNSTask setTerminationHandler:^(NSTask *task) {
NSLog(@"LAUNCH SERVER TASK TERMINATED!");
stopLoop = YES;
[tester endTesting];
}];
while (!stopLoop && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:loopUntil]) {
if (!runner) {
runner = [[SequentialTestRunner alloc] initWithFileHandle:pipeFileHandle];
NSLog(@"Did launch runner: %@", runner);
}
if (![launchServerNSTask isRunning]) {
NSLog(@"Launching server");
// This says it needs to be called from a thread with an active run-loop.
[pipeFileHandle waitForDataInBackgroundAndNotify];
NSLog(@"Pipe %@ waiting in background.", pipeFileHandle);
[launchServer launchAndReturnError:&internalError];
if (internalError) {
[internalError prettyPrint];
stopLoop = YES;
}
}
NSLog(@".");
loopUntil = [NSDate dateWithTimeIntervalSinceNow:0.5];
}
不幸的是,我没有得到任何数据,即使我认为我已经正确地设置了运行循环。我做错了什么?如何获得自定义文件句柄以像以前一样更新Stdout和Stderr