473,763 Members | 1,377 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 7636
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
12715
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
3761
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
3975
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
6307
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
11084
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
5788
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
2594
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
2118
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
2612
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
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
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...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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
7366
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
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.