Send PyQt log messages to Python's logging.

master
Pacman Ghost 6 years ago
parent 03e6295bd0
commit 8d4733231f
  1. 46
      vasl_templates/main.py
  2. 3
      vasl_templates/main_window.py
  3. 3
      vasl_templates/webapp/__init__.py

@ -11,6 +11,7 @@ import urllib.request
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt, QSettings, QDir
import PyQt5.QtCore
import click
from vasl_templates.main_window import MainWindow
@ -19,22 +20,21 @@ from vasl_templates.webapp import snippets, load_debug_config
# ---------------------------------------------------------------------
class LoggerProxy:
"""Redirect messages to Python logging."""
def __init__( self, logger, level ):
self.logger = logger
self.level = level
self.closed = False
def write( self, msg ):
"""Output a message."""
if isinstance(msg, bytes):
msg = msg.decode( "utf-8" )
msg = msg.rstrip()
if msg:
self.logger.log( self.level, msg )
def flush( self ):
"""Flush the output stream."""
pass
_QT_LOGGING_LEVELS = {
PyQt5.QtCore.QtCriticalMsg: logging.CRITICAL,
PyQt5.QtCore.QtFatalMsg: logging.ERROR,
PyQt5.QtCore.QtWarningMsg: logging.WARNING,
PyQt5.QtCore.QtInfoMsg: logging.INFO,
PyQt5.QtCore.QtDebugMsg: logging.DEBUG,
}
def qtMessageHandler( msg_type, context, msg ):# pylint: disable=unused-argument
"""Handle PyQt logging messages."""
# FUDGE! PyQt issues a bunch of warning messages because we had to proxy WebChannel requests
# via the MainWindow object - we filter them out here.
if "has no notify signal and is not constant" in msg:
return
logging.getLogger().log( _QT_LOGGING_LEVELS[msg_type], "%s", msg )
# ---------------------------------------------------------------------
@ -64,17 +64,19 @@ def main( template_pack, remote_debugging, debug ):
# load the application settings
fname = "vasl-templates.ini" if sys.platform == "win32" else ".vasl-templates.conf"
if not os.path.isfile( fname ) :
fname = os.path.join( QDir.homePath() , fname )
settings = QSettings( fname , QSettings.IniFormat )
fname = os.path.join( QDir.homePath(), fname )
settings = QSettings( fname, QSettings.IniFormat )
# install the debug config file
if debug:
load_debug_config( debug )
# connect stdout/stderr to Python logging
# NOTE: Logging to the console causes crashes on Windows if we are frozen, so don't do that!
sys.stdout = LoggerProxy( logging, logging.INFO )
sys.stderr = LoggerProxy( logging, logging.WARNING )
# connect PyQt's logging to Python logging
PyQt5.QtCore.qInstallMessageHandler( qtMessageHandler )
# disable the Flask "do not use in a production environment" warning
import flask.cli
flask.cli.show_server_banner = lambda *args: None
# start the webapp server
port = webapp.config["FLASK_PORT_NO"]

@ -77,7 +77,8 @@ class MainWindow( QWidget ):
web_channel = QWebChannel( page )
# FUDGE! We would like to register a WebChannelHandler instance as the handler, but this crashes PyQt :-/
# Instead, we register ourself as the handler, and delegate processing to a WebChannelHandler.
# The downside is that PyQt emits lots of warnings about our member variables not being properties :-/
# The downside is that PyQt emits lots of warnings about our member variables not being properties,
# but we filter them out in qtMessageHandler() :-/
self._web_channel_handler = WebChannelHandler( self )
web_channel.registerObject( "handler", self )
page.setWebChannel( web_channel )

@ -44,9 +44,6 @@ import vasl_templates.webapp.main #pylint: disable=cyclic-import
import vasl_templates.webapp.vo #pylint: disable=cyclic-import
import vasl_templates.webapp.snippets #pylint: disable=cyclic-import
# initialize the application
logger = logging.getLogger( "startup" )
# ---------------------------------------------------------------------
@app.context_processor

Loading…
Cancel
Save