From 420f13665d8f68c27616d64e3f08a5846ae1b816 Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Fri, 10 Jul 2020 21:59:55 +0930 Subject: [PATCH] Move log updates into queue --- horus-gui.spec | 2 +- horusgui/gui.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/horus-gui.spec b/horus-gui.spec index 2e2123b..30c854b 100644 --- a/horus-gui.spec +++ b/horus-gui.spec @@ -26,7 +26,7 @@ exe = EXE(pyz, bootloader_ignore_signals=False, strip=False, upx=True, - console=False ) + console=True) coll = COLLECT(exe, a.binaries, a.zipfiles, diff --git a/horusgui/gui.py b/horusgui/gui.py index 7b0c5b3..c345348 100644 --- a/horusgui/gui.py +++ b/horusgui/gui.py @@ -45,6 +45,7 @@ widgets = {} # Queues for handling updates to image / status indications. fft_update_queue = Queue(256) status_update_queue = Queue(256) +log_update_queue = Queue(256) # List of audio devices and their info audio_devices = {} @@ -676,6 +677,15 @@ def start_decoding(): widgets["startDecodeButton"].clicked.connect(start_decoding) +def handle_log_update(log_update): + global widgets + + widgets["console"].appendPlainText(log_update) + # Make sure the scroll bar is right at the bottom. + _sb = widgets["console"].verticalScrollBar() + _sb.setValue(_sb.maximum()) + + # GUI Update Loop def processQueues(): """ Read in data from the queues, this decouples the GUI and async inputs somewhat. """ @@ -691,6 +701,11 @@ def processQueues(): handle_status_update(_status) + while log_update_queue.qsize() > 0: + _log = log_update_queue.get() + + handle_log_update(_log) + # Try and force a re-draw. QtGui.QApplication.processEvents() @@ -713,23 +728,23 @@ gui_update_timer.start(100) class ConsoleHandler(logging.Handler): """ Logging handler to write to the GUI console """ - def __init__(self, consolewidget): + def __init__(self, log_queue): logging.Handler.__init__(self) - self.consolewidget = consolewidget + self.log_queue = log_queue def emit(self, record): _time = datetime.datetime.now() _text = f"{_time.strftime('%H:%M:%S')} [{record.levelname}] {record.msg}" - self.consolewidget.appendPlainText(_text) - # Make sure the scroll bar is right at the bottom. - _sb = self.consolewidget.verticalScrollBar() - _sb.setValue(_sb.maximum()) - # Redraw - self.consolewidget.repaint() + + try: + self.log_queue.put_nowait(_text) + except: + print("Queue full!") + # Add console handler to top level logger. -console_handler = ConsoleHandler(widgets["console"]) +console_handler = ConsoleHandler(log_update_queue) logging.getLogger().addHandler(console_handler)