Logging
This commit is contained in:
@@ -6,12 +6,15 @@ from typing import Protocol
|
||||
from PySide6.QtCore import QEvent, QObject
|
||||
|
||||
from bedit_gui.documents import Document
|
||||
from bedit_gui.services.application_logging import get_logger
|
||||
from bedit_gui.views.dialogs.document_dialogs import (
|
||||
DocumentDialogs,
|
||||
SaveChangesChoice,
|
||||
)
|
||||
from bedit_gui.views.main_window import MainWindow
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class DocumentDialogProvider(Protocol):
|
||||
def choose_open_path(self, current_path: Path | None) -> Path | None: ...
|
||||
@@ -51,6 +54,7 @@ class DocumentController(QObject):
|
||||
def new_document(self) -> None:
|
||||
if self.maybe_save_changes():
|
||||
self.document.new()
|
||||
logger.info("Created new document")
|
||||
|
||||
def open_document(self) -> None:
|
||||
if not self.maybe_save_changes():
|
||||
@@ -63,7 +67,10 @@ class DocumentController(QObject):
|
||||
try:
|
||||
self.document.open(path)
|
||||
except (OSError, TypeError, ValueError) as exc:
|
||||
logger.exception("Could not open document %s", path)
|
||||
self.dialogs.show_file_error("Could not open document", exc)
|
||||
else:
|
||||
logger.info("Opened document: %s", path)
|
||||
|
||||
def save_document(self) -> bool:
|
||||
if self.document.path is None:
|
||||
@@ -71,8 +78,10 @@ class DocumentController(QObject):
|
||||
try:
|
||||
self.document.save()
|
||||
except (OSError, TypeError, ValueError) as exc:
|
||||
logger.exception("Could not save document %s", self.document.path)
|
||||
self.dialogs.show_file_error("Could not save document", exc)
|
||||
return False
|
||||
logger.info("Saved document: %s", self.document.path)
|
||||
return True
|
||||
|
||||
def save_document_as(self) -> bool:
|
||||
@@ -83,8 +92,10 @@ class DocumentController(QObject):
|
||||
try:
|
||||
self.document.save_as(path)
|
||||
except (OSError, TypeError, ValueError) as exc:
|
||||
logger.exception("Could not save document as %s", path)
|
||||
self.dialogs.show_file_error("Could not save document", exc)
|
||||
return False
|
||||
logger.info("Saved document as: %s", path)
|
||||
return True
|
||||
|
||||
def maybe_save_changes(self) -> bool:
|
||||
|
||||
51
src/bedit_gui/controllers/log_controller.py
Normal file
51
src/bedit_gui/controllers/log_controller.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from PySide6.QtCore import QObject, Signal
|
||||
|
||||
from bedit_gui.services.application_logging import configure_logging
|
||||
from bedit_gui.views.main_window import MainWindow
|
||||
from bedit_gui.views.models import LogListModel
|
||||
|
||||
|
||||
class _LogEmitter(QObject):
|
||||
message = Signal(str)
|
||||
|
||||
|
||||
class _QtLogHandler(logging.Handler):
|
||||
def __init__(self, emitter: _LogEmitter) -> None:
|
||||
super().__init__()
|
||||
self._emitter = emitter
|
||||
|
||||
def emit(self, record: logging.LogRecord) -> None:
|
||||
try:
|
||||
self._emitter.message.emit(self.format(record))
|
||||
except (RuntimeError, TypeError, ValueError):
|
||||
self.handleError(record)
|
||||
|
||||
|
||||
class LogController(QObject):
|
||||
"""Routes standard application log records into the log list view."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
window: MainWindow,
|
||||
level: int | str = logging.INFO,
|
||||
) -> None:
|
||||
super().__init__(window)
|
||||
|
||||
self.model = LogListModel()
|
||||
self.emitter = _LogEmitter(self)
|
||||
self.handler = _QtLogHandler(self.emitter)
|
||||
self.handler.setFormatter(
|
||||
logging.Formatter(
|
||||
"%(asctime)s %(levelname)s %(message)s",
|
||||
datefmt="%H:%M:%S",
|
||||
)
|
||||
)
|
||||
|
||||
self.emitter.message.connect(self.model.append)
|
||||
self.model.rowsInserted.connect(window.ui.listView.scrollToBottom)
|
||||
window.ui.listView.setModel(self.model)
|
||||
configure_logging(self.handler, level)
|
||||
@@ -1,8 +1,11 @@
|
||||
from PySide6.QtCore import QObject
|
||||
|
||||
from bedit_gui.documents import Document
|
||||
from bedit_gui.services.application_logging import get_logger
|
||||
from bedit_gui.views.main_window import MainWindow
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class UndoController(QObject):
|
||||
def __init__(self, document: Document, window: MainWindow) -> None:
|
||||
@@ -15,8 +18,8 @@ class UndoController(QObject):
|
||||
redo_action = window.ui.actionRedo
|
||||
undo_stack = document.undo_stack
|
||||
|
||||
undo_action.triggered.connect(undo_stack.undo)
|
||||
redo_action.triggered.connect(undo_stack.redo)
|
||||
undo_action.triggered.connect(self.undo)
|
||||
redo_action.triggered.connect(self.redo)
|
||||
|
||||
undo_stack.canUndoChanged.connect(undo_action.setEnabled)
|
||||
undo_stack.canRedoChanged.connect(redo_action.setEnabled)
|
||||
@@ -27,10 +30,20 @@ class UndoController(QObject):
|
||||
undo_action.setEnabled(undo_stack.canUndo())
|
||||
redo_action.setEnabled(undo_stack.canRedo())
|
||||
|
||||
def undo(self) -> None:
|
||||
command = self.document.undo_stack.undoText()
|
||||
self.document.undo_stack.undo()
|
||||
logger.info("Undo: %s", command)
|
||||
|
||||
def redo(self) -> None:
|
||||
command = self.document.undo_stack.redoText()
|
||||
self.document.undo_stack.redo()
|
||||
logger.info("Redo: %s", command)
|
||||
|
||||
def _update_undo_text(self, command: str) -> None:
|
||||
text = f"Undo {command}" if command else "Undo"
|
||||
self.window.ui.actionUndo.setText(text)
|
||||
|
||||
def _update_redo_text(self, command: str) -> None:
|
||||
text = f"Redo {command}" if command else "Redo"
|
||||
self.window.ui.actionRedo.setText(text)
|
||||
self.window.ui.actionRedo.setText(text)
|
||||
|
||||
Reference in New Issue
Block a user