| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import logging |
| 26 |
|
| 27 |
from ..log import LogHandler |
| 28 |
from ..Qt import QtWidgets, QtCore, QtGui |
| 29 |
from .tab_widget import TabWidget |
| 30 |
|
| 31 |
log = logging.getLogger(__name__) |
| 32 |
log.addHandler(logging.NullHandler()) |
| 33 |
|
| 34 |
|
| 35 |
class HTMLFormatter(logging.Formatter): |
| 36 |
level_colors = { |
| 37 |
"DEBUG": "DarkGray", |
| 38 |
# "INFO": "Black", |
| 39 |
"WARNING": "DarkOrange", |
| 40 |
"ERROR": "Red", |
| 41 |
"CRITICAL": "DarkRed", |
| 42 |
} |
| 43 |
|
| 44 |
html_replacements = { |
| 45 |
"\r\n": "<br> ", |
| 46 |
"\n": "<br> ", |
| 47 |
"\r": "<br> ", |
| 48 |
" ": " " * 2, |
| 49 |
"\t": " " * 4, |
| 50 |
} |
| 51 |
|
| 52 |
def format(self, record): |
| 53 |
formatted = super().format(record) |
| 54 |
|
| 55 |
# Apply color if a level-color is defined |
| 56 |
if record.levelname in self.level_colors: |
| 57 |
formatted = f"<font color=\"{self.level_colors[record.levelname]}\">{formatted}</font>" |
| 58 |
|
| 59 |
# ensure newlines and indents are preserved |
| 60 |
for replacement in self.html_replacements.items(): |
| 61 |
formatted = formatted.replace(*replacement) |
| 62 |
|
| 63 |
# Prepend the level as HTML comment |
| 64 |
formatted = f"<!--{record.levelname}-->{formatted}" |
| 65 |
|
| 66 |
return formatted |
| 67 |
|
| 68 |
|
| 69 |
class LogWidget(TabWidget, QtWidgets.QWidget): |
| 70 |
""" Widget to display logging information in GUI |
| 71 |
|
| 72 |
It is recommended to include this widget in all subclasses of |
| 73 |
:class:`ManagedWindowBase<pymeasure.display.windows.managed_window.ManagedWindowBase>` |
| 74 |
""" |
| 75 |
|
| 76 |
fmt = '%(asctime)s : %(message)s (%(levelname)s)' |
| 77 |
datefmt = '%m/%d/%Y %I:%M:%S %p' |
| 78 |
|
| 79 |
tab_widget = None |
| 80 |
tab_index = None |
| 81 |
|
| 82 |
_blink_qtimer = QtCore.QTimer() |
| 83 |
_blink_color = None |
| 84 |
_blink_state = False |
| 85 |
|
| 86 |
def __init__(self, name, parent=None, fmt=None, datefmt=None): |
| 87 |
if fmt is not None: |
| 88 |
self.fmt = fmt |
| 89 |
if datefmt is not None: |
| 90 |
self.datefmt = datefmt |
| 91 |
|
| 92 |
super().__init__(name, parent) |
| 93 |
self._setup_ui() |
| 94 |
self._layout() |
| 95 |
|
| 96 |
# Setup blinking |
| 97 |
self._blink_qtimer.timeout.connect(self._blink) |
| 98 |
self.handler.connect(self._blinking_start) |
| 99 |
|
| 100 |
def _setup_ui(self): |
| 101 |
self.view = QtWidgets.QPlainTextEdit() |
| 102 |
self.view.setReadOnly(True) |
| 103 |
self.handler = LogHandler() |
| 104 |
self.handler.setFormatter(HTMLFormatter( |
| 105 |
fmt=self.fmt, |
| 106 |
datefmt=self.datefmt, |
| 107 |
)) |
| 108 |
self.handler.connect(self.view.appendHtml) |
| 109 |
|
| 110 |
def _layout(self): |
| 111 |
vbox = QtWidgets.QVBoxLayout(self) |
| 112 |
vbox.setSpacing(0) |
| 113 |
|
| 114 |
vbox.addWidget(self.view) |
| 115 |
self.setLayout(vbox) |
| 116 |
|
| 117 |
def _blink(self): |
| 118 |
self.tab_widget.tabBar().setTabTextColor( |
| 119 |
self.tab_index, |
| 120 |
QtGui.QColor("black" if self._blink_state else self._blink_color) |
| 121 |
) |
| 122 |
|
| 123 |
self._blink_state = not self._blink_state |
| 124 |
|
| 125 |
def _blinking_stop(self, index): |
| 126 |
if index == self.tab_index: |
| 127 |
self._blink_qtimer.stop() |
| 128 |
self._blink_state = True |
| 129 |
self._blink() |
| 130 |
|
| 131 |
self._blink_color = None |
| 132 |
self.tab_widget.setTabIcon(self.tab_index, QtGui.QIcon()) |
| 133 |
|
| 134 |
def _blinking_start(self, message): |
| 135 |
# Delayed setup, since only now the widget is added to the TabWidget |
| 136 |
if self.tab_widget is None: |
| 137 |
self.tab_widget = self.parent().parent() |
| 138 |
self.tab_index = self.tab_widget.indexOf(self) |
| 139 |
self.tab_widget.tabBar().setIconSize(QtCore.QSize(12, 12)) |
| 140 |
self.tab_widget.tabBar().currentChanged.connect(self._blinking_stop) |
| 141 |
|
| 142 |
if message.startswith("<!--ERROR-->") or message.startswith("<!--CRITICAL-->"): |
| 143 |
error = True |
| 144 |
elif message.startswith("<!--WARNING-->"): |
| 145 |
error = False |
| 146 |
else: # no blinking |
| 147 |
return |
| 148 |
|
| 149 |
# Check if the current tab is actually the log-tab |
| 150 |
if self.tab_widget.currentIndex() == self.tab_index: |
| 151 |
self._blinking_stop(self.tab_widget.currentIndex()) |
| 152 |
return |
| 153 |
|
| 154 |
# Define color and icon based on severity |
| 155 |
# If already red, this should not be updated |
| 156 |
if not self._blink_color == "red": |
| 157 |
self._blink_color = "red" if error else "darkorange" |
| 158 |
|
| 159 |
pixmapi = QtWidgets.QStyle.StandardPixmap.SP_MessageBoxCritical if \ |
| 160 |
error else QtWidgets.QStyle.StandardPixmap.SP_MessageBoxWarning |
| 161 |
|
| 162 |
icon = self.style().standardIcon(pixmapi) |
| 163 |
self.tab_widget.setTabIcon(self.tab_index, icon) |
| 164 |
|
| 165 |
# Start timer |
| 166 |
self._blink_qtimer.start(500) |