class LoggerWindow(QtGui.QDialog): """ Window that displays a history of the logged messages in the app. """ def __init__(self, handler): """ Initialize the widget with the custom handler. :param handler: Custom handler that supports history and signal. :type handler: LeapLogHandler. """ QtGui.QDialog.__init__(self) leap_assert(handler, "We need a handler for the logger window") leap_assert_type(handler, LeapLogHandler) # Load UI self.ui = Ui_LoggerWindow() self.ui.setupUi(self) # Make connections self.ui.btnSave.clicked.connect(self._save_log_to_file) self.ui.btnDebug.toggled.connect(self._load_history), self.ui.btnInfo.toggled.connect(self._load_history), self.ui.btnWarning.toggled.connect(self._load_history), self.ui.btnError.toggled.connect(self._load_history), self.ui.btnCritical.toggled.connect(self._load_history) self.ui.leFilterBy.textEdited.connect(self._filter_by) self.ui.cbCaseInsensitive.stateChanged.connect(self._load_history) self._current_filter = "" # Load logging history and connect logger with the widget self._logging_handler = handler self._connect_to_handler() self._load_history() def _connect_to_handler(self): """ This method connects the loggerwindow with the handler through a signal communicate the logger events. """ self._logging_handler.new_log.connect(self._add_log_line) def _add_log_line(self, log): """ Adds a line to the history, only if it's in the desired levels to show. :param log: a log record to be inserted in the widget :type log: a dict with RECORD_KEY and MESSAGE_KEY. the record contains the LogRecord of the logging module, the message contains the formatted message for the log. """ html_style = { logging.DEBUG: "background: #CDFFFF;", logging.INFO: "background: white;", logging.WARNING: "background: #FFFF66;", logging.ERROR: "background: red; color: white;", logging.CRITICAL: "background: red; color: white; font: bold;", } level = log[LeapLogHandler.RECORD_KEY].levelno message = cgi.escape(log[LeapLogHandler.MESSAGE_KEY]) if self._logs_to_display[level]: open_tag = "<tr style='" + html_style[level] + "'>" open_tag += "<td width='100%' style='padding: 5px;'>" close_tag = "</td></tr>" message = open_tag + message + close_tag filter_by = self._current_filter msg = message if self.ui.cbCaseInsensitive.isChecked(): msg = msg.upper() filter_by = filter_by.upper() if msg.find(filter_by) != -1: self.ui.txtLogHistory.append(message) def _load_history(self): """ Load the previous logged messages in the widget. They are stored in the custom handler. """ self._set_logs_to_display() self.ui.txtLogHistory.clear() history = self._logging_handler.log_history for line in history: self._add_log_line(line) def _set_logs_to_display(self): """ Sets the logs_to_display dict getting the toggled options from the ui """ self._logs_to_display = { logging.DEBUG: self.ui.btnDebug.isChecked(), logging.INFO: self.ui.btnInfo.isChecked(), logging.WARNING: self.ui.btnWarning.isChecked(), logging.ERROR: self.ui.btnError.isChecked(), logging.CRITICAL: self.ui.btnCritical.isChecked(), } def _filter_by(self, text): """ Sets the text to use for filtering logs in the log window. :param text: the text to compare with the logs when filtering. :type text: str """ self._current_filter = text self._load_history() def _save_log_to_file(self): """ Lets the user save the current log to a file """ fileName, filtr = QtGui.QFileDialog.getSaveFileName( self, self.tr("Save As"), options=QtGui.QFileDialog.DontUseNativeDialog ) if fileName: try: with open(fileName, "w") as output: history = self.ui.txtLogHistory.toPlainText() # Chop some \n. # html->plain adds several \n because the html is made # using table cells. history = history.replace("\n\n\n", "\n") output.write(history) logger.debug("Log saved in %s" % (fileName,)) except IOError, e: logger.error("Error saving log file: %r" % (e,)) else:
class LoggerWindow(QtGui.QDialog): """ Window that displays a history of the logged messages in the app. """ _paste_ok = QtCore.Signal(object) _paste_error = QtCore.Signal(object) def __init__(self, parent): """ Initialize the widget. """ QtGui.QDialog.__init__(self, parent) # Load UI self.ui = Ui_LoggerWindow() self.ui.setupUi(self) # Make connections self.ui.btnSave.clicked.connect(self._save_log_to_file) self.ui.btnDebug.toggled.connect(self._load_history), self.ui.btnInfo.toggled.connect(self._load_history), self.ui.btnWarning.toggled.connect(self._load_history), self.ui.btnError.toggled.connect(self._load_history), self.ui.btnCritical.toggled.connect(self._load_history) self.ui.leFilterBy.textEdited.connect(self._filter_by) self.ui.cbCaseInsensitive.stateChanged.connect(self._load_history) self.ui.btnPastebin.clicked.connect(self._pastebin_this) self._paste_ok.connect(self._pastebin_ok) self._paste_error.connect(self._pastebin_err) self._current_filter = "" self._current_history = "" self._set_logs_to_display() LOG_CONTROLLER.new_log.connect(self._add_log_line) self._load_history() def _add_log_line(self, log): """ Adds a line to the history, only if it's in the desired levels to show. :param log: a log record to be inserted in the widget :type log: Logbook.LogRecord. """ html_style = { logbook.DEBUG: "background: #CDFFFF;", logbook.INFO: "background: white;", logbook.WARNING: "background: #FFFF66;", logbook.ERROR: "background: red; color: white;", logbook.CRITICAL: "background: red; color: white; font: bold;" } level = log.level message = cgi.escape(log.msg) if self._logs_to_display[level]: open_tag = "<tr style='" + html_style[level] + "'>" open_tag += "<td width='100%' style='padding: 5px;'>" close_tag = "</td></tr>" message = open_tag + message + close_tag filter_by = self._current_filter msg = message if self.ui.cbCaseInsensitive.isChecked(): msg = msg.upper() filter_by = filter_by.upper() if msg.find(filter_by) != -1: self.ui.txtLogHistory.append(message) def _load_history(self): """ Load the previous logged messages in the widget. They are stored in the custom handler. """ self._set_logs_to_display() self.ui.txtLogHistory.clear() current_history = [] for record in LOG_CONTROLLER.get_logs(): self._add_log_line(record) current_history.append(record.msg) self._current_history = "\n".join(current_history) def _set_logs_to_display(self): """ Sets the logs_to_display dict getting the toggled options from the ui """ self._logs_to_display = { logbook.DEBUG: self.ui.btnDebug.isChecked(), logbook.INFO: self.ui.btnInfo.isChecked(), logbook.WARNING: self.ui.btnWarning.isChecked(), logbook.ERROR: self.ui.btnError.isChecked(), logbook.CRITICAL: self.ui.btnCritical.isChecked() } def _filter_by(self, text): """ Sets the text to use for filtering logs in the log window. :param text: the text to compare with the logs when filtering. :type text: str """ self._current_filter = text self._load_history() def _save_log_to_file(self): """ Lets the user save the current log to a file """ fileName, filtr = QtGui.QFileDialog.getSaveFileName( self, self.tr("Save As"), options=QtGui.QFileDialog.DontUseNativeDialog) if fileName: try: with open(fileName, 'w') as output: history = self.ui.txtLogHistory.toPlainText() # Chop some \n. # html->plain adds several \n because the html is made # using table cells. history = history.replace('\n\n\n', '\n') output.write(history) logger.debug('Log saved in %s' % (fileName, )) except IOError, e: logger.error("Error saving log file: %r" % (e, )) else: