Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > redirecting stdout to a file as well as screen

Reply
Thread Tools

redirecting stdout to a file as well as screen

 
 
SamG
Guest
Posts: n/a
 
      04-12-2007
How could i make, from inside the program, to have the stdout and
stderr to be printed both to a file as well the terminal(as usual).

 
Reply With Quote
 
 
 
 
Ant
Guest
Posts: n/a
 
      04-12-2007
On Apr 12, 8:14 am, "SamG" <(E-Mail Removed)> wrote:
> How could i make, from inside the program, to have the stdout and
> stderr to be printed both to a file as well the terminal(as usual).


One way would be to create a custom class which has the same methods
as the file type, and held a list of file-like objects to write to.
e.g.

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)
def writelines(self, str_list):
#etc

Then assign stdout and stderr to a new instance of one of these
objects:

mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc

HTH


 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-12-2007
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <(E-Mail Removed)> escribió:

> How could i make, from inside the program, to have the stdout and
> stderr to be printed both to a file as well the terminal(as usual).


A very minimal example:

import sys

class Tee(file):
others = ()

def write(self, data):
file.write(self, data)
for f in others:
f.write(data)

tee = Tee(r"c:\temp\output.log","wt")
tee.others = [sys.stdout, sys.stderr]
sys.stdout = sys.stderr = tee

print dir(sys)
sys.foo # error

--
Gabriel Genellina

 
Reply With Quote
 
SamG
Guest
Posts: n/a
 
      04-12-2007
On Apr 12, 1:00 pm, "Gabriel Genellina" <(E-Mail Removed)>
wrote:
> En Thu, 12 Apr 2007 04:14:32 -0300, SamG <(E-Mail Removed)> escribió:
>
> > How could i make, from inside the program, to have the stdout and
> > stderr to be printed both to a file as well the terminal(as usual).

>
> A very minimal example:
>
> import sys
>
> class Tee(file):
> others = ()
>
> def write(self, data):
> file.write(self, data)
> for f in others:
> f.write(data)
>
> tee = Tee(r"c:\temp\output.log","wt")
> tee.others = [sys.stdout, sys.stderr]
> sys.stdout = sys.stderr = tee
>
> print dir(sys)
> sys.foo # error
>
> --
> Gabriel Genellina


This is only creating an out.log file and all the stdout and stderr
are logged there.

 
Reply With Quote
 
SamG
Guest
Posts: n/a
 
      04-12-2007
On Apr 12, 12:40 pm, "Ant" <(E-Mail Removed)> wrote:
> On Apr 12, 8:14 am, "SamG" <(E-Mail Removed)> wrote:
>
> > How could i make, from inside the program, to have the stdout and
> > stderr to be printed both to a file as well the terminal(as usual).

>
> One way would be to create a custom class which has the same methods
> as the file type, and held a list of file-like objects to write to.
> e.g.
>
> class multicaster(object):
> def __init__(self, filelist):
> self.filelist = filelist
>
> def write(self, str):
> for f in self.filelist:
> f.write(str)
> def writelines(self, str_list):
> #etc
>
> Then assign stdout and stderr to a new instance of one of these
> objects:
>
> mc = multicaster([sys.stdout, sys.stderr, log_file])
> sys.stdout = mc
> sys.stderr = mc
>
> HTH




I have written this....

import sys

class multicaster(object):
def __init__(self, filelist):
self.filelist = filelist

def write(self, str):
for f in self.filelist:
f.write(str)

log_file='out.log'
mc = multicaster([sys.stdout, sys.stderr, log_file])
sys.stdout = mc
sys.stderr = mc

print "Hello"

And i get this when i run the porgram.

HelloHelloTraceback (most recent call last):
Traceback (most recent call last):

Kindly advice!


 
Reply With Quote
 
7stud
Guest
Posts: n/a
 
      04-12-2007
On Apr 12, 3:35 am, "SamG" <(E-Mail Removed)> wrote:
> On Apr 12, 12:40 pm, "Ant" <(E-Mail Removed)> wrote:
>
>
>
> > On Apr 12, 8:14 am, "SamG" <(E-Mail Removed)> wrote:

>
> > > How could i make, from inside the program, to have the stdout and
> > > stderr to be printed both to a file as well the terminal(as usual).

>
> > One way would be to create a custom class which has the same methods
> > as the file type, and held a list of file-like objects to write to.
> > e.g.

>
> > class multicaster(object):
> > def __init__(self, filelist):
> > self.filelist = filelist

>
> > def write(self, str):
> > for f in self.filelist:
> > f.write(str)
> > def writelines(self, str_list):
> > #etc

>
> > Then assign stdout and stderr to a new instance of one of these
> > objects:

>
> > mc = multicaster([sys.stdout, sys.stderr, log_file])
> > sys.stdout = mc
> > sys.stderr = mc

>
> > HTH

>
> I have written this....
>
> import sys
>
> class multicaster(object):
> def __init__(self, filelist):
> self.filelist = filelist
>
> def write(self, str):
> for f in self.filelist:
> f.write(str)
>
> log_file='out.log'
> mc = multicaster([sys.stdout, sys.stderr, log_file])
> sys.stdout = mc
> sys.stderr = mc
>
> print "Hello"
>
> And i get this when i run the porgram.
>
> HelloHelloTraceback (most recent call last):
> Traceback (most recent call last):
>
> Kindly advice!


