Source code for gui.widget_logging

"""
This widget contains the functionality to display the logging statements
onto the GUI. The widget can be executed as a standalone version.

########################################
Functionalities provided in this widget:
########################################

1.  Choose between different logging levels Debug, Info, Warning, Error.
2.  The chopper states for Viper with wobbler experiment are displayed in
    the logging window if warning level is set.
"""
if __name__ == "__main__":
    # Add directories to path for imports
    import os, sys, inspect

    currentdir = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe()))
    )
    parentdir = os.path.dirname(currentdir)

    sys.path.insert(0, os.path.join(currentdir, "ui_files"))
    sys.path.insert(0, parentdir)

import time

# PyQt imports
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import QRunnable, QThreadPool, pyqtSignal

# Import RegEx input validators
from regex_validators import *

# Import Qt Threading wrapper
from qt_multithreading_wrapper import Worker

# Import of file made from UI designer
from ui_logging import Ui_GroupBox_log as Ui_log

import logging


[docs]class WidgetLogging(QtWidgets.QGroupBox, Ui_log): def __init__(self, *args, obj=None, **kwargs): super(QtWidgets.QGroupBox, self).__init__(*args, **kwargs) self.setupUi(self) # Instantiate threadpool self.threadpool = QThreadPool() self.logging_handler = QPlainTextEditLogger() self.logging_handler.setFormatter( logging.Formatter( "%(asctime)s %(levelname)s %(module)s %(funcName)s %(message)s" ) ) logging.getLogger().addHandler(self.logging_handler) logging.getLogger().setLevel(logging.INFO) self.logging_handler.log_arrived.connect(self.plainTextEdit_log.appendPlainText) self.radioButton_log_debug.clicked.connect( lambda: logging.getLogger().setLevel(logging.DEBUG) ) self.radioButton_log_info.clicked.connect( lambda: logging.getLogger().setLevel(logging.INFO) ) self.radioButton_log_warning.clicked.connect( lambda: logging.getLogger().setLevel(logging.WARNING) ) self.radioButton_log_error.clicked.connect( lambda: logging.getLogger().setLevel(logging.ERROR) ) # self.test_logging()
[docs] def test_logging(self): def run(): i = 0 while True: logging.info("""Counter {}""".format(i)) logging.debug("""Counter {}""".format(i)) logging.warning("""Counter {}""".format(i)) logging.error("""Counter {}""".format(i)) time.sleep(1) i += 1 if i == 200: break work = Worker(run) self.threadpool.start(work)
[docs]class QPlainTextEditLogger(logging.Handler, QtCore.QObject): log_arrived = pyqtSignal(str) def __init__(self): super().__init__() QtCore.QObject.__init__(self)
[docs] def emit(self, record): msg = self.format(record) self.log_arrived.emit(msg)
if __name__ == "__main__": class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, obj=None, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle("Please god let this work too") widget = WidgetLogging() self.setCentralWidget(widget) app = QtWidgets.QApplication(sys.argv) window = MainWindow() window.show() app.exec()