views:

11

answers:

1

How do I redirect RuntimeError to log instead of the console?

In the code below, the 'Hello' is printed to console, which is redirected to the wx.TextCtrl. The RuntimeError raised OnClose is printed to the terminal however. How do I redirect the RuntimeError to see same log as the 'Hello'?

import sys
import wx

class RedirectText:
    def __init__(self, log):
        self.log = log
    def write(self,string):
        self.log.AppendText(string)

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.log = wx.TextCtrl(self, -1, '', style=wx.TE_READONLY|wx.TE_MULTILINE)
        sizer = wx.BoxSizer()
        sizer.Add(self.log, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(sizer)
        redirection = RedirectText(self.log)
        sys.stdout = redirection
        print 'hello'
        self.Bind(wx.EVT_CLOSE, self.OnClose)
    def OnClose(self, event):
        raise RuntimeError('error')

if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MainFrame()
    frame.Show()
    app.MainLoop()
A: 

I'd assume you just have to assign your log to sys.stderr.

Tim
Haha! That was waaaay easier than I thought. Silly to have overlooked that little detail.
c00kiemonster