Try:

log_file = open("out.log", "w")

 
Reply With Quote
 
Antoon Pardon
Guest
Posts: n/a
 
      04-12-2007
On 2007-04-12, SamG <(E-Mail Removed)> wrote:
> On Apr 12, 12:40 pm, "Ant" <(E-Mail Removed)> wrote:
>> On Apr 12, 8:14 am, "SamG" <(E-Mail Removed)> wrote:
>>
>> > How could i make, from inside the program, to have the stdout and
>> > stderr to be printed both to a file as well the terminal(as usual).

>>
>> One way would be to create a custom class which has the same methods
>> as the file type, and held a list of file-like objects to write to.
>> e.g.
>>
>> class multicaster(object):
>> def __init__(self, filelist):
>> self.filelist = filelist
>>
>> def write(self, str):
>> for f in self.filelist:
>> f.write(str)
>> def writelines(self, str_list):
>> #etc
>>
>> Then assign stdout and stderr to a new instance of one of these
>> objects:
>>
>> mc = multicaster([sys.stdout, sys.stderr, log_file])
>> sys.stdout = mc
>> sys.stderr = mc
>>
>> HTH

>
>
>
> I have written this....
>
> import sys
>
> class multicaster(object):
> def __init__(self, filelist):
> self.filelist = filelist
>
> def write(self, str):
> for f in self.filelist:
> f.write(str)
>
> log_file='out.log'


log_file is not a file but a string, So when you reach the
write method in your multicaster you get an atttribute error
because a string has no write method

> mc = multicaster([sys.stdout, sys.stderr, log_file])


Since sys.stdout and sys.stderr usually both refer to
the terminal, this will result in your output appearing
twice on the terminal

> sys.stdout = mc
> sys.stderr = mc


Maybe you are better of leaving sys.stderr as it is,
at least until you are sure your multicaster itself
is working as it should.

> print "Hello"

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-12-2007
En Thu, 12 Apr 2007 06:01:18 -0300, SamG <(E-Mail Removed)> escribió:

> On Apr 12, 1:00 pm, "Gabriel Genellina" <(E-Mail Removed)>
> wrote:
>> En Thu, 12 Apr 2007 04:14:32 -0300, SamG <(E-Mail Removed)> escribió:
>>
>> > How could i make, from inside the program, to have the stdout and
>> > stderr to be printed both to a file as well the terminal(as usual).

>>
>> class Tee(file):
>> others = ()
>>
>> def write(self, data):
>> file.write(self, data)
>> for f in others:
>> f.write(data)
>>

>
> This is only creating an out.log file and all the stdout and stderr
> are logged there.


Sorry, `for f in others:` should read `for f in self.others:`

--
Gabriel Genellina

 
Reply With Quote
 
SamG
Guest
Posts: n/a
 
      04-12-2007
On Apr 12, 3:16 pm, "Gabriel Genellina" <(E-Mail Removed)>
wrote:
> En Thu, 12 Apr 2007 06:01:18 -0300, SamG <(E-Mail Removed)> escribió:
>
>
>
> > On Apr 12, 1:00 pm, "Gabriel Genellina" <(E-Mail Removed)>
> > wrote:
> >> En Thu, 12 Apr 2007 04:14:32 -0300, SamG <(E-Mail Removed)> escribió:

>
> >> > How could i make, from inside the program, to have the stdout and
> >> > stderr to be printed both to a file as well the terminal(as usual).

>
> >> class Tee(file):
> >> others = ()

>
> >> def write(self, data):
> >> file.write(self, data)
> >> for f in others:
> >> f.write(data)

>
> > This is only creating an out.log file and all the stdout and stderr
> > are logged there.

>
> Sorry, `for f in others:` should read `for f in self.others:`
>
> --
> Gabriel Genellina



Does not make difference, does this work for you? Im working on linux.
But this code looks portable except for the file path

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-12-2007
En Thu, 12 Apr 2007 07:23:43 -0300, SamG <(E-Mail Removed)> escribió:

>> >> > How could i make, from inside the program, to have the stdout and
>> >> > stderr to be printed both to a file as well the terminal(as usual).

>>
>> >> class Tee(file):
>> >> others = ()

>>
>> >> def write(self, data):
>> >> file.write(self, data)
>> >> for f in others:
>> >> f.write(data)

>>
>> > This is only creating an out.log file and all the stdout and stderr
>> > are logged there.

>>
>> Sorry, `for f in others:` should read `for f in self.others:`
>>

> Does not make difference, does this work for you? Im working on linux.
> But this code looks portable except for the file path


Yes. And it's rather similar to your other example... Omit sys.stderr as
Antoon Pardon suggested.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
redirecting output ruby and from stdout results in blank file comp.lang.ruby Ruby 4 01-11-2010 11:10 PM
Issue of redirecting the stdout to both file and screen =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7zS?= Python 5 05-29-2007 02:12 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
Ant redirecting stdout to a file while in a spawned java program Douwe Java 1 01-12-2004 02:16 AM
status of redirecting STDOUT/STDERR to file jonathan Perl Misc 6 11-25-2003 12:58 AM



Advertisments