Writing to stdout and a log file

M

Mike

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
def write(self, data):
self.stdout.write(data)
self.write(data)

import sys
sys.stdout = StdoutLog(sys.stdout)
print 'STDOUT', sys.stdout

When the program is run the string is written to the log file but
nothing appears on my screen. Where's the screen output?

It looks like the superclass's write() method is getting called instead
of the StdoutLog instance's write() method.

The python documentation says 'print' should write to
sys.stdout.write() but that doesn't seem to be happening.

Any idea what's going one?
Or ideas on how to debug this?

Thanks, Mike
 
J

Jeff Epler

This variation works:
#------------------------------------------------------------------------
class Tee:
def __init__(self, *args):
self.files = args

def write(self, data):
for f in self.files:
result = f.write(data)
return result

def writelines(self, seq):
for i in seq: self.write(i)

import sys
sys.stdout = Tee(sys.stdout, open("/tmp/stdout.log", "w"))

print 'STDOUT', sys.stdout
#------------------------------------------------------------------------

It appears that the 'print' statement always uses file.write if
isinstance(sys.stdout, file). I don't know whether this has been
reported as a bug before, or if there's a reason for the current
behavior. It may be an accidental behavior that is left over from the
days when builtin types were not subclassable.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCZaMDJd01MZaTXX0RAmdYAJ9GiKYMT/1HjCfa/8DdpPXNSIvJrACeOOK6
mCfiTD9mqTqNpUudqvm/Log=
=2JZ/
-----END PGP SIGNATURE-----
 
M

Mike

Thanks.

I should've mentioned I want StdoutLog to subclass the 'file' type
because I need all the file attributes available.

I could add all the standard file methods and attributes to StdoutLog
without subclassing 'file' but I'd rather avoid this if I can.
 
J

Jeff Epler

In that case, it looks like you won't be able to get what you want
without modifying CPython. PRINT_ITEM calls PyFile_SoftSpace,
PyFile_WriteString, and PyFile_WriteObject, which all use
PyFile_Check(). It might be as simple as changing these to
PyFile_CheckExact() calls in PyFile_WriteString / PyFile_WriteObject,
but I have no idea whether the test suite still works after this change
is made. It does make this program work (it prints things from X.write):

class X(file):
def write(self, s):
print "X.write", `s`
return file.write(self, s)

import sys
x = X("/tmp/out.txt", "w")
print >>x, 42

I don't care to be the champion of this patch, or to submit it to
sourceforge; I suspect there should be a better review of PyFile_Check
vs PyFile_CheckExact uses in fileobject.c, instead of just picking the
few spots that make this usage work. Before being submitted as a patch,
a testcase should be added too. Feel free to run with this if you feel
strongly about it.

Jeff

Index: Objects/fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.193
diff -u -u -r2.193 fileobject.c
--- Objects/fileobject.c 7 Nov 2004 14:15:28 -0000 2.193
+++ Objects/fileobject.c 20 Apr 2005 02:41:32 -0000
@@ -2012,7 +2012,7 @@
PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
return -1;
}
- else if (PyFile_Check(f)) {
+ else if (PyFile_CheckExact(f)) {
FILE *fp = PyFile_AsFile(f);
#ifdef Py_USING_UNICODE
PyObject *enc = ((PyFileObject*)f)->f_encoding;
@@ -2082,7 +2082,7 @@
"null file for PyFile_WriteString");
return -1;
}
- else if (PyFile_Check(f)) {
+ else if (PyFile_CheckExact(f)) {
FILE *fp = PyFile_AsFile(f);
if (fp == NULL) {
err_closed();


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCZcHmJd01MZaTXX0RAh4eAJ4wjWlguTzoHWLbjltZ2f+cXEfa9ACdErTX
C5ebIIE+I3NrgC3cIUO9W9o=
=WN4p
-----END PGP SIGNATURE-----
 
W

Wolfram Kraus

Mike said:
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
def write(self, data):
self.stdout.write(data)
What happens when you do a self.stdout.flush() here?
self.write(data)

import sys
sys.stdout = StdoutLog(sys.stdout)
print 'STDOUT', sys.stdout

When the program is run the string is written to the log file but
nothing appears on my screen. Where's the screen output?

It looks like the superclass's write() method is getting called instead
of the StdoutLog instance's write() method.

The python documentation says 'print' should write to
sys.stdout.write() but that doesn't seem to be happening.

Any idea what's going one?
Or ideas on how to debug this?

Thanks, Mike

I had the same problem (writing to file and stdout with print) and my
solution was *not* to subclass file and instead add a
self.outfile=file(...) to the constructor.

HTH,
Wolfram
 
M

Mike

flushing stdout has no effect.

I've got an implementation that does not subclass file. It's not as
nice but it works.
 
M

Michael Hoffman

Mike said:
I should've mentioned I want StdoutLog to subclass the 'file' type
because I need all the file attributes available.

You might use a surrogate pattern. Here's one I use for this kind of
situation, where I want to subclass but that can't be done for some reason.

class SurrogateNotInitedError(exceptions.AttributeError):
pass

class Surrogate(object):
def __init__(self, data):
self._data = data

def __getattr__(self, name):
if name == "_data":
raise SurrogateNotInitedError, name
else:
try:
return getattr(self._data, name)
except SurrogateNotInitedError:
raise SurrogateNotInitedError, name

I'll leave it as an exercise to the reader to make this work when
self._data is actually a list of objects instead of a single object.
You'll obviously need special logic for different methods, like write(),
since for some of them you will want to call every object in self._data,
and others only a single object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top