Add a log window to see the current log

This commit is contained in:
Timmy Welch 2022-06-05 17:34:51 -07:00
parent 62d927a104
commit b5e6e41043
4 changed files with 153 additions and 1 deletions

View File

@ -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")

View File

@ -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")

View File

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Log Window</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textEdit">
<property name="readOnly">
<bool>true</bool>
</property>
<property name="acceptRichText">
<bool>false</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -1182,7 +1182,7 @@
<x>0</x>
<y>0</y>
<width>1096</width>
<height>21</height>
<height>30</height>
</rect>
</property>
<widget class="QMenu" name="menuComicTagger">
@ -1245,6 +1245,7 @@
<string>Window</string>
</property>
<addaction name="actionPageBrowser"/>
<addaction name="actionLogWindow"/>
</widget>
<addaction name="menuComicTagger"/>
<addaction name="menuTags"/>
@ -1455,6 +1456,11 @@
<string>Parse Filename and split words</string>
</property>
</action>
<action name="actionLogWindow">
<property name="text">
<string>Show Log Window</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>