Hello all,

I am having some trouble with capturing and redirecting stdout in my Cocoa
application. A little bit of background: My app has an embedded scripting
language interpreter, and I would like to redirect stdout in order to
display the results of executed scripts in an NSTextView.

I've followed the approach described in the solution to StackOverflow
question: http://stackoverflow.com/questions/2406204

To summarize, I use dup2() to "bind" stdout to my own NSPipe, and then add
an NSNotification observer to listen for an
NSFileHandleReadCompletionNotification on the NSPipe; the notification
callback then reads the NSPipe. NSFileHandle readInBackgroundAndNotify is
used to asynchronously read the data.

This approach works, for the most part -- when executed scripts print only a
small amount of data to stdout, my app redirects the output to an NSTextView
successfully. However, when executed scripts print a large amount of data to
stdout, the app hangs.

I think the problem is that the pipe buffer is not being flushed/read when
it's full; when the pipe is full, the script interpreter then is blocked
because it's waiting for the pipe to empty so that it can write.

I've also tried using an pseudo-tty via openpty() instead of an NSPipe, but
I get the same problem: The app hangs when too much output is sent to stdout
by an executing script.

I'm sure that what I want to do is possible -- after all, the Xcode debugger
console has no problems displaying stdout when running the same script that
hangs my app.

I guess my question is: How do I deal with a large amount of data that's
printed to stdout in this situation? Is there some configuration I can use
to change the buffering behavior (I've read elsewhere that stdout is
block-buffered by default)? Is there a way to force NSFileHandle
readInBackgroundAndNotify to read the pipe more frequently? Or is there
something else?

Thanks very much in advance,
Edmond
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to