Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
311:ff665880a6b0 | 312:2c9768114877 |
---|---|
4 import argparse | 4 import argparse |
5 import logging | 5 import logging |
6 | 6 |
7 from ppci.c3 import Builder | 7 from ppci.c3 import Builder |
8 import ppci | 8 import ppci |
9 from ppci.irutils import Verifier, Writer | |
9 from ppci.codegen import CodeGenerator | 10 from ppci.codegen import CodeGenerator |
10 import outstream | 11 import outstream |
11 from utils import HexFile | 12 from utils import HexFile |
12 import target | 13 import target |
13 from ppci import irutils | 14 from ppci import irutils |
16 | 17 |
17 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s' | 18 logformat='%(asctime)s|%(levelname)s|%(name)s|%(message)s' |
18 | 19 |
19 | 20 |
20 def logLevel(s): | 21 def logLevel(s): |
22 """ Converts a string to a valid logging level """ | |
21 numeric_level = getattr(logging, s.upper(), None) | 23 numeric_level = getattr(logging, s.upper(), None) |
22 if not isinstance(numeric_level, int): | 24 if not isinstance(numeric_level, int): |
23 raise ValueError('Invalid log level: {}'.format(s)) | 25 raise ValueError('Invalid log level: {}'.format(s)) |
24 return numeric_level | 26 return numeric_level |
27 | |
28 | |
29 class RstFormatter(logging.Formatter): | |
30 """ Formatter that tries to create an rst document """ | |
31 def __init__(self): | |
32 super().__init__(fmt=logformat) | |
33 | |
34 def format(self, record): | |
35 s = super().format(record) | |
36 if hasattr(record, 'ircode'): | |
37 f = io.StringIO() | |
38 print('', file=f) | |
39 print('', file=f) | |
40 print('.. code::', file=f) | |
41 print('', file=f) | |
42 Writer(' ').write(record.ircode, f) | |
43 print('', file=f) | |
44 s += '\n' + f.getvalue() | |
45 if hasattr(record, 'irfunc'): | |
46 f = io.StringIO() | |
47 print('', file=f) | |
48 print('', file=f) | |
49 print('.. code::', file=f) | |
50 print('', file=f) | |
51 Writer(' ').write_function(record.irfunc, f) | |
52 print('', file=f) | |
53 s += '\n' + f.getvalue() | |
54 if hasattr(record, 'ppci_frame'): | |
55 f = io.StringIO() | |
56 frame = record.ppci_frame | |
57 print('', file=f) | |
58 print('.. code::', file=f) | |
59 print('', file=f) | |
60 print(' {}'.format(frame.name), file=f) | |
61 for i in frame.instructions: | |
62 print(' {}'.format(i),file=f) | |
63 print('', file=f) | |
64 s += '\n' + f.getvalue() | |
65 if hasattr(record, 'ra_cfg'): | |
66 f = io.StringIO() | |
67 print('', file=f) | |
68 print('', file=f) | |
69 print('.. graphviz::', file=f) | |
70 print('', file=f) | |
71 print(' digraph G {', file=f) | |
72 cfg = record.ra_cfg | |
73 cfg.to_dot(f) | |
74 print(' }', file=f) | |
75 print('', file=f) | |
76 s += '\n' + f.getvalue() | |
77 return s | |
25 | 78 |
26 | 79 |
27 target_list = [target.armtarget] | 80 target_list = [target.armtarget] |
28 targets = {t.name: t for t in target_list} | 81 targets = {t.name: t for t in target_list} |
29 targetnames = list(targets.keys()) | 82 targetnames = list(targets.keys()) |
65 tg.startCode(outs) | 118 tg.startCode(outs) |
66 for ircode in c3b.build(srcs, imps): | 119 for ircode in c3b.build(srcs, imps): |
67 if not ircode: | 120 if not ircode: |
68 return | 121 return |
69 # Optimization passes, TODO | 122 # Optimization passes, TODO |
123 Verifier().verify(ircode) | |
70 | 124 |
71 if dumpir: | 125 if dumpir: |
72 f = io.StringIO() | 126 f = io.StringIO() |
73 irutils.Writer().write(ircode, f) | 127 irutils.Writer().write(ircode, f) |
74 print(f.getvalue()) | 128 print(f.getvalue()) |
75 | 129 |
76 # Code generation: | 130 # Code generation: |
77 logging.info('Starting code generation for {}'.format(ircode)) | 131 d = {'ircode':ircode} |
132 logging.info('Starting code generation for {}'.format(ircode), extra=d) | |
78 cg.generate(ircode, outs) | 133 cg.generate(ircode, outs) |
79 # TODO: fixup references, do this in another way? | 134 # TODO: fixup references, do this in another way? |
80 outs.backpatch() | 135 outs.backpatch() |
81 outs.backpatch() # Why two times? | 136 outs.backpatch() # Why two times? |
82 return c3b.ok | 137 return c3b.ok |
83 | 138 |
84 | 139 |
85 def main(args): | 140 def main(args): |
86 logging.basicConfig(format=logformat, level=args.log) | 141 logging.basicConfig(format=logformat, level=args.log) |
142 #logging.getLogger().addHandler(RstLogHandler()) | |
143 fh = logging.FileHandler('log.rst', mode='w') | |
144 fh.setFormatter(RstFormatter()) | |
145 logging.getLogger().addHandler(fh) | |
146 | |
87 tg = targets[args.target] | 147 tg = targets[args.target] |
88 diag = ppci.DiagnosticsManager() | 148 diag = ppci.DiagnosticsManager() |
89 outs = outstream.TextOutputStream() | 149 outs = outstream.TextOutputStream() |
90 | 150 |
91 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir) | 151 res = zcc(args.source, args.imp, tg, outs, diag, dumpir=args.dumpir) |