From 6004bfa72f1b40acbebf2062889deb1b32c084ee Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 23 Feb 2017 06:43:00 +0100 Subject: [PATCH] Don't lose warnings shown in early init phases --- qutebrowser/mainwindow/mainwindow.py | 1 + qutebrowser/utils/message.py | 35 +++++++++++++++++++++++----- tests/helpers/messagemock.py | 1 + 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/qutebrowser/mainwindow/mainwindow.py b/qutebrowser/mainwindow/mainwindow.py index 9606585e1..732b7a688 100644 --- a/qutebrowser/mainwindow/mainwindow.py +++ b/qutebrowser/mainwindow/mainwindow.py @@ -437,6 +437,7 @@ class MainWindow(QWidget): # messages message.global_bridge.show_message.connect( self._messageview.show_message) + message.global_bridge.flush() message_bridge.s_set_text.connect(status.set_text) message_bridge.s_maybe_reset_text.connect(status.txt.maybe_reset_text) diff --git a/qutebrowser/utils/message.py b/qutebrowser/utils/message.py index 82fe74f04..b39e2ab49 100644 --- a/qutebrowser/utils/message.py +++ b/qutebrowser/utils/message.py @@ -61,8 +61,7 @@ def error(message, *, stack=None, replace=False): typ = 'error (from exception)' _log_stack(typ, stack) log.message.error(message) - global_bridge.show_message.emit(usertypes.MessageLevel.error, message, - replace) + global_bridge.show(usertypes.MessageLevel.error, message, replace) def warning(message, *, replace=False): @@ -74,8 +73,7 @@ def warning(message, *, replace=False): """ _log_stack('warning', traceback.format_stack()) log.message.warning(message) - global_bridge.show_message.emit(usertypes.MessageLevel.warning, message, - replace) + global_bridge.show(usertypes.MessageLevel.warning, message, replace) def info(message, *, replace=False): @@ -86,8 +84,7 @@ def info(message, *, replace=False): replace: Replace existing messages with replace=True """ log.message.info(message) - global_bridge.show_message.emit(usertypes.MessageLevel.info, message, - replace) + global_bridge.show(usertypes.MessageLevel.info, message, replace) def _build_question(title, text=None, *, mode, default=None, abort_on=()): @@ -173,6 +170,10 @@ class GlobalMessageBridge(QObject): """Global (not per-window) message bridge for errors/infos/warnings. + Attributes: + _connected: Whether a slot is connected and we can show messages. + _cache: Messages shown while we were not connected. + Signals: show_message: Show a message arg 0: A MessageLevel member @@ -194,6 +195,11 @@ class GlobalMessageBridge(QObject): ask_question = pyqtSignal(usertypes.Question, bool) mode_left = pyqtSignal(usertypes.KeyMode) + def __init__(self, parent=None): + super().__init__(parent) + self._connected = False + self._cache = [] + def ask(self, question, blocking, *, log_stack=False): """Ask a question to the user. @@ -208,6 +214,23 @@ class GlobalMessageBridge(QObject): """ self.ask_question.emit(question, blocking) + def show(self, level, text, replace=False): + if self._connected: + self.show_message.emit(level, text, replace) + else: + self._cache.append((level, text, replace)) + + def flush(self): + """Flush messages which accumulated while no handler was connected. + + This is so we don't miss messages shown during some early init phase. + It needs to be called once the show_message signal is connected. + """ + self._connected = True + for args in self._cache: + self.show(*args) + self._cache = [] + class MessageBridge(QObject): diff --git a/tests/helpers/messagemock.py b/tests/helpers/messagemock.py index 38fdf3e97..7854aabcc 100644 --- a/tests/helpers/messagemock.py +++ b/tests/helpers/messagemock.py @@ -70,6 +70,7 @@ class MessageMock: def patch(self): """Start recording messages.""" message.global_bridge.show_message.connect(self._record_message) + message.global_bridge._connected = True def unpatch(self): """Stop recording messages."""