-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
GUI: Add "Generate debug log" action #3328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
C-Achard
wants to merge
18
commits into
main
Choose a base branch
from
cy/debug-logger-ui
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1676f0d
Add debug recorder and reporting utilities
C-Achard 85612c1
Add debug report dialog and helpers
C-Achard c59cfba
Add debug log generation action to Help menu
C-Achard c628491
Add tests for debug logger utilities
C-Achard 8c8e208
Rename debug tests
C-Achard 1efb65a
Add license headers and tidy debug exports
C-Achard 881a9b4
Use namespaced GUI loggers
C-Achard 3965fa9
Reuse DebugTextDialog and rename hint arg
C-Achard 1e48fa6
Make debug timing and recorder configurable
C-Achard b0de21f
Fix tests
C-Achard cfe82a1
Move reload_debug_settings_from_env to debug_logger
C-Achard fa4c643
Show exec even if default None
C-Achard 8a82406
Show wait cursor while building debug text
C-Achard 968d8f1
Auto-init debug recorder logger level
C-Achard 67e250c
Fix logger level docstring
C-Achard 4f7c8e5
Snapshot iterables and remove redundant refresh
C-Achard 76c844d
Prefer module version for OpenCV; adjust logger API
C-Achard f213948
Export DebugSection and add tests for debug report
C-Achard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add debug report dialog and helpers
Introduce a reusable debug text dialog and helper functions for generating and displaying DeepLabCut diagnostic reports. Adds deeplabcut/gui/dialogs/debug_dialog.py implementing DebugTextDialog (read-only log view with refresh/copy/keyboard shortcut), providers to render logs and full issue reports, show_debug_report_dialog to install/reuse recorders and present the report, and create_generate_debug_log_action to wire up a QAction. Also exports these symbols via deeplabcut/gui/dialogs/__init__.py. Dialog instances are attached to the parent to avoid GC and duplicate windows.
- Loading branch information
commit 85612c1866919f248151944eb4c41729f4a0993d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from collections.abc import Sequence | ||
|
|
||
| from .debug_dialog import ( | ||
| DebugTextDialog, | ||
| create_generate_debug_log_action, | ||
| make_issue_report_provider, | ||
| make_log_text_provider, | ||
| show_debug_report_dialog, | ||
| ) | ||
|
|
||
| __all__: Sequence[str] = ( | ||
| "DebugTextDialog", | ||
| "create_generate_debug_log_action", | ||
| "make_issue_report_provider", | ||
| "make_log_text_provider", | ||
| "show_debug_report_dialog", | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,296 @@ | ||
| # | ||
| # DeepLabCut Toolbox (deeplabcut.org) | ||
| # © A. & M.W. Mathis Labs | ||
| # https://github.com/DeepLabCut/DeepLabCut | ||
| # | ||
| # Please see AUTHORS for contributors. | ||
| # https://github.com/DeepLabCut/DeepLabCut/blob/master/AUTHORS | ||
| # | ||
| # Licensed under GNU Lesser General Public License v3.0 | ||
| # | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable, Iterable | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from PySide6 import QtGui | ||
| from PySide6.QtCore import Qt | ||
| from PySide6.QtGui import QAction, QFontDatabase, QKeySequence, QTextCursor | ||
| from PySide6.QtWidgets import ( | ||
| QApplication, | ||
| QDialog, | ||
| QHBoxLayout, | ||
| QLabel, | ||
| QPlainTextEdit, | ||
| QPushButton, | ||
| QVBoxLayout, | ||
| QWidget, | ||
| ) | ||
|
|
||
| from deeplabcut.core.debug import ( | ||
| ExecutableSpec, | ||
| InMemoryDebugRecorder, | ||
| LibrarySpec, | ||
| build_debug_report, | ||
| get_debug_recorder, | ||
| install_debug_recorder, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| pass | ||
|
|
||
|
|
||
| def make_log_text_provider( | ||
| *, | ||
| recorder: InMemoryDebugRecorder | None, | ||
| limit: int = 300, | ||
| ) -> Callable[[], str]: | ||
| """Return a callable that renders recent captured logs.""" | ||
|
|
||
| def _provider() -> str: | ||
| if recorder is None: | ||
| return "<debug recorder unavailable>" | ||
| return recorder.render_text(limit=limit) | ||
|
|
||
| return _provider | ||
|
|
||
|
|
||
| def make_issue_report_provider( | ||
| *, | ||
| recorder: InMemoryDebugRecorder | None, | ||
| libraries: Iterable[LibrarySpec | str] | None = None, | ||
| executables: Iterable[ExecutableSpec | str] | None = None, | ||
| include_module_paths: bool = False, | ||
| include_executable_paths: bool = True, | ||
| log_limit: int = 300, | ||
| ) -> Callable[[], str]: | ||
| """Return a callable that builds a full DLC debug report.""" | ||
|
|
||
| def _provider() -> str: | ||
| return build_debug_report( | ||
| recorder=recorder, | ||
| libraries=libraries, | ||
| executables=executables, | ||
|
C-Achard marked this conversation as resolved.
Outdated
|
||
| include_module_paths=include_module_paths, | ||
| include_executable_paths=include_executable_paths, | ||
| log_limit=log_limit, | ||
| ) | ||
|
|
||
| return _provider | ||
|
|
||
|
|
||
| class DebugTextDialog(QDialog): | ||
| """ | ||
| Minimal, application-agnostic debug text viewer. | ||
|
|
||
| This widget only knows how to: | ||
| - fetch text from a callable | ||
| - display it read-only | ||
| - copy it to clipboard | ||
| - refresh it on demand | ||
|
|
||
| It intentionally knows nothing about: | ||
| - recorder internals | ||
| - DLC main window internals | ||
| - environment/report formatting | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| title: str, | ||
| text_provider: Callable[[], str], | ||
| parent: QWidget | None = None, | ||
| initial_hint: str = "Read-only diagnostic output", | ||
| ) -> None: | ||
| super().__init__(parent=parent) | ||
| self.setWindowTitle(title) | ||
| self.setModal(False) | ||
| self.resize(950, 700) | ||
|
|
||
| self._text_provider = text_provider | ||
|
|
||
| self._build_ui(initial_hint=initial_hint) | ||
| self.refresh_text() | ||
|
|
||
| def _build_ui(self, *, initial_hint: str) -> None: | ||
| layout = QVBoxLayout(self) | ||
|
|
||
| self._hint_label = QLabel(initial_hint, self) | ||
| self._hint_label.setTextInteractionFlags(Qt.TextSelectableByMouse) | ||
| layout.addWidget(self._hint_label) | ||
|
|
||
| self._text_edit = QPlainTextEdit(self) | ||
| self._text_edit.setReadOnly(True) | ||
| self._text_edit.setLineWrapMode(QPlainTextEdit.NoWrap) | ||
|
|
||
| # Use a fixed-width system font for logs / reports | ||
| font = QFontDatabase.systemFont(QFontDatabase.SystemFont.FixedFont) | ||
| self._text_edit.setFont(font) | ||
|
|
||
| layout.addWidget(self._text_edit, stretch=1) | ||
|
|
||
| button_row = QHBoxLayout() | ||
|
|
||
| self._status_label = QLabel("", self) | ||
| self._status_label.setTextInteractionFlags(Qt.TextSelectableByMouse) | ||
| button_row.addWidget(self._status_label, stretch=1) | ||
|
|
||
| self._refresh_btn = QPushButton("Refresh", self) | ||
| self._refresh_btn.clicked.connect(self.refresh_text) | ||
| button_row.addWidget(self._refresh_btn) | ||
|
|
||
| self._copy_btn = QPushButton("Copy to clipboard", self) | ||
| self._copy_btn.clicked.connect(self.copy_to_clipboard) | ||
| button_row.addWidget(self._copy_btn) | ||
|
|
||
| self._close_btn = QPushButton("Close", self) | ||
| self._close_btn.clicked.connect(self.close) | ||
| button_row.addWidget(self._close_btn) | ||
|
|
||
| layout.addLayout(button_row) | ||
|
|
||
| # Optional keyboard shortcut | ||
| copy_action = QAction(self) | ||
| copy_action.setShortcut(QKeySequence.StandardKey.Copy) | ||
| copy_action.triggered.connect(self.copy_to_clipboard) | ||
| self.addAction(copy_action) | ||
|
|
||
| def refresh_text(self) -> None: | ||
| try: | ||
| text = self._text_provider() | ||
| except Exception as exc: | ||
| text = f"[debug-dialog] failed to build debug text\n\n{exc!r}" | ||
|
|
||
| self._text_edit.setPlainText(text or "<no debug text available>") | ||
|
C-Achard marked this conversation as resolved.
|
||
| self._text_edit.moveCursor(QTextCursor.MoveOperation.Start) | ||
| self._status_label.setText("") | ||
|
|
||
| def copy_to_clipboard(self) -> None: | ||
| try: | ||
| text = self._text_edit.toPlainText() | ||
| QApplication.clipboard().setText(text) | ||
| self._status_label.setText("Copied to clipboard") | ||
| except Exception: | ||
| self._status_label.setText("Could not copy to clipboard") | ||
|
|
||
| def showEvent(self, event: QtGui.QShowEvent) -> None: | ||
| """Refresh each time the dialog becomes visible.""" | ||
| super().showEvent(event) | ||
| self.refresh_text() | ||
|
|
||
|
|
||
| def _get_or_create_debug_dialog( | ||
| *, | ||
| parent: QWidget, | ||
| title: str, | ||
| text_provider: Callable[[], str], | ||
| initial_hint: str, | ||
| attr_name: str = "_dlc_debug_dialog", | ||
| ) -> DebugTextDialog: | ||
| """ | ||
| Reuse a single dialog instance attached to ``parent``. | ||
|
|
||
| Storing the dialog on the main window avoids accidental garbage collection | ||
| and prevents opening a pile of duplicate windows. | ||
| """ | ||
| dlg = getattr(parent, attr_name, None) | ||
| if isinstance(dlg, DebugTextDialog): | ||
| return dlg | ||
|
|
||
| dlg = DebugTextDialog( | ||
| title=title, | ||
| text_provider=text_provider, | ||
| parent=parent, | ||
|
C-Achard marked this conversation as resolved.
|
||
| initial_hint=initial_hint, | ||
| ) | ||
| setattr(parent, attr_name, dlg) | ||
| return dlg | ||
|
|
||
|
|
||
| def show_debug_report_dialog( | ||
| *, | ||
| parent: QWidget, | ||
| recorder: InMemoryDebugRecorder | None = None, | ||
| logger_name: str = "deeplabcut", | ||
| libraries: Iterable[LibrarySpec | str] | None = None, | ||
| executables: Iterable[ExecutableSpec | str] | None = None, | ||
| include_module_paths: bool = False, | ||
| include_executable_paths: bool = True, | ||
| log_limit: int = 300, | ||
| dialog_attr_name: str = "_dlc_debug_dialog", | ||
| ) -> DebugTextDialog: | ||
| """ | ||
| Open (or reuse) the full diagnostic report dialog. | ||
|
|
||
| If ``recorder`` is not provided, this function tries to reuse an existing | ||
| recorder for the given logger namespace and installs one if missing. | ||
| """ | ||
| if recorder is None: | ||
| recorder = get_debug_recorder(logger_name=logger_name) | ||
| if recorder is None: | ||
| recorder = install_debug_recorder(logger_name=logger_name) | ||
|
|
||
| provider = make_issue_report_provider( | ||
| recorder=recorder, | ||
| libraries=libraries, | ||
| executables=executables, | ||
| include_module_paths=include_module_paths, | ||
| include_executable_paths=include_executable_paths, | ||
| log_limit=log_limit, | ||
| ) | ||
|
|
||
| dlg = _get_or_create_debug_dialog( | ||
| parent=parent, | ||
| title="DeepLabCut debug log", | ||
| text_provider=provider, | ||
| initial_hint=("Diagnostic report for issue reporting. Use Refresh to update, then Copy to clipboard."), | ||
| attr_name=dialog_attr_name, | ||
| ) | ||
| dlg.refresh_text() | ||
|
C-Achard marked this conversation as resolved.
Outdated
|
||
| dlg.show() | ||
| dlg.raise_() | ||
| dlg.activateWindow() | ||
| return dlg | ||
|
|
||
|
|
||
| def create_generate_debug_log_action( | ||
| *, | ||
| parent: QWidget, | ||
| recorder: InMemoryDebugRecorder | None = None, | ||
| logger_name: str = "deeplabcut", | ||
| libraries: Iterable[LibrarySpec | str] | None = None, | ||
| executables: Iterable[ExecutableSpec | str] | None = None, | ||
| include_module_paths: bool = False, | ||
| include_executable_paths: bool = True, | ||
| log_limit: int = 300, | ||
| text: str = "&Generate debug log...", | ||
| status_tip: str = "Generate a diagnostic report for troubleshooting", | ||
| dialog_attr_name: str = "_dlc_debug_dialog", | ||
| ) -> QAction: | ||
| """ | ||
| Create a QAction that opens the DLC debug report dialog. | ||
|
|
||
| Typical usage in ``MainWindow.create_actions``:: | ||
|
|
||
| self.generateDebugLogAction = create_generate_debug_log_action(parent=self) | ||
| """ | ||
| action = QAction(text, parent) | ||
| action.setStatusTip(status_tip) | ||
|
|
||
| def _open_dialog() -> None: | ||
| show_debug_report_dialog( | ||
| parent=parent, | ||
| recorder=recorder, | ||
| logger_name=logger_name, | ||
| libraries=libraries, | ||
| executables=executables, | ||
| include_module_paths=include_module_paths, | ||
| include_executable_paths=include_executable_paths, | ||
| log_limit=log_limit, | ||
| dialog_attr_name=dialog_attr_name, | ||
| ) | ||
|
|
||
| action.triggered.connect(_open_dialog) | ||
| return action | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.