Source code for vdat.gui.logger_widget

# Virus Data Analysis Tool: a data reduction GUI for HETDEX/VIRUS data
# Copyright (C) 2016, 2017, 2018  "The HETDEX collaboration"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""Logging handlers

.. todo:

    move it to libvdat/gui

"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import logging

from qtpy import QtCore, QtWidgets


# Dict of HTML colours to shade the messages
_msgCols = {"CRITICAL": "red",
            "ERROR": "red",
            "WARNING": "Chocolate",
            "INFO": "black",
            "DEBUG": "black"}


[docs]class TextWindowHandler(QtCore.QObject, logging.Handler): """This is an implementation of a logging Handler that prints log messages to a QTextEdit widget .. list-table:: **Custom signals** :header-rows: 1 * - Name - Signature - Description * - :attr:`postText` - string - emit the signal with the text to submit .. list-table:: **Custom slot** :header-rows: 1 * - Name - Signature - Description * - :meth:`post_to_panel` - string - append the incoming message to :attr:`textBrowser` .. list-table:: **Connections between custom signals and/or slots**. :header-rows: 1 * - Signal - Slot * - :attr:`postText` - :meth:`post_to_panel` Parameters ---------- browser : :class:`PyQt5.QtWidgets.QTextEdit` A text edit widget to write the information to parent : qobject, optional parent of the QObject used to create the logger Attributes ---------- textBrowser : :class:`PyQt5.QtWidgets.QTextEdit` text widget where the log message is posted """ postText = QtCore.Signal(str) def __init__(self, browser, parent=None): QtCore.QObject.__init__(self, parent) logging.Handler.__init__(self) # handle is old style class in py2.7 self.textBrowser = browser self.postText.connect(self.post_to_panel)
[docs] def emit(self, record): """Construct a string in HTML out of the record, and emit a signal that tells another function to update the text browser Parameters ---------- record : :class:`logging.LogRecord` a record from a logger """ f = "[%(asctime)s] %(levelname)s: %(message)s" fmtr = logging.Formatter(fmt=f) msg = '<font color={color}> {message} </font>' msg = msg.format(color=_msgCols[record.levelname], message=fmtr.format(record)) self.postText.emit(msg)
[docs] @QtCore.Slot(str) def post_to_panel(self, msg): """Update the text window in the GUI by appending ``msg``. This method is also a PyQt slot. Parameters ---------- msg : String A message to post to the test browser window """ self.textBrowser.append(msg)
[docs]class LoggerWidget(QtWidgets.QTextEdit): """Widget where the logging messages are shown""" def __init__(self, parent=None): super(LoggerWidget, self).__init__(parent=parent) self.setEnabled(True) self.setReadOnly(True) self.setAcceptDrops(False) self.setObjectName("log_panel") # Attach this to the logger logger = logging.getLogger('logger') handler = TextWindowHandler(browser=self, parent=self) handler.setLevel(logging.INFO) logger.addHandler(handler)