annotate python/logview.py @ 281:4496cae24d7f

Improved logview
author Windel Bouwman
date Sat, 02 Nov 2013 11:11:40 +0100
parents
children
rev   line source
281
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
2
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
3 import sys
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
4 import os
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
5 import logging
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
6 import datetime
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
7
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
8 from PyQt4.QtGui import QApplication, QWidget, QTableView, QVBoxLayout
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
9 from PyQt4.QtGui import QHeaderView
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
10 from PyQt4.QtCore import Qt
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
11 from PyQt4.QtCore import QAbstractTableModel
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
12
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
13 def formatTime(t):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
14 t2 = datetime.datetime.fromtimestamp(t)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
15 return t2.strftime('%H:%M:%S')
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
16
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
17 class LogModel(QAbstractTableModel):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
18 def __init__(self):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
19 super().__init__()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
20 self.entries = []
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
21 self.headers = ['Time', 'Level', 'Logger', 'Message']
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
22 self.txts = []
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
23 self.txts.append(lambda e: str(formatTime(e.created)))
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
24 self.txts.append(lambda e: str(e.levelname))
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
25 self.txts.append(lambda e: str(e.name))
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
26 self.txts.append(lambda e: str(e.msg))
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
27
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
28 def rowCount(self, parent):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
29 return len(self.entries)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
30
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
31 def columnCount(self, parent):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
32 return len(self.headers)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
33
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
34 def data(self, index, role):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
35 if not index.isValid():
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
36 return
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
37 row, col = index.row(), index.column()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
38 if role == Qt.DisplayRole:
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
39 le = self.entries[row]
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
40 return self.txts[col](le)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
41
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
42 def headerData(self, section, orientation, role):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
43 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
44 return self.headers[section]
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
45
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
46 def newLog(self, x):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
47 self.entries.append(x)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
48 self.modelReset.emit()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
49
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
50
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
51 class LogView(QWidget):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
52 """ Log view component """
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
53 def __init__(self, parent=None):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
54 super().__init__(parent)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
55 l = QVBoxLayout(self)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
56 self.tv = QTableView(self)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
57 self.tv.horizontalHeader().setStretchLastSection(True)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
58 l.addWidget(self.tv)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
59 self.lm = LogModel()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
60 self.tv.setModel(self.lm)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
61
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
62 class MyHandler(logging.Handler):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
63 def emit(self2, x):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
64 self.lm.newLog(x)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
65 self.tv.scrollToBottom()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
66 for i in range(3):
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
67 self.tv.resizeColumnToContents(i)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
68
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
69 logging.getLogger().addHandler(MyHandler())
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
70
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
71
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
72 if __name__ == '__main__':
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
73 app = QApplication(sys.argv)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
74 lv = LogView()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
75 lv.show()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
76 lv.resize(600, 200)
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
77 logging.error('Error!!!')
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
78 logging.warn('Warn here!')
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
79 app.exec_()
4496cae24d7f Improved logview
Windel Bouwman
parents:
diff changeset
80