2022-06-05 17:34:51 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from PyQt5 import QtCore, QtGui, QtWidgets, uic
|
|
|
|
|
2022-10-25 21:48:01 -07:00
|
|
|
from comictaggerlib.ui import ui_path
|
2022-06-05 17:34:51 -07:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class QTextEditLogger(QtCore.QObject, logging.Handler):
|
|
|
|
qlog = QtCore.pyqtSignal(str)
|
|
|
|
|
2022-07-18 12:17:13 -07:00
|
|
|
def __init__(self, formatter: logging.Formatter, level: int) -> None:
|
2022-06-05 17:34:51 -07:00
|
|
|
super().__init__()
|
|
|
|
self.setFormatter(formatter)
|
|
|
|
self.setLevel(level)
|
|
|
|
|
2022-07-18 12:17:13 -07:00
|
|
|
def emit(self, record: logging.LogRecord) -> None:
|
2022-06-05 17:34:51 -07:00
|
|
|
msg = self.format(record)
|
|
|
|
self.qlog.emit(msg.strip())
|
|
|
|
|
|
|
|
|
|
|
|
class ApplicationLogWindow(QtWidgets.QDialog):
|
2023-02-09 19:33:10 -08:00
|
|
|
def __init__(self, log_handler: QTextEditLogger, parent: QtCore.QObject | None = None) -> None:
|
2022-06-05 17:34:51 -07:00
|
|
|
super().__init__(parent)
|
2022-10-25 21:48:01 -07:00
|
|
|
uic.loadUi(ui_path / "logwindow.ui", self)
|
2022-06-05 17:34:51 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-07-18 12:17:13 -07:00
|
|
|
def test(self) -> None:
|
2022-06-05 17:34:51 -07:00
|
|
|
logger.debug("damn, a bug")
|
|
|
|
logger.info("something to remember")
|
|
|
|
logger.warning("that's not right")
|
|
|
|
logger.error("foobar")
|