how to log qt exceptions raised in c++ in pyside2

I have an QT5/Pyside2 application happily logging into a file. Also exceptions are logged via excepthook:

sys.excepthook = lambda t, value, tb: log.exception(f'{t} {value} {"".join(traceback.format_exception(t, value, tb))}') 

But some exceptions raised by QT do not seem to pass the handler. They are simply printed to the console.

2020-07-14 13:42:51,676 - TRACE - sidewindow::__init__() - new view. call super. 2020-07-14 13:42:51,681 - TRACE - sidewindow::__init__() - show window and append QWindowsEGLStaticContext::createWindowSurface: Could not create the EGL window surface: 0x3003 qt.qpa.backingstore: composeAndFlush: makeCurrent() failed createDIB: CreateDIBSection failed (1273x666, format: 6) 

How can I redirect these Exceptions to a file or make python handle it?

Add Comment
1 Answer(s)

I had the exact same problem in the past with PyQt, so I wrote a simple module that allows to set output files for both stdout and stderr. The module is called pyfreopen but has to be compiled.

Unfortunately I did it a lot of time ago, using Python 2.7, so it’s possible it won’t work on Python 3 (I’ll be at home later and will check if it can be built for Python 3 too).

The usage is very simple:

freopen.set_stdout(path="stderr.log") freopen.set_stderr(path="stdout.log") 

Note that, as explained in the readme, once the output is set, it cannot be restored to the default behavior.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.