[Tutor] Capturing stdout/stderror from linked c++ program

Douglas Eck doug@idsia.ch
Mon Mar 10 05:26:00 2003


Hello Tutor,

I have a very nice neural network that is written in c++. I've wrapped a 
python interface around it so that I can do things like this:

from dse import neuralnet
neuralnet.init("params.txt")
neuralnet.run()

This all works beautifully.  However, the c++ code prints some important 
information to stdout (cout << info << endl;) during the run of the 
network. Is there a way to capture this standard output so that I can 
display it using wxLogMessage() (from wxPython) in my python GUI?

You may ask, why? Why not just look at the terminal you used to start 
the python gui? Answer: because once I have about 5 of them running it 
becomes a headache... each simulation has it's own python gui and it's 
own terminal. If I spawn all of them from the same terminal, the stdout 
messages get jumbled up. If I give each GUI it's own terminal, then I've 
got to keep the terminals lined up with the python guis or I no longer 
know which is which.

In addition, I would strongly prefer not to have to replace all of the 
"cout" calls in the c++ code with special python calls. I say this 
becuase I also have a non-python command-line wrapper as well. But if 
that's the only way (to use a callback function) then I'll do it.


Suggestions. Isn't there some magic I can do that just tells python to 
grab (even a copy of) the stdout/stderr of the terminal that called it?

Thanks,
Doug Eck