Re: Capturing stdout
Re: Capturing stdout
- Subject: Re: Capturing stdout
- From: Sailor Quasar <email@hidden>
- Date: Thu, 18 Dec 2003 02:17:48 -0500
On Thursday, December 18, 2003, at 12:47 AM, Don Murta wrote:
How about freopen on stdout to a temp file then use fread to regain
that output like as in
http://www.opengroup.org/onlinepubs/007904975/functions/freopen.html
The traditional way that I've used to recapture stdout (and stderr, and
stdin too in fact) runs not entirely unlike an interprocess
communication:
1. close() all the descriptors you wish to capture. I'll do just stdout
for this. Make sure you do this as early in your execution as humanly
possible, otherwise you could lose some data to the stdio buffers. An
fclose(stdout) may be better advised. You may want to do this stuff
before main() calls NSApplicationMain(), if you're worried about Cocoa
throwing stuff to stdout before you gain control from your
NSApplication delegate. This restricts you to using pipe() rather than
NSPipe in steps 2 and 4. Yes, it's safe to do this stuff before
NSApplicationMain() is called, whether or not Apple recommends having
anything else in main() in a Cocoa app :).
2. call pipe() to get a pair of descriptors, or use NSPipe and call
[[pipe fileHandleForReading] fileDescriptor] for step 3.
3. dup2() the write end of the pipe to STDOUT_FILENO (or
fileno(stdout), but both are guaranteed good). There is no Cocoa
equivelant to this call in NSPipe or NSFileHandle, so if you use them
you'll have to extract the descriptor as noted in step 2.
4. use whatever form of I/O you like on the read end of the pipe
(fdopen() it to a FILE* stream, stick an NSFileHandle* on it, pull the
read end NSFileHandle* from an NSPipe* if you used one, etc.) to get
anything printed to stdout.
The same process works identically for stderr; in fact you can dup2()
the same pipe to both STDOUT_FILENO and STDERR_FILENO to get the output
as a combined stream, though you lose the ability to distinguish where
they came from. It's almost as easy to just open two pipes and read
from both of them.
Redirecting user input to stdin, while often a pointless exercise in
code complexity, sometimes has its uses. The process is essentially the
same, but for stdin one would dup2() the read end of the pipe to
STDIN_FILENO, and stuff things down the write end.
What's going on behind the scenes here, for those not familiar with
pipes, is that pipe() (and the NSPipe counterpart) creates a pair of
file descriptors (file handles) that are connected directly to each
other. dup2() maps all the descriptor entries for a given descriptor
onto another given descriptor, effectively aliasing them.
On 17-Dec-03, at 9:05 PM, Nicholas Francis wrote:
Hi guys...
We're trying to make a console for our game editor, and would like to
capture any output sent to stdout for display. This is all done using
cocoa. I've looked at the NSPipe and NSTask documentation, but I
can't see how to get the output of the running (main) process...
Any pointers?
-- Sailor Quasar, guardian of Leraz's memory
"A face of stone may hide a soul with the deepest Love of all"
Email: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.