Skip to content

Commit

Permalink
Logging (#208)
Browse files Browse the repository at this point in the history
* Add logging tab

* Setup logging in GUI tab

* Ruff

* Rename

* Adjust import

* Adding overwrite to close of log_tab widget

* Cosmetics

* Overwrite close event of global app to close logging

* Rename

* Rename

* remove unneccessary confirmationwq

---------

Co-authored-by: Staiger, Christine <christine.staiger@wur.nl>
  • Loading branch information
chStaiger and Staiger, Christine authored Jun 26, 2024
1 parent 13f4bbd commit 441d92a
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 7 deletions.
25 changes: 18 additions & 7 deletions ibridgesgui/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from ibridgesgui.browser import Browser
from ibridgesgui.config import get_log_level, init_logger, set_log_level
from ibridgesgui.gui_utils import UI_FILE_DIR
from ibridgesgui.info import Info
from ibridgesgui.login import Login
from ibridgesgui.logviewer import LogViewer
from ibridgesgui.popup_widgets import CheckConfig
from ibridgesgui.search import Search
from ibridgesgui.sync import Sync
Expand All @@ -34,10 +34,9 @@ class MainMenu(PyQt6.QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, app_name):
"""Initialise the main window."""
super().__init__()
if getattr(sys, "frozen", False):
super().setupUi(self)
else:
PyQt6.uic.loadUi(UI_FILE_DIR / "MainMenu.ui", self)
super().setupUi(self)

app.aboutToQuit.connect(self.close_event)

self.logger = logging.getLogger(app_name)

Expand All @@ -49,6 +48,7 @@ def __init__(self, app_name):
"tabSync": self.init_sync_tab,
"tabSearch": self.init_search_tab,
"tabInfo": self.init_info_tab,
"tabLog": self.init_log_tab
}

self.session = None
Expand All @@ -61,6 +61,7 @@ def __init__(self, app_name):
self.action_check_configuration.triggered.connect(self.inspect_env_file)
self.tab_widget.setCurrentIndex(0)


def disconnect(self):
"""Close iRODS session."""
self.error_label.clear()
Expand Down Expand Up @@ -109,6 +110,11 @@ def exit(self):
else:
pass

def close_event(self):
"""Close program properly if main window is closed."""
self.disconnect()
sys.exit()

def setup_tabs(self):
"""Init tab view."""
for tab_fun in self.ui_tabs_lookup.values():
Expand All @@ -125,6 +131,11 @@ def init_info_tab(self):
irods_info = Info(self.session)
self.tab_widget.addTab(irods_info, "Info")

def init_log_tab(self):
"""Create log tab."""
ibridges_log = LogViewer(self.logger)
self.tab_widget.addTab(ibridges_log, "Logs")

def init_browser_tab(self):
"""Create browser."""
self.irods_browser = Browser(self.session, self.app_name)
Expand Down Expand Up @@ -159,8 +170,8 @@ def main():
if log_level is not None:
init_logger(THIS_APPLICATION, log_level)
else:
set_log_level("error")
init_logger(THIS_APPLICATION, "error")
set_log_level("debug")
init_logger(THIS_APPLICATION, "debug")
main_widget = PyQt6.QtWidgets.QStackedWidget()
main_app = MainMenu(THIS_APPLICATION)
main_widget.addWidget(main_app)
Expand Down
50 changes: 50 additions & 0 deletions ibridgesgui/logviewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Logging tab."""

import logging
import sys

import PyQt6

from ibridgesgui.config import CONFIG_DIR
from ibridgesgui.gui_utils import UI_FILE_DIR
from ibridgesgui.ui_files.tabLogging import Ui_tabLogging


class QTextEditLogger(logging.Handler, PyQt6.QtCore.QObject):
"""Logging in a Qt text browser."""

append_plain_text = PyQt6.QtCore.pyqtSignal(str)

def __init__(self, text_browser):
"""Initialise."""
super().__init__()
PyQt6.QtCore.QObject.__init__(self)
self.widget = text_browser
self.widget.setReadOnly(True)
self.append_plain_text.connect(self.widget.insertPlainText)

def emit(self, record):
"""Emit when new logging accurs."""
msg = self.format(record)+"\n"
self.append_plain_text.emit(msg)


class LogViewer(PyQt6.QtWidgets.QWidget, Ui_tabLogging):
"""Set iBridges logging in GUI."""

def __init__(self, logger):
"""Initialise the tab."""
super().__init__()
if getattr(sys, "frozen", False):
super().setupUi(self)
else:
PyQt6.uic.loadUi(UI_FILE_DIR / "tabLogging.ui", self)

self.logger = logger
self.log_label.setText(str(CONFIG_DIR))
self.log_text = QTextEditLogger(self.log_browser)
self.log_text.setFormatter(
logging.Formatter(
'%(asctime)s %(levelname)s %(module)s %(funcName)s %(message)s'))
self.logger.addHandler(self.log_text)
self.logger.setLevel(logging.DEBUG)
49 changes: 49 additions & 0 deletions ibridgesgui/ui_files/tabLogging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Form implementation generated from reading ui file 'ibridgesgui/ui_files/tabLogging.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_tabLogging(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 244)
Form.setStyleSheet("QWidget\n"
"{\n"
" background-color: rgb(211,211,211);\n"
" color: rgb(88, 88, 90);\n"
" selection-background-color: rgb(21, 165, 137);\n"
" selection-color: rgb(245, 244, 244)\n"
"}\n"
"\n"
"QLabel\n"
"{\n"
" background-color: rgb(211,211,211);\n"
"}\n"
"\n"
"QTextBrowser\n"
"{\n"
" background-color: rgb(245, 244, 244)\n"
"}\n"
"")
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.log_label = QtWidgets.QLabel(parent=Form)
self.log_label.setObjectName("log_label")
self.verticalLayout.addWidget(self.log_label)
self.log_browser = QtWidgets.QTextBrowser(parent=Form)
self.log_browser.setObjectName("log_browser")
self.verticalLayout.addWidget(self.log_browser)

self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.log_label.setText(_translate("Form", "Log file"))
51 changes: 51 additions & 0 deletions ibridgesgui/ui_files/tabLogging.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>244</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<property name="styleSheet">
<string notr="true">QWidget
{
background-color: rgb(211,211,211);
color: rgb(88, 88, 90);
selection-background-color: rgb(21, 165, 137);
selection-color: rgb(245, 244, 244)
}

QLabel
{
background-color: rgb(211,211,211);
}

QTextBrowser
{
background-color: rgb(245, 244, 244)
}
</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="log_label">
<property name="text">
<string>Log file</string>
</property>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="log_browser"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit 441d92a

Please sign in to comment.