Don't lose warnings shown in early init phases

This commit is contained in:
Florian Bruhin 2017-02-23 06:43:00 +01:00
parent cd2c547b6b
commit 6004bfa72f
3 changed files with 31 additions and 6 deletions

View File

@ -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)

View File

@ -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):

View File

@ -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."""