Collection console output

Esben von Buchwald find.mig at paa.google
Thu Sep 17 08:03:22 EDT 2009


MRAB wrote:
> Esben von Buchwald wrote:
>> Hello
>>
>> Are there any simple ways to collect the data, python prints to the 
>> console when running an app?
>>
>> I'm doing som apps for S60 mobile phones and can't see the console, 
>> when the UI is running, but i'd like to collect the output, to look 
>> for eventual exceptions etc.
>>
>> Cant it be redirected to a log file, just like when runing a script in 
>> unix, where you say $ ./script.pl > logfile.txt ?
> 
> Does the S60 support a command line like *nix and Windows?
> 
> An alternative from within the script is:
> 
>     import sys
> 
>     sys.stdout = open(log_path, "w")
> 
> 
> You might also want to capture stderr:
> 
>     import sys
> 
>     log_file = open(log_path, "w")
>     sys.stdout = log_file
>     sys.stderr = log_file

Thanks a lot - it works fine :)

I've made this class which works perfectly

class Consolefile:
     def __init__(self):
         self.file=None
         self.status='init'
     def start(self):
         self.filename="%s\\%s_console.log"%(cfg.logdir,FILEID)
         print self.filename
         self.oldstderr=sys.stderr
         self.oldstdout=sys.stdout
         self.file=open(self.filename,'w')
         print >> self.file, '#Logging STDERR and STDOUT to this file'
         sys.stderr=self.file
         sys.stdout=self.file
         self.status='started'
     def stop(self):
         print >> self.file, '#Console logging stopped'
         sys.stderr=self.oldstderr
         sys.stdout=self.oldstdout
         self.file.close()
         self.status='stopped'





More information about the Python-list mailing list