Files
h3n3-jmwright-CQ-Editor/cq_editor/widgets/log.py
2023-10-10 13:59:37 -05:00

46 lines
1.3 KiB
Python

import logbook as logging
from PySide6.QtWidgets import QPlainTextEdit
from PySide6 import QtCore
from ..mixins import ComponentMixin
class QtLogHandler(logging.Handler,logging.StringFormatterHandlerMixin):
def __init__(self, log_widget,*args,**kwargs):
super(QtLogHandler,self).__init__(*args,**kwargs)
log_format_string = '[{record.time:%H:%M:%S.%f%z}] {record.level_name}: {record.message}'
logging.StringFormatterHandlerMixin.__init__(self,log_format_string)
self.log_widget = log_widget
def emit(self, record):
msg = self.format(record)
QtCore.QMetaObject\
.invokeMethod(self.log_widget,
'appendPlainText',
QtCore.Qt.QueuedConnection,
QtCore.Q_ARG(str, msg))
class LogViewer(QPlainTextEdit, ComponentMixin):
name = 'Log viewer'
def __init__(self,*args,**kwargs):
super(LogViewer,self).__init__(*args,**kwargs)
self._MAX_ROWS = 500
self.setReadOnly(True)
self.setMaximumBlockCount(self._MAX_ROWS)
self.setLineWrapMode(QPlainTextEdit.NoWrap)
self.handler = QtLogHandler(self)
def append(self,msg):
self.appendPlainText(msg)