Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yconst committed Apr 29, 2024
1 parent 4abe475 commit 80f44b7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
1 change: 1 addition & 0 deletions studio/Python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"docopt",
"flatten-dict",
"pint",
"pretty_errors"
],
extras_require={"gui": ["pyside6", "pyqtgraph>=0.13.3"]},
entry_points={
Expand Down
4 changes: 3 additions & 1 deletion studio/Python/tinymovr/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
display_file_save_dialog,
magnitude_of,
StreamRedirector,
QTextBrowserLogger,
TimedGetter,
check_selected_items,
get_dynamic_attrs,
is_dark_mode,
strtobool
strtobool,
configure_pretty_errors
)
from tinymovr.gui.widgets import (
NodeTreeWidgetItem,
Expand Down
66 changes: 62 additions & 4 deletions studio/Python/tinymovr/gui/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,29 @@
"""

import time
import logging
from datetime import datetime
import os
import enum
import pint
from PySide6 import QtGui
from PySide6.QtGui import QGuiApplication, QPalette
from PySide6.QtGui import QGuiApplication, QPalette, QTextCursor
from PySide6.QtWidgets import QMessageBox, QFileDialog
from avlos.definitions import RemoteAttribute, RemoteEnum, RemoteBitmask
import pretty_errors
import tinymovr


class ConsoleColor:
NORMAL = "<font color='white'>"
ERROR = "<font color='red'>"
WARNING = "<font color='orange'>"
INFO = "<font color='lightblue'>"
DEBUG = "<font color='lightgreen'>"
TIMESTAMP = "<font color='gray'>"
END = "</font>"


app_stylesheet = """
/* --------------------------------------- QPushButton -----------------------------------*/
Expand Down Expand Up @@ -467,21 +480,66 @@ def magnitude_of(val):


class StreamRedirector(object):
"""A class to redirect writes from a stream to a QPlainTextEdit."""
"""
A class to redirect writes from a stream to a QPlainTextEdit, including pretty_errors handling and timestamps.
"""
def __init__(self, widget):
self.widget = widget
self.buffer = ''

def write(self, message):
# Timestamp the message
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
message_with_timestamp = f"[{timestamp}] {message}"

# Redirect to the QPlainTextEdit widget
self.widget.moveCursor(QtGui.QTextCursor.End)
self.widget.insertPlainText(message)
self.widget.moveCursor(QtGui.QTextCursor.End)
self.widget.insertPlainText(message_with_timestamp)
self.widget.ensureCursorVisible()

def flush(self):
pass


class QTextBrowserLogger(logging.Handler):
"""A logging handler that directs logging output to a QTextBrowser widget."""
def __init__(self, widget):
super().__init__()
self.widget = widget
self.widget.setReadOnly(True)
self.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))

def emit(self, record):
msg = self.format(record)
self.widget.append(self.colorize_message(record.levelno, msg))

def colorize_message(self, level, message):
color = {
logging.DEBUG: "lightgreen",
logging.INFO: "lightblue",
logging.WARNING: "orange",
logging.ERROR: "red",
logging.CRITICAL: "purple"
}.get(level, "black")
timestamp = datetime.now().strftime('%H:%M:%S')
return f'<font color="gray">[{timestamp}]</font> <font color="{color}">{message}</font>'


def configure_pretty_errors():
pretty_errors.configure(
separator_character='*',
filename_display=pretty_errors.FILENAME_EXTENDED,
line_number_first=True,
display_link=True,
lines_before=5,
lines_after=2,
line_color=pretty_errors.RED + '> ' + pretty_errors.default_config.line_color,
code_color=' ' + pretty_errors.default_config.code_color,
truncate_code=True, # Truncate code lines to not overflow in the GUI
display_locals=True
)


class TimedGetter:
"""
An interface class that maintains timing
Expand Down
13 changes: 7 additions & 6 deletions studio/Python/tinymovr/gui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
QMessageBox,
QTreeWidgetItem,
QSplitter,
QPlainTextEdit
QTextBrowser
)
from PySide6.QtGui import QAction
import pyqtgraph as pg
Expand All @@ -54,11 +54,13 @@
PlaceholderQTreeWidget,
BoolTreeWidgetItem,
StreamRedirector,
QTextBrowserLogger,
format_value,
display_file_open_dialog,
display_file_save_dialog,
magnitude_of,
check_selected_items,
configure_pretty_errors
)


Expand All @@ -74,6 +76,7 @@ def __init__(self, app, arguments, logger):

self.start_time = time.time()
self.logger = logger if logger is not None else logging.getLogger("tinymovr")
configure_pretty_errors()

self.attr_widgets_by_id = {}
self.graphs_by_id = {}
Expand Down Expand Up @@ -142,10 +145,10 @@ def __init__(self, app, arguments, logger):
self.splitter.addWidget(self.left_frame)
self.splitter.addWidget(self.right_frame)

self.console = QPlainTextEdit()
self.console = QTextBrowser()
self.console.setReadOnly(True)
sys.stdout = StreamRedirector(self.console)
sys.stderr = StreamRedirector(self.console)
self.log_handler = QTextBrowserLogger(self.console)
self.logger.addHandler(self.log_handler)

self.main_splitter = QSplitter(QtCore.Qt.Vertical)
self.main_splitter.setHandleWidth(0)
Expand Down Expand Up @@ -219,8 +222,6 @@ def about_to_quit(self):
self.visibility_update_timer.stop()
self.worker_update_timer.stop()
self.worker.stop()
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

@QtCore.Slot()
def handle_worker_error(self, e):
Expand Down
2 changes: 1 addition & 1 deletion studio/Python/tinymovr/gui/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def _update(self):
def _device_appeared(self, device, node_id):
self.mutx.lock()
display_name = "{}{}".format(device.name, node_id)
self.logger.warn("Found {} with device uid {}".format(display_name, node_id))
self.logger.info("Found {} (uid {})".format(display_name, device.uid))
self.devices_by_name[display_name] = device
self.names_by_id[node_id] = display_name
device.name = display_name
Expand Down

0 comments on commit 80f44b7

Please sign in to comment.