comparison python/ppci/outstream.py @ 348:442fb043d149

Added log option to zcc
author Windel Bouwman
date Sat, 08 Mar 2014 15:32:33 +0100
parents 3bb7dcfe5529
children 39bf68bf1891
comparison
equal deleted inserted replaced
346:3bb7dcfe5529 348:442fb043d149
1 import logging
1 import binascii 2 import binascii
2 from ppci.target import Instruction, Alignment 3 from ppci.target import Instruction, Alignment
3 from ppci.objectfile import ObjectFile 4 from ppci.objectfile import ObjectFile
4 5
5 """ 6 """
10 11
11 class OutputStream: 12 class OutputStream:
12 def emit(self, item): 13 def emit(self, item):
13 raise NotImplementedError('Abstract base class') 14 raise NotImplementedError('Abstract base class')
14 15
15 def selectSection(self, sname): 16 def select_section(self, sname):
16 raise NotImplementedError('Abstract base class') 17 raise NotImplementedError('Abstract base class')
17 18
18 19
19 class OutputStreamWriter: 20 class OutputStreamWriter:
20 def __init__(self, extra_indent=''): 21 def __init__(self, extra_indent=''):
61 # Special case for align, TODO do this different? 62 # Special case for align, TODO do this different?
62 if type(item) is Alignment: 63 if type(item) is Alignment:
63 while section.Size % item.align != 0: 64 while section.Size % item.align != 0:
64 section.add_data(bytes([0])) 65 section.add_data(bytes([0]))
65 66
66 def selectSection(self, sname): 67 def select_section(self, sname):
67 self.currentSection = self.obj_file.get_section(sname) 68 self.currentSection = self.obj_file.get_section(sname)
68 69
69 70
70 class DummyOutputStream(OutputStream): 71 class DummyOutputStream(OutputStream):
72 """ Stream that implements the bare minimum and does nothing """
71 def emit(self, item): 73 def emit(self, item):
72 pass 74 pass
73 75
74 def selectSection(self, sname): 76 def select_section(self, sname):
75 pass 77 pass
78
79
80 class LoggerOutputStream(OutputStream):
81 """ Stream that emits instructions as text in the log """
82 def __init__(self):
83 self.logger = logging.getLogger('LoggerOutputStream')
84
85 def emit(self, item):
86 self.logger.debug(str(item))
87
88 def select_section(self, sname):
89 self.logger.debug('.section {}'.format(sname))
90
91
92 class MasterOutputStream(OutputStream):
93 """ Stream that emits to multiple sub streams """
94 def __init__(self):
95 self.substreams = []
96
97 def add_substream(self, output_stream):
98 self.substreams.append(output_stream)
99
100 def emit(self, item):
101 for output_stream in self.substreams:
102 output_stream.emit(item)
103
104 def select_section(self, sname):
105 for output_stream in self.substreams:
106 output_stream.select_section(sname)