Problem redirecting stderr to a wxPython application

Hello,

in an application, I want to redirect stdout/stderr to a wxTextCtrl. Just at the start of the app, I have

Log = LogRedirect()
sys.stdout = sys.stderr = Log

with the class LogRedirect having just the one method

def write(self, msg):
  wxLogMessage(msg)

Then in the __init__ of my main wxFrame I have

self.log = wxTextCtrl(splitterWindow, -1,
    style=wxTE_MULTILINE|wxTE_READONLY)
wxLog_SetActiveTarget(wxLogTextCtrl(self.log))

This works for some, but not for *all* stderr output. In particular, if I create some OGL objects and assign non-existing fonts to them, the following messages are sents to the command line:

** (HMMBuilder.py:7512): WARNING **: Couldn't load font "Times New Roman 10" falling back to "Sans 10"

That is, the messages above apparently don't get processed by the call to wxLogMessage in the class LogRedirect. If OTOH I redirect stderr on the command line and start the program with something like

python HMMBuilder.py >& somefile

then the messages above get sent to somefile. How can I capture those stderr font messages directly in the application?

I am using python 2.3.2 and wxPython 2.4.2.4 on linux.

Thanks,
Davide

Davide Salomoni wrote:

Hello,

in an application, I want to redirect stdout/stderr to a wxTextCtrl. Just at the start of the app, I have

Log = LogRedirect()
sys.stdout = sys.stderr = Log

with the class LogRedirect having just the one method

def write(self, msg):
    wxLogMessage(msg)

Then in the __init__ of my main wxFrame I have

self.log = wxTextCtrl(splitterWindow, -1,
   style=wxTE_MULTILINE|wxTE_READONLY)
wxLog_SetActiveTarget(wxLogTextCtrl(self.log))

This works for some, but not for *all* stderr output. In particular, if I create some OGL objects and assign non-existing fonts to them, the following messages are sents to the command line:

** (HMMBuilder.py:7512): WARNING **: Couldn't load font "Times New Roman 10" falling back to "Sans 10"

That is, the messages above apparently don't get processed by the call to wxLogMessage in the class LogRedirect. If OTOH I redirect stderr on the command line and start the program with something like

python HMMBuilder.py >& somefile

then the messages above get sent to somefile. How can I capture those stderr font messages directly in the application?

Those messages are coming from the C++ code (the "HMMBuilder.py:7512" is the process name and pid, not a file and line number) so the output is bypassing the Python streams and going directly to the C stdio/stderr streams. There may be a way to capture those from Python by doing low-level tricks with the file handles, but I've never tried it.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!