Question

I set up an NSNotification for NSFileHandleReadCompletionNotification.

I set up the standard I/O with two separate pipes.

NSPipe * input  = NSPipe.new;
NSPipe * output = NSPipe.new;

[serverTask setStandardInput:input];
[serverTask setStandardOutput:output];

I launch a NSTask executing a Java jar, and start reading the data.

[[serverTask.standardOutput fileHandleForReading] readInBackgroundAndNotify];

and I continuously read the data and append the data in an NSTextView if it is new data:

- (void)serverLogHasChanged:(NSNotification *)notification
{
    [[serverTask.standardOutput fileHandleForReading] readInBackgroundAndNotify];

    NSData * newData = [notification.userInfo objectForKey:NSFileHandleNotificationDataItem];

    if (newData != nil && availableData != newData)
    {
        NSMutableString * serverLogString = [NSMutableString stringWithString:serverLog.string];

        [serverLogString appendString:[NSString.alloc initWithData:newData encoding:NSUTF8StringEncoding]];
        [serverLog setString:serverLogString];
        [serverLog.enclosingScrollView.contentView scrollPoint:NSMakePoint(0, NSMaxY(serverLog.enclosingScrollView.contentView.bounds))];
    }

    newData = availableData;
}

However, I'm getting strange output into the NSTextView

enter image description here

Those ">" characters should be lines of actual output, but instead the output ends up in Xcode's console.

In other words, the console is printing out the output instead of my NSPipe which is printing out only the indication of new lines of the output.

Was it helpful?

Solution

As has been commented above, it's a case of adding catching the standard error output. I've had a similar issue recently. I solved it by doing the following:

NSPipe *pipe = [NSPipe pipe];
[javaTask setStandardOutput:pipe];
[javaTask setStandardError:pipe];
NSFileHandle *fileHandleForReading = [pipe fileHandleForReading];

[javaTask launch];

NSData *result = [fileHandleForReading readDataToEndOfFile];

NSString *output = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

Thanks, CRD for your help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top