diff python/zcc.py @ 312:2c9768114877

Added cool logging formatter
author Windel Bouwman
date Mon, 16 Dec 2013 17:58:15 +0100
parents ff665880a6b0
children 04cf4d26a3bc
line wrap: on
line diff
--- a/python/zcc.py	Mon Dec 16 12:49:24 2013 +0100
+++ b/python/zcc.py	Mon Dec 16 17:58:15 2013 +0100
@@ -6,6 +6,7 @@
 
 from ppci.c3 import Builder
 import ppci
+from ppci.irutils import Verifier, Writer
 from ppci.codegen import CodeGenerator
 import outstream
 from utils import HexFile
@@ -18,12 +19,64 @@
 
 
 def logLevel(s):
+    """ Converts a string to a valid logging level """
     numeric_level = getattr(logging, s.upper(), None)
     if not isinstance(numeric_level, int):
         raise ValueError('Invalid log level: {}'.format(s))
     return numeric_level
 
 
+class RstFormatter(logging.Formatter):
+    """ Formatter that tries to create an rst document """
+    def __init__(self):
+        super().__init__(fmt=logformat)
+
+    def format(self, record):
+        s = super().format(record)
+        if hasattr(record, 'ircode'):
+            f = io.StringIO()
+            print('', file=f)
+            print('', file=f)
+            print('.. code::', file=f)
+            print('', file=f)
+            Writer('  ').write(record.ircode, f)
+            print('', file=f)
+            s += '\n' + f.getvalue()
+        if hasattr(record, 'irfunc'):
+            f = io.StringIO()
+            print('', file=f)
+            print('', file=f)
+            print('.. code::', file=f)
+            print('', file=f)
+            Writer('  ').write_function(record.irfunc, f)
+            print('', file=f)
+            s += '\n' + f.getvalue()
+        if hasattr(record, 'ppci_frame'):
+            f = io.StringIO()
+            frame = record.ppci_frame
+            print('', file=f)
+            print('.. code::', file=f)
+            print('', file=f)
+            print('  {}'.format(frame.name), file=f)
+            for i in frame.instructions:
+                print('   {}'.format(i),file=f)
+            print('', file=f)
+            s += '\n' + f.getvalue()
+        if hasattr(record, 'ra_cfg'):
+            f = io.StringIO()
+            print('', file=f)
+            print('', file=f)
+            print('.. graphviz::', file=f)
+            print('', file=f)
+            print('  digraph G {', file=f)
+            cfg = record.ra_cfg
+            cfg.to_dot(f)
+            print('  }', file=f)
+            print('', file=f)
+            s += '\n' + f.getvalue()
+        return s
+
+
 target_list = [target.armtarget]
 targets = {t.name: t for t in target_list}
 targetnames = list(targets.keys())
@@ -67,6 +120,7 @@
         if not ircode:
             return
         # Optimization passes, TODO
+        Verifier().verify(ircode)
 
         if dumpir:
             f = io.StringIO()
@@ -74,7 +128,8 @@
             print(f.getvalue())
 
         # Code generation:
-        logging.info('Starting code generation for {}'.format(ircode))
+        d = {'ircode':ircode}
+        logging.info('Starting code generation for {}'.format(ircode), extra=d)
         cg.generate(ircode, outs)
     # TODO: fixup references, do this in another way?
     outs.backpatch()
@@ -84,6 +139,11 @@
 
 def main(args):
     logging.basicConfig(format=logformat, level=args.log)
+    #logging.getLogger().addHandler(RstLogHandler())
+    fh = logging.FileHandler('log.rst', mode='w')
+    fh.setFormatter(RstFormatter())
+    logging.getLogger().addHandler(fh)
+
     tg = targets[args.target]
     diag = ppci.DiagnosticsManager()
     outs = outstream.TextOutputStream()