How to capture stdout/stderr ?

We're trying to make a console in my application and would like to capture any data written to STD streams... how to ?
The FILE structure contains a field which is a pointer on the write routine to be used. (see the stdio header file)
int stdoutwrite(void *inFD, const char *buffer, int size)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init] ;
NSString *tmp = [NSString stringWithCString:buffer length:size] ; // choose the best encoding for your app

// do what you need with the tmp string here
// like appending it to a NSTextView
// you may like to scan for a char(12) == CLEARSCREEN
// and others "special" characters...

[pool release] ;
return size ;
}
add somewhere in your code
stdout->_write = stdoutwrite ;
You may also want to save the original value before setting it to yours...
In a multithreaded program, we have to add a mutex lock test wrapping the whole body code of the routine to avoid various streams mixing their outputs in your text view.
The autorelease pool may not be necessary in your context, but if you have some printf in
+load class method or if you have threads in pure C that don't allocate a release pool, you will find it useful to avoid memory leaks and numerous NSLog messages.