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?