artiq/artiq/gui/log.py

103 lines
3.4 KiB
Python
Raw Normal View History

2015-07-22 05:13:50 +08:00
import asyncio
import logging
import time
2015-07-22 05:13:50 +08:00
2015-07-23 23:06:37 +08:00
from quamash import QtGui, QtCore
2015-05-24 00:38:30 +08:00
from pyqtgraph import dockarea
2015-07-22 05:13:50 +08:00
from artiq.protocols.sync_struct import Subscriber
from artiq.gui.tools import ListSyncModel
def _level_to_name(level):
if level >= logging.CRITICAL:
return "CRITICAL"
if level >= logging.ERROR:
return "ERROR"
if level >= logging.WARNING:
return "WARNING"
if level >= logging.INFO:
return "INFO"
return "DEBUG"
2015-07-22 05:13:50 +08:00
class _LogModel(ListSyncModel):
def __init__(self, parent, init):
ListSyncModel.__init__(self,
["Level", "Source", "Time", "Message"],
2015-07-22 05:13:50 +08:00
parent, init)
2015-07-25 11:38:26 +08:00
self.fixed_font = QtGui.QFont()
self.fixed_font.setFamily("Monospace")
self.debug_fg = QtGui.QBrush(QtGui.QColor(55, 55, 55))
self.warning_bg = QtGui.QBrush(QtGui.QColor(255, 255, 180))
self.error_bg = QtGui.QBrush(QtGui.QColor(255, 150, 150))
2015-07-25 11:38:26 +08:00
def data(self, index, role):
if (role == QtCore.Qt.FontRole and index.isValid()
and index.column() == 3):
2015-07-25 11:38:26 +08:00
return self.fixed_font
elif role == QtCore.Qt.BackgroundRole and index.isValid():
level = self.backing_store[index.row()][0]
if level >= logging.ERROR:
return self.error_bg
elif level >= logging.WARNING:
return self.warning_bg
else:
return ListSyncModel.data(self, index, role)
elif role == QtCore.Qt.ForegroundRole and index.isValid():
level = self.backing_store[index.row()][0]
if level <= logging.DEBUG:
return self.debug_fg
else:
return ListSyncModel.data(self, index, role)
2015-07-25 11:38:26 +08:00
else:
return ListSyncModel.data(self, index, role)
2015-07-22 05:13:50 +08:00
def convert(self, v, column):
if column == 0:
return _level_to_name(v[0])
elif column == 1:
return v[1]
elif column == 2:
return time.strftime("%m/%d %H:%M:%S", time.localtime(v[2]))
else:
return v[3]
2015-07-22 05:13:50 +08:00
2015-05-24 00:38:30 +08:00
class LogDock(dockarea.Dock):
def __init__(self):
dockarea.Dock.__init__(self, "Log", size=(1000, 300))
2015-07-22 05:13:50 +08:00
self.log = QtGui.QTableView()
self.log.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
2015-07-23 22:36:52 +08:00
self.log.horizontalHeader().setResizeMode(
QtGui.QHeaderView.ResizeToContents)
2015-07-23 23:06:37 +08:00
self.log.setHorizontalScrollMode(
QtGui.QAbstractItemView.ScrollPerPixel)
self.log.setShowGrid(False)
self.log.setTextElideMode(QtCore.Qt.ElideNone)
2015-07-22 05:13:50 +08:00
self.addWidget(self.log)
2015-07-24 00:11:05 +08:00
self.scroll_at_bottom = False
2015-07-22 05:13:50 +08:00
2015-10-03 19:28:57 +08:00
async def sub_connect(self, host, port):
2015-07-22 05:13:50 +08:00
self.subscriber = Subscriber("log", self.init_log_model)
2015-10-03 19:28:57 +08:00
await self.subscriber.connect(host, port)
2015-07-22 05:13:50 +08:00
2015-10-03 19:28:57 +08:00
async def sub_close(self):
await self.subscriber.close()
2015-07-22 05:13:50 +08:00
2015-07-24 00:11:05 +08:00
def rows_inserted_before(self):
scrollbar = self.log.verticalScrollBar()
self.scroll_at_bottom = scrollbar.value() == scrollbar.maximum()
def rows_inserted_after(self):
if self.scroll_at_bottom:
self.log.scrollToBottom()
2015-07-22 05:13:50 +08:00
def init_log_model(self, init):
table_model = _LogModel(self.log, init)
self.log.setModel(table_model)
2015-07-24 00:11:05 +08:00
table_model.rowsAboutToBeInserted.connect(self.rows_inserted_before)
table_model.rowsInserted.connect(self.rows_inserted_after)
2015-07-22 05:13:50 +08:00
return table_model