473,848 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

redirecting stdout to a file as well as screen

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).

Apr 12 '07 #1
10 7641
Ant
On Apr 12, 8:14 am, "SamG" <mad.vi...@gmai l.comwrote:
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(obj ect):
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
Apr 12 '07 #2
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <ma*******@gmai l.comescribió:
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\o utput.log","wt" )
tee.others = [sys.stdout, sys.stderr]
sys.stdout = sys.stderr = tee

print dir(sys)
sys.foo # error

--
Gabriel Genellina

Apr 12 '07 #3
On Apr 12, 1:00 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <mad.vi...@gmai l.comescribió:
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\o utput.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.

Apr 12 '07 #4
On Apr 12, 12:40 pm, "Ant" <ant...@gmail.c omwrote:
On Apr 12, 8:14 am, "SamG" <mad.vi...@gmai l.comwrote:
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(obj ect):
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(obj ect):
def __init__(self, filelist):
self.filelist = filelist

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

log_file='out.l og'
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.

HelloHelloTrace back (most recent call last):
Traceback (most recent call last):

Kindly advice!
Apr 12 '07 #5
On Apr 12, 3:35 am, "SamG" <mad.vi...@gmai l.comwrote:
On Apr 12, 12:40 pm, "Ant" <ant...@gmail.c omwrote:
On Apr 12, 8:14 am, "SamG" <mad.vi...@gmai l.comwrote:
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(obj ect):
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(obj ect):
def __init__(self, filelist):
self.filelist = filelist

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

log_file='out.l og'
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.

HelloHelloTrace back (most recent call last):
Traceback (most recent call last):

Kindly advice!
Try:

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

Apr 12 '07 #6
On 2007-04-12, SamG <ma*******@gmai l.comwrote:
On Apr 12, 12:40 pm, "Ant" <ant...@gmail.c omwrote:
>On Apr 12, 8:14 am, "SamG" <mad.vi...@gmai l.comwrote:
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(obj ect):
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(obj ect):
def __init__(self, filelist):
self.filelist = filelist

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

log_file='out.l og'
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"
Apr 12 '07 #7
En Thu, 12 Apr 2007 06:01:18 -0300, SamG <ma*******@gmai l.comescribió:
On Apr 12, 1:00 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>En Thu, 12 Apr 2007 04:14:32 -0300, SamG <mad.vi...@gmai l.comescribió:
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

Apr 12 '07 #8
On Apr 12, 3:16 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Thu, 12 Apr 2007 06:01:18 -0300, SamG <mad.vi...@gmai l.comescribió:
On Apr 12, 1:00 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Thu, 12 Apr 2007 04:14:32 -0300, SamG <mad.vi...@gmai l.comescribió:
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 :)

Apr 12 '07 #9
En Thu, 12 Apr 2007 07:23:43 -0300, SamG <ma*******@gmai l.comescribió:
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

Apr 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12719
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this script to a file (including that which is printed by the spawned programs) via the redirect (">") function on a Win2K Command Prompt. In the captured output however, the output from the os.system() calls ALWAYS comes before the output from the...
3
3764
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My problem is the C++ lib does write stuffs on its stdout, and I would like to print those messages in a Tk frame. When I run the computation, it has it's own thread. So my question is : how van I redirect the dll's stdout to something I can
7
3980
by: Mike | last post by:
I would like my 'print' statements to send its output to the user's screen and a log file. This is my initial attempt: class StdoutLog(file): def __init__(self, stdout, name='/tmp/stdout.log', mode='w',bufsize=-1): super(StdoutLog, self).__init__(name,mode,bufsize) self.stdout = stdout
4
6312
by: Alexander Stippler | last post by:
Hi, The short story: I have to redirect stdout (not cout!!) to an ostream. How can I manage this? The long story: I use the NAGC numerics library with C++ and want to use its output routines for e.g. complex matrices. The problem: You can either print to a file (given by filename) or to stdout. To call the NAGC-method within an operator<< thus I have to redirect stdout to the given
2
11093
by: Jason Heyes | last post by:
Here is what I use to redirect printed messages to a file: std::ofstream file("logfile"); std::cout.rdbuf(file.rdbuf()); std::cout << "this message goes to a file" << std::endl; Will the next message I print go to a file as well? std::printf("does this message go to a file?");
8
5795
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can technically create an executable that calls the function and then using a command prompt call the exexutable using the > output.txt redirector but I would prefer to do everything within the application itself. Is there a way for force an output...
1
2598
by: pp4175 | last post by:
Hello Everyone, I am currently working on writing a GUI wrapper for a Menu driven DOS Program. What I was looking on doing is redirecting stdout/stdin to a stream and read/write similar to a file. My issue is that I try to read from the stdout stream and never get anything only if the program has exited. Is there a way of reading the redirected stdout from a DOS program in say real time? I have tried the async (BeginOutputReadLine) and...
3
2124
by: pnsreee | last post by:
Hi All, I am trying to print a log file created in the same script with the code bellow. but Im not getting output on screen. The output file is created using rediretion of output. open(STDOUT, ">flat.log") || die "Can't redirect stdout"; prinf " ----------------------------------------------\n";
5
2619
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I wanna print the log to both the screen and file, so I simulatered a 'tee' class Tee(file): def __init__(self, name, mode): file.__init__(self, name, mode) self.stdout = sys.stdout sys.stdout = self
0
9735
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10658
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10718
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9497
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4541
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4133
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.