<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to 14: improve error logging</title><link>https://sourceforge.net/p/py2exe/feature-requests/14/</link><description>Recent changes to 14: improve error logging</description><atom:link href="https://sourceforge.net/p/py2exe/feature-requests/14/feed.rss" rel="self"/><language>en</language><lastBuildDate>Thu, 03 Nov 2005 23:32:27 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/py2exe/feature-requests/14/feed.rss" rel="self" type="application/rss+xml"/><item><title>improve error logging</title><link>https://sourceforge.net/p/py2exe/feature-requests/14/</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;Frequently my Python application must be installed into&lt;br /&gt;
a folder on the user's PC where the user himself does&lt;br /&gt;
not have write-permission.   I have created binary&lt;br /&gt;
executeables from my Python code using Py2exe and I&lt;br /&gt;
find that if the application raises an error during&lt;br /&gt;
execution it tries to write an error log file.&lt;br /&gt;
Py2exe, as currently written, trys to write that error&lt;br /&gt;
log file into the same folder as the executeable file,&lt;br /&gt;
but this fails when the user does not have&lt;br /&gt;
write-permission to that folder.&lt;/p&gt;
&lt;p&gt;I revised py2exe's boot_common.py code to solve this&lt;br /&gt;
problem.  It still tries to write into the same folder&lt;br /&gt;
as the executeable file, but if this fails it writes to&lt;br /&gt;
the user's $HOMEPATH instead.&lt;/p&gt;
&lt;p&gt;Here is the code from boot_common.py in&lt;br /&gt;
py2exe-0.6.2.win32-py2.4 :&lt;/p&gt;
&lt;p&gt;import sys&lt;br /&gt;
if sys.frozen == "windows_exe":&lt;br /&gt;
class Stderr(object):&lt;br /&gt;
softspace = 0&lt;br /&gt;
_file = None&lt;br /&gt;
_error = None&lt;br /&gt;
def write(self, text, alert=sys._MessageBox,&lt;br /&gt;
fname=sys.executable + '.log'):&lt;br /&gt;
if self._file is None and self._error is None:&lt;br /&gt;
try:&lt;br /&gt;
self._file = open(fname, 'a')&lt;br /&gt;
except Exception, details:&lt;br /&gt;
self._error = details&lt;br /&gt;
import atexit&lt;br /&gt;
atexit.register(alert, 0,&lt;br /&gt;
"The logfile '%s'&lt;br /&gt;
could not be opened:\n %s" % \
(fname, details),&lt;br /&gt;
"Errors occurred")&lt;br /&gt;
else:&lt;br /&gt;
import atexit&lt;br /&gt;
atexit.register(alert, 0,&lt;br /&gt;
"See the logfile&lt;br /&gt;
'%s' for details" % fname,&lt;br /&gt;
"Errors occurred")&lt;br /&gt;
if self._file is not None:&lt;br /&gt;
self._file.write(text)&lt;br /&gt;
self._file.flush()&lt;br /&gt;
def flush(self):&lt;br /&gt;
if self._file is not None:&lt;br /&gt;
self._file.flush()&lt;br /&gt;
sys.stderr = Stderr()&lt;br /&gt;
del sys._MessageBox&lt;br /&gt;
del Stderr&lt;/p&gt;
&lt;p&gt;And here's my revised code:&lt;/p&gt;
&lt;p&gt;import sys,os&lt;br /&gt;
if sys.frozen == "windows_exe":&lt;br /&gt;
class Stderr(object):&lt;br /&gt;
softspace = 0&lt;br /&gt;
_file = None&lt;br /&gt;
_error = None&lt;br /&gt;
def write(self, text, alert=sys._MessageBox,&lt;br /&gt;
fname=sys.executable + '.log'):&lt;br /&gt;
if self._file is None and self._error is None:&lt;br /&gt;
try:&lt;br /&gt;
self._file = open(fname, 'a')&lt;br /&gt;
except Exception, details:&lt;br /&gt;
try:&lt;br /&gt;
log_file_dir =&lt;br /&gt;
os.getenv('HOMEDRIVE') + os.getenv('HOMEPATH')&lt;br /&gt;
log_file_name =&lt;br /&gt;
os.path.basename(fname)&lt;/p&gt;
&lt;p&gt;fname=os.path.join(log_file_dir, log_file_name)&lt;br /&gt;
self._file = open(fname, 'a')&lt;br /&gt;
except:&lt;br /&gt;
self._error = details&lt;br /&gt;
import atexit&lt;br /&gt;
atexit.register(alert, 0,&lt;br /&gt;
"The logfile&lt;br /&gt;
'%s' could not be opened:\n %s" % \
(fname, details),&lt;br /&gt;
"Errors occurred")&lt;br /&gt;
else:&lt;br /&gt;
import atexit&lt;br /&gt;
atexit.register(alert, 0,&lt;br /&gt;
"See the logfile&lt;br /&gt;
'%s' for details" % fname,&lt;br /&gt;
"Errors occurred")&lt;br /&gt;
if self._file is not None:&lt;br /&gt;
self._file.write(text)&lt;br /&gt;
self._file.flush()&lt;br /&gt;
def flush(self):&lt;br /&gt;
if self._file is not None:&lt;br /&gt;
self._file.flush()&lt;br /&gt;
sys.stderr = Stderr()&lt;br /&gt;
del sys._MessageBox&lt;br /&gt;
del Stderr&lt;/p&gt;
&lt;p&gt;Background info for the curious:&lt;br /&gt;
I work in a large corporation where the typical user&lt;br /&gt;
does not have Administrator or Power User authority on&lt;br /&gt;
his PC.  All the PCs run either Windows XP or Win2K.  I&lt;br /&gt;
must distribute the python applications that I write&lt;br /&gt;
via an automated system that contains all new&lt;br /&gt;
applications that have been authorized.   Users must&lt;br /&gt;
install new applications by picking them in this&lt;br /&gt;
automated system, which has Administrator authority on&lt;br /&gt;
all the PCs, and can write to folders that the user is&lt;br /&gt;
not allowed to write to.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Mike Fox</dc:creator><pubDate>Thu, 03 Nov 2005 23:32:27 -0000</pubDate><guid>https://sourceforge.netbf55bdf435cd054cd75960a1d0c4d0cd48219466</guid></item></channel></rss>