#14 improve error logging

open
nobody
None
5
2005-11-03
2005-11-03
Mike Fox
No

Frequently my Python application must be installed into
a folder on the user's PC where the user himself does
not have write-permission. I have created binary
executeables from my Python code using Py2exe and I
find that if the application raises an error during
execution it tries to write an error log file.
Py2exe, as currently written, trys to write that error
log file into the same folder as the executeable file,
but this fails when the user does not have
write-permission to that folder.

I revised py2exe's boot_common.py code to solve this
problem. It still tries to write into the same folder
as the executeable file, but if this fails it writes to
the user's $HOMEPATH instead.

Here is the code from boot_common.py in
py2exe-0.6.2.win32-py2.4 :

import sys
if sys.frozen == "windows_exe":
class Stderr(object):
softspace = 0
_file = None
_error = None
def write(self, text, alert=sys._MessageBox,
fname=sys.executable + '.log'):
if self._file is None and self._error is None:
try:
self._file = open(fname, 'a')
except Exception, details:
self._error = details
import atexit
atexit.register(alert, 0,
"The logfile '%s'
could not be opened:\n %s" % \ (fname, details),
"Errors occurred")
else:
import atexit
atexit.register(alert, 0,
"See the logfile
'%s' for details" % fname,
"Errors occurred")
if self._file is not None:
self._file.write(text)
self._file.flush()
def flush(self):
if self._file is not None:
self._file.flush()
sys.stderr = Stderr()
del sys._MessageBox
del Stderr

And here's my revised code:

import sys,os
if sys.frozen == "windows_exe":
class Stderr(object):
softspace = 0
_file = None
_error = None
def write(self, text, alert=sys._MessageBox,
fname=sys.executable + '.log'):
if self._file is None and self._error is None:
try:
self._file = open(fname, 'a')
except Exception, details:
try:
log_file_dir =
os.getenv('HOMEDRIVE') + os.getenv('HOMEPATH')
log_file_name =
os.path.basename(fname)

fname=os.path.join(log_file_dir, log_file_name)
self._file = open(fname, 'a')
except:
self._error = details
import atexit
atexit.register(alert, 0,
"The logfile
'%s' could not be opened:\n %s" % \ (fname, details),
"Errors occurred")
else:
import atexit
atexit.register(alert, 0,
"See the logfile
'%s' for details" % fname,
"Errors occurred")
if self._file is not None:
self._file.write(text)
self._file.flush()
def flush(self):
if self._file is not None:
self._file.flush()
sys.stderr = Stderr()
del sys._MessageBox
del Stderr

Background info for the curious:
I work in a large corporation where the typical user
does not have Administrator or Power User authority on
his PC. All the PCs run either Windows XP or Win2K. I
must distribute the python applications that I write
via an automated system that contains all new
applications that have been authorized. Users must
install new applications by picking them in this
automated system, which has Administrator authority on
all the PCs, and can write to folders that the user is
not allowed to write to.

Discussion