[wxPython] Redirected stdout/stderr

Hi all!

I have noticed that when an exception is raised in my program, that a small
window pops up displaying the exception. My problem is that the window
disappears before I have time to read it. (The exceptions I refer to cause
the main frame to go away).

Is it possible to keep the window open long enough to read it? Is there an
easy function call to redirect stdout/stderr back to their original values?
Or do I have to do this manually?

Thanks!

jaime wyant (jwyant@sfbcic . com)
MS Farm Bureau
(601) 977 - 4291

Is it possible to keep the window open long enough to read it? Is there

an

easy function call to redirect stdout/stderr back to their original

values?

Or do I have to do this manually?

I believe the default logger is some window thing, you want to change that.
wxLog_SetActiveTarget(some wxPyLog) is the command to change the default
logger. There are some standard ones, but I just made my own (see below)

For development(under win32) I use the following logger to display on the
console. I run my app from a dos box, the box sticks around even if my app
does dies. So, I can see the fatal exceptions

Logging to a file would also be a good choice.

I'm no wxPython expert but this is what I've been using. It does a little
more than you ask for. wxLogException is something I find very usefull. I
have it in almost every except block.

class stdout_Logger(wxPyLog):
    def __init__(self, CodeStamp=1, TimeStamp=0):
        """
        CodeStamp bool (t) adds file/func/line to log
        TimeStamp bool (t) adds timestamp to log
        """
        wxPyLog.__init__(self)
        self.TimeStamp = TimeStamp
        self.CodeStamp = CodeStamp

    def DoLogString(self, message, timeStamp):
        if self.CodeStamp:
            try:
                f = sys._getframe(1)
                t = "[%s:%s:%i]" % (f.f_code.co_filename, f.f_code.co_name,
f.f_lineno)
                message = "%-32s %s" % (t,message)
            except:
              pass
        if self.TimeStamp:
            message = time.strftime("%X", time.localtime(timeStamp)) + " " +
message
        sys.stderr.write(message+'\n')

#cause under win32 wxLogDebug goes someplace silly instead of where I want
it.
wxLogDebug = wxLogInfo

def wxLogException():
    f = sys._getframe(1)
    type, value, trace = sys.exc_info()
    wxLogInfo("%s" % (''.join(traceback.format_exception(type, value,
trace))) )
    del trace
    del value
    del type

#trickyness (aka lame hack) to init only if not existing
try:
    a_Logger.GetActiveTarget()
except:
    a_Logger = stdout_Logger(is_devel_system(),not is_devel_system())
    wxLog_SetActiveTarget(a_Logger)

You can also just pass a false value to wxApp.__init__.

Or, if you want to have the output redirected to a text-file, you can
pass a true value and a path to the file.

···

On Thu, Jun 27, 2002 at 06:07:06AM -0700, Norman Harman wrote:

> Is it possible to keep the window open long enough to read it? Is there
an
> easy function call to redirect stdout/stderr back to their original
values?
> Or do I have to do this manually?

I believe the default logger is some window thing, you want to change that.
wxLog_SetActiveTarget(some wxPyLog) is the command to change the default
logger. There are some standard ones, but I just made my own (see below)

One very simple thing you can try is to invoke your program using
"python -i" which will drop you into the python interpreter after the
exception. This leaves the logging window open as I recall, although it no
longer refreshes (since the wxPython mainloop is not running), so it won't
repaint if you cover it up. This usually sufficient for me when I run into
this problem.

-tim

Hi all!

I have noticed that when an exception is raised in my program, that a

small

window pops up displaying the exception. My problem is that the window
disappears before I have time to read it. (The exceptions I refer to

cause

the main frame to go away).

Is it possible to keep the window open long enough to read it? Is there

an

easy function call to redirect stdout/stderr back to their original

values?

···

----- Original Message -----

Or do I have to do this manually?

Thanks!

jaime wyant (jwyant@sfbcic . com)
MS Farm Bureau
(601) 977 - 4291

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users

import sys
from wxPython.wx import *

class MyApp(wxApp):
    def OnInit(self);
        'whatever you need to do for initialization'
        self.exceptions_redirected = 0

    def other_stuff(self):
        pass

   def idle(self,frame):
        if self.Initialized() and not self.exceptions_redirected:
            self.exceptions_redirected = 1
            sys.stderr = sys.__stderr__
            sys.stdout = sys.__stdout__
        
            #if you want to get more fancy and let your app intercept exceptions and print them to the log or
            #something like that, add
             #sys.excepthook = self.errorHandler #handle Python exceptions
             #where self.errorHandler has the form
             #def errorHandler(self, etype,value,traceback) and then use
             #the facilities of the traceback Python module to format what you print to stderr or to some window
             #like the log window

HTH

···

#--------------------------------
Jeff Sasmor
jeff@sasmor.com
----- Original Message -----
From: "Wyant, Jaime" <jwyant@sfbcic.com>
To: <wxpython-users@lists.wxwindows.org>
Sent: Thursday, June 27, 2002 8:56 AM
Subject: [wxPython] Redirected stdout/stderr

Hi all!

I have noticed that when an exception is raised in my program, that a small
window pops up displaying the exception. My problem is that the window
disappears before I have time to read it. (The exceptions I refer to cause
the main frame to go away).

Is it possible to keep the window open long enough to read it? Is there an
easy function call to redirect stdout/stderr back to their original values?
Or do I have to do this manually?

Thanks!

jaime wyant (jwyant@sfbcic . com)
MS Farm Bureau
(601) 977 - 4291

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users