2

I have a GUI python program and a simple error logging system via

import sys
sys.stderr = open("err.log", "w")

and it works mostly fine. The one problem I have is that whenever I encounter a run-time error using Windows 7 and Python 2.7.3, the file err.log gets written only after I close the program. Reading related issues I gather I'd need to perform flush() and os.fsync() to sys.stderr after the error, but I don't know how to do that easily.

One way would perhaps be to perform flush/fsync after every possible point in the program where a run-time error is possible, but this is obviously not a good solution. The main reason for using this error logging is debugging, so by definition I can't really know beforehand where I'd need to flush other than at all the possible places where a run-time error is possible. Since there are a lot of such places I'd prefer not to have to try/catch every one of them.

I'm using PyGTK, so there is no main loop visible to me in which I could do the flush/fsync.

Is there any way to tell Python to always perform flush and fsync after an error, or does anyone have other ideas how to solve this (minor) problem?

2 Answers 2

1

I agree with syhpoon: using logging for logging. But if you really need your arrangement, look at the third buffering argument to open to disable it:

If the buffering argument is given, 0 means unbuffered, 1 means line buffered, and larger numbers specify the buffer size. The preferred way to open a file is with the builtin open() function.

I tried this and it works. In a Python session:

>>> a = open('/tmp/bar', 'w')
>>> a.write('hi\n')

And in another term window:

$ cat /tmp/bar
$

Then I re-ran the experiment with buffering disabled:

>>> a = open('/tmp/bar', 'w', 0)
>>> a.write('hi\n')

And:

$ cat /tmp/var
hi
$

So use logging, but if not, see if just disabling buffering will give you what you need.

2
  • This works for me as well and I'll mark this as accepted. However, I'm also curious about logging. I tried a simple example, but there were some issues. Not only was the log file written only after closing the program, I couldn't find a way to get run-time errors to be handled by the logger.
    – Asta
    Nov 21, 2012 at 17:31
  • A more common usage pattern would be to let logging write to stderr (which is unbuffered) as usual, then inspect that stream as it's generated. That workflow is very well tested. Nov 21, 2012 at 17:59
1

Maybe a logging module would we a better choice for logs?

2
  • I don't have enough reputation to vote this down, so I'll just verbally express that I don't consider this answer to be of any use to the question, since the problem didn't have anything to do with the flexibility of logging and whatnot.
    – Asta
    Nov 21, 2012 at 17:46
  • Mature and established logging frameworks provide battle tested solutions for a lot of different child errors like the one you described, that's why it was recommended.
    – user1806568
    Nov 21, 2012 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.