Similar to what was discussed
here, I'm trying to redirect python stderr and stdout so that I can get the text of python error messages in the cpp code that's calling the embedded python script. I've been somewhat successful, but am getting some weird results with extra endlines.
To simplify the problem, I've made a library using Boost Python that can be imported into Python that does similar redirection of stderr...
Some cpp code to expose a simple "logging" library to Python:
void happy_write(const char* text) {
std::cout << "captured stderr text:"<< text;
}
BOOST_PYTHON_MODULE(Logger)
{
boost::python::def("write", happy_write);
}
and then, in the python interpreter:
>>> import Logger
>>> import sys
>>> sys.stderr = Logger
>>> blah
captured stderr text:Traceback (most recent call last):
captured stderr text: File "<stdin>", line 1, in <module>
captured stderr text:NameErrorcaptured stderr text:: captured stderr text:name 'blah' is not definedcaptured stderr text:
>>>
For some reason, I get "captured stderr text:" on stdout twice for each line of text sent from Python to stderr, with the second copy being seemingly randomly inserted into the Python error message text.
Any ideas why this might be? Thanks.