Gene Chiaramonte <gchiaramonte@mail.com> writes:
Also, is there and example of trapping the output from python within a
running wxPython program? ie. When when an error is generated in my
wxPython program, where does the message go and how do I get it? This
is something I really need to do. I'm running wxPython on Windoze.
By default wxPython should redirect stdout/stderr for routines under
its control to pop-up an automatic console window. There is a named
"redirect" parameter to the wxApp() __init__ that can be set to None
to prevent this redirection, or you can change the named "filename"
parameter to send output to a file rather than the console window.
If you use the None approach, then you should be able to handle the
redirection in a standard way from your own program. This method can
be used in any Python program to take control of stdout/stderr (and
is actually how wxPython itself is doing it)
First, define a class that's going to look like a File class for output
(that just means it has to have a "write" method that accepts a string).
Then, re-assign sys.stdout and/or sys.stderr to your function. (Use
separate functions if you want to distinguish the two). If you want to
use the original values (either directly, or to restore them at the end
of your script), you can reference sys.__stdout__ and sys.__stderr__.
For example, here's a small Log class that takes redirected stdout/stderr
and prints it to any other file with a timestamp and process id pre-pended.
Note that without a file specified, it defaults to the current sys.stdout
(so if you redirect and then create a new LogClass, it actually goes to
the redirection :-))
import os
import sys
import time
class LogClass:
def __init__ (self, Filename=None):
if (Filename != None):
self._file = open(Filename,"a")
else:
self._file = sys.stdout
self._needprefix = 1
self._sitename = None
def write(self, Message):
if (self._needprefix):
self._file.write("[%s-%d" %
(time.strftime("%H:%M:%S",
time.localtime(time.time())),
os.getpid()))
if (self._sitename):
self._file.write("-")
self._file.write(self._sitename)
self._file.write("] ")
self._needprefix = 0
self._file.write(Message)
self._file.flush()
if (Message[-1] == "\n"):
self._needprefix = 1
def log(self, Message):
self.write(Message)
self.write("\n")
def setsite(self, SiteName):
self._sitename = SiteName
This might be how you would use that in a script. Note the top level
try/finally clause to ensure that the redirection is cleaned up. This
avoids problems when running the script interactively multiple times.
Log = LogClass('filename') # Or use 'None' to stick with stdout
# And then reroute stdout/stderr to that file object
sys.stdout = Log
sys.stderr = Log
try:
# Main body of your code goes here
finally:
# Ensure stdout/stderr is restored (in case we're interactive)
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
Of course, in your case, you might not just log the output again but might
want to do something based on it - it's up to you - you have complete
control
of all of the strings that would normally go to stdout/stderr.
I use this all the time to get accurate logging of my script executions,
including any imported modules (and process execution, since I open pipes
and just print all the child process output to stdout).
Hope this helps.
-- David
/-----------------------------------------------------------------------\
\ David Bolen \ E-mail: db3l@fitlinxx.com /
> FitLinxx, Inc. \ Phone: (203) 708-5192 |
/ 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \
\-----------------------------------------------------------------------/