diff python/zcc.py @ 315:084cccaa5deb

Added console and screen
author Windel Bouwman
date Sat, 21 Dec 2013 10:03:01 +0100
parents 38f5f298ce0e
children 56e6ff84f646
line wrap: on
line diff
--- a/python/zcc.py	Wed Dec 18 20:22:20 2013 +0100
+++ b/python/zcc.py	Sat Dec 21 10:03:01 2013 +0100
@@ -41,7 +41,6 @@
             print('.. code::', file=f)
             print('', file=f)
             AstPrinter().printAst(record.c3_ast, f)
-            #Writer('  ').write(record.c3_ast, f)
             print('', file=f)
             s += '\n' + f.getvalue()
         if hasattr(record, 'ircode'):
@@ -112,22 +111,25 @@
 parser.add_argument('-i', '--imp', type=argparse.FileType('r'), \
   help='Possible import module', action='append', default=[])
 
-parser.add_argument('--dumpir', action='store_true', help="Dump IR-code")
 parser.add_argument('--optimize', action='store_true', help="Optimize")
 parser.add_argument('--target', help="Backend selection",
     choices=targetnames, required=True)
 parser.add_argument('-o', '--output', help='Output file', metavar='filename')
 parser.add_argument('--hexfile', help='Output hexfile',
     type=argparse.FileType('w'))
-parser.add_argument('--log', help='Log level (INFO,DEBUG)', type=logLevel)
+parser.add_argument('--log', help='Log level (INFO,DEBUG,[WARN])', 
+                    type=logLevel, default='WARN')
+parser.add_argument('--report', help='Specify a file to write the compile report to', 
+            type=argparse.FileType('w'))
 
 
-def zcc(srcs, imps, tg, outs, diag, dumpir=False):
+def zcc(srcs, imps, tg, outs, diag):
     """
+        Compiler driver
         Compile sources into output stream.
         Sources is an iterable of open files.
     """
-    logging.info('Zcc started')
+    logging.info('Zcc started {}'.format(srcs))
     # Front end:
     c3b = Builder(diag, tg)
     cg = CodeGenerator(tg)
@@ -158,17 +160,22 @@
 
 
 def main(args):
-    #logging.getLogger().setLevel(logging.DEBUG)
-    #logging.getLogger().addHandler(RstLogHandler())
-    #fh = logging.FileHandler('log.rst', mode='w')
-    #fh.setFormatter(RstFormatter())
-    #logging.getLogger().addHandler(fh)
+    # Configure some logging:
+    logging.getLogger().setLevel(logging.DEBUG)
+    ch = logging.StreamHandler()
+    ch.setFormatter(logging.Formatter(logformat))
+    ch.setLevel(args.log)
+    logging.getLogger().addHandler(ch)
+    if args.report:
+        fh = logging.StreamHandler(stream=args.report)
+        fh.setFormatter(RstFormatter())
+        logging.getLogger().addHandler(fh)
 
     tg = targets[args.target]
     diag = ppci.DiagnosticsManager()
     outs = outstream.TextOutputStream()
 
-    res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir)
+    res = zcc(args.source, args.imp, tg, outs, diag)
     if not res:
         diag.printErrors()
         return 1
@@ -186,6 +193,10 @@
         hf = HexFile()
         hf.addRegion(0x08000000, code_bytes)
         hf.save(args.hexfile)
+
+    if args.report:
+        logging.getLogger().removeHandler(fh)
+    logging.getLogger().removeHandler(ch)
     return 0