changeset 281:4496cae24d7f

Improved logview
author Windel Bouwman
date Sat, 02 Nov 2013 11:11:40 +0100
parents 02385f62f250
children c137f1fe3e65
files python/ide.py python/logview.py python/ppci/errors.py python/zcc.py
diffstat 4 files changed, 87 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/python/ide.py	Sat Nov 02 10:03:26 2013 +0100
+++ b/python/ide.py	Sat Nov 02 11:11:40 2013 +0100
@@ -11,28 +11,13 @@
 import ppci
 from astviewer import AstViewer
 from codeedit import CodeEdit
+from logview import LogView as BuildOutput
 stutil = __import__('st-util')
 import c3
 import zcc
 import outstream
 
 
-class BuildOutput(QTextEdit):
-    """ Build output component """
-    def __init__(self, parent=None):
-        super(BuildOutput, self).__init__(parent)
-        fmt = logging.Formatter(fmt=zcc.logformat)
-
-        class MyHandler(logging.Handler):
-            def emit(self2, x):
-                self.append(str(fmt.format(x)))
-
-        logging.getLogger().addHandler(MyHandler())
-        self.setCurrentFont(QFont('Courier'))
-        self.setReadOnly(True)
-        logging.info('Build output will appear here!')
-
-
 class BuildErrors(QTreeView):
     sigErrorSelected = pyqtSignal(object)
 
@@ -44,6 +29,7 @@
         self.errorIcon = QIcon('icons/error.png')
         self.model = QStandardItemModel()
         self.model.setHorizontalHeaderLabels(['Message', 'Row', 'Column'])
+        self.header().setStretchLastSection(True)
         self.setModel(self.model)
 
     def setErrorList(self, errorlist):
@@ -59,6 +45,8 @@
             icol = QStandardItem(col)
             icol.setData(e)
             self.model.appendRow([item, irow, icol])
+        for i in range(3):
+            self.resizeColumnToContents(i)
 
     def itemSelected(self, index):
         if not index.isValid():
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/logview.py	Sat Nov 02 11:11:40 2013 +0100
@@ -0,0 +1,80 @@
+#!/usr/bin/python
+
+import sys
+import os
+import logging
+import datetime
+
+from PyQt4.QtGui import QApplication, QWidget, QTableView, QVBoxLayout
+from PyQt4.QtGui import QHeaderView
+from PyQt4.QtCore import Qt
+from PyQt4.QtCore import QAbstractTableModel
+
+def formatTime(t):
+    t2 = datetime.datetime.fromtimestamp(t)
+    return t2.strftime('%H:%M:%S')
+
+class LogModel(QAbstractTableModel):
+    def __init__(self):
+        super().__init__()
+        self.entries = []
+        self.headers = ['Time', 'Level', 'Logger', 'Message']
+        self.txts = []
+        self.txts.append(lambda e: str(formatTime(e.created)))
+        self.txts.append(lambda e: str(e.levelname))
+        self.txts.append(lambda e: str(e.name))
+        self.txts.append(lambda e: str(e.msg))
+
+    def rowCount(self, parent):
+        return len(self.entries)
+        
+    def columnCount(self, parent):
+        return len(self.headers)
+
+    def data(self, index, role):
+        if not index.isValid():
+            return
+        row, col = index.row(), index.column()
+        if role == Qt.DisplayRole:
+            le = self.entries[row]
+            return self.txts[col](le)
+
+    def headerData(self, section, orientation, role):
+        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
+            return self.headers[section]
+            
+    def newLog(self, x):
+        self.entries.append(x)
+        self.modelReset.emit()
+
+
+class LogView(QWidget):
+    """ Log view component """
+    def __init__(self, parent=None):
+        super().__init__(parent)
+        l = QVBoxLayout(self)
+        self.tv = QTableView(self)
+        self.tv.horizontalHeader().setStretchLastSection(True)
+        l.addWidget(self.tv)
+        self.lm = LogModel()
+        self.tv.setModel(self.lm)
+    
+        class MyHandler(logging.Handler):
+            def emit(self2, x):
+                self.lm.newLog(x)
+                self.tv.scrollToBottom()
+                for i in range(3):
+                    self.tv.resizeColumnToContents(i)
+
+        logging.getLogger().addHandler(MyHandler())
+
+
+if __name__ == '__main__':
+    app = QApplication(sys.argv)
+    lv = LogView()
+    lv.show()
+    lv.resize(600, 200)
+    logging.error('Error!!!')
+    logging.warn('Warn here!')
+    app.exec_()
+
--- a/python/ppci/errors.py	Sat Nov 02 10:03:26 2013 +0100
+++ b/python/ppci/errors.py	Sat Nov 02 11:11:40 2013 +0100
@@ -54,7 +54,7 @@
         self.logger = logging.getLogger('diagnostics')
 
     def addDiag(self, d):
-        self.logger.info(str(d))
+        self.logger.info(str(d.msg))
         self.diags.append(d)
 
     def error(self, msg, loc):
--- a/python/zcc.py	Sat Nov 02 10:03:26 2013 +0100
+++ b/python/zcc.py	Sat Nov 02 11:11:40 2013 +0100
@@ -8,6 +8,8 @@
 import hexfile
 import logging
 
+logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
+
 def logLevel(s):
     numeric_level = getattr(logging, s.upper(), None)
     if not isinstance(numeric_level, int):
@@ -46,8 +48,6 @@
     obj = cg.generate(ircode)
     return True
 
-logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s'
-
 def main(args):
     logging.basicConfig(format=logformat, level=args.log)
     src = args.source.read()