diff --git a/comictaggerlib/applicationlogwindow.py b/comictaggerlib/applicationlogwindow.py new file mode 100644 index 0000000..f37fa26 --- /dev/null +++ b/comictaggerlib/applicationlogwindow.py @@ -0,0 +1,50 @@ +from __future__ import annotations + +import logging + +from PyQt5 import QtCore, QtGui, QtWidgets, uic + +from comictaggerlib.settings import ComicTaggerSettings + +logger = logging.getLogger(__name__) + + +class QTextEditLogger(QtCore.QObject, logging.Handler): + qlog = QtCore.pyqtSignal(str) + + def __init__(self, formatter: logging.Formatter, level: int): + super().__init__() + self.setFormatter(formatter) + self.setLevel(level) + + def emit(self, record): + msg = self.format(record) + self.qlog.emit(msg.strip()) + + +class ApplicationLogWindow(QtWidgets.QDialog): + def __init__(self, log_handler: QTextEditLogger, parent=None): + super().__init__(parent) + uic.loadUi(ComicTaggerSettings.get_ui_file("logwindow.ui"), self) + + self.log_handler = log_handler + self.log_handler.qlog.connect(self.textEdit.append) + + f = QtGui.QFont("menlo") + f.setStyleHint(QtGui.QFont.Monospace) + self.setFont(f) + self._button = QtWidgets.QPushButton(self) + self._button.setText("Test Me") + + layout = self.layout() + layout.addWidget(self._button) + + # Connect signal to slot + self._button.clicked.connect(self.test) + self.textEdit.setTabStopDistance(self.textEdit.tabStopDistance() * 2) + + def test(self): + logger.debug("damn, a bug") + logger.info("something to remember") + logger.warning("that's not right") + logger.error("foobar") diff --git a/comictaggerlib/taggerwindow.py b/comictaggerlib/taggerwindow.py index 025d225..092a49c 100644 --- a/comictaggerlib/taggerwindow.py +++ b/comictaggerlib/taggerwindow.py @@ -39,6 +39,7 @@ from comicapi.filenameparser import FileNameParser from comicapi.genericmetadata import GenericMetadata from comicapi.issuestring import IssueString from comictaggerlib import ctversion +from comictaggerlib.applicationlogwindow import ApplicationLogWindow, QTextEditLogger from comictaggerlib.autotagmatchwindow import AutoTagMatchWindow from comictaggerlib.autotagprogresswindow import AutoTagProgressWindow from comictaggerlib.autotagstartwindow import AutoTagStartWindow @@ -83,6 +84,7 @@ class TaggerWindow(QtWidgets.QMainWindow): uic.loadUi(ComicTaggerSettings.get_ui_file("taggerwindow.ui"), self) self.settings = settings + self.log_window = self.setup_logger() # prevent multiple instances socket = QtNetwork.QLocalSocket(self) @@ -262,6 +264,20 @@ Have fun! # defer the actual close in the app loop thread QtCore.QTimer.singleShot(200, lambda: execute(self.close)) + def setup_logger(self) -> ApplicationLogWindow: + try: + current_logs = (ComicTaggerSettings.get_settings_folder() / "logs" / "ComicTagger.log").read_text("utf-8") + except Exception: + current_logs = "" + root_logger = logging.getLogger() + qapplogwindow = ApplicationLogWindow( + QTextEditLogger(logging.Formatter("%(asctime)s | %(name)s | %(levelname)s | %(message)s"), logging.DEBUG), + parent=self, + ) + qapplogwindow.textEdit.append(current_logs.strip()) + root_logger.addHandler(qapplogwindow.log_handler) + return qapplogwindow + def reset_app(self) -> None: self.archiveCoverWidget.clear() @@ -387,6 +403,9 @@ Have fun! self.actionPageBrowser.setShortcut("Ctrl+P") self.actionPageBrowser.setStatusTip("Show the page browser") self.actionPageBrowser.triggered.connect(self.show_page_browser) + self.actionLogWindow.setShortcut("Ctrl+Shift+L") + self.actionLogWindow.setStatusTip("Show the log window") + self.actionLogWindow.triggered.connect(self.log_window.show) # Help Menu self.actionAbout.setStatusTip("Show the " + self.appName + " info") diff --git a/comictaggerlib/ui/applicationlogwindow.ui b/comictaggerlib/ui/applicationlogwindow.ui new file mode 100644 index 0000000..77d8741 --- /dev/null +++ b/comictaggerlib/ui/applicationlogwindow.ui @@ -0,0 +1,77 @@ + + + Dialog + + + + 0 + 0 + 400 + 300 + + + + Log Window + + + + + + true + + + false + + + Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttonBox + accepted() + Dialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + Dialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/comictaggerlib/ui/taggerwindow.ui b/comictaggerlib/ui/taggerwindow.ui index c8981e7..337d2d4 100644 --- a/comictaggerlib/ui/taggerwindow.ui +++ b/comictaggerlib/ui/taggerwindow.ui @@ -1182,7 +1182,7 @@ 0 0 1096 - 21 + 30 @@ -1245,6 +1245,7 @@ Window + @@ -1455,6 +1456,11 @@ Parse Filename and split words + + + Show Log Window + +