def __init__(self, dev, attr_name, level=logging.NOTSET, max_buff_size=0): logging.Handler.__init__(self, level) self._attr_name = attr_name self._level = level self._max_buff_size = max_buff_size self._dev = weakref.ref(dev) self._attr = dev.get_device_attr().get_attr_by_name(attr_name) self._buff = LIFO(max_buff_size)
class AttributeLogHandler(logging.Handler): def __init__(self, dev, attr_name, level=logging.NOTSET, max_buff_size=0): logging.Handler.__init__(self, level) self._attr_name = attr_name self._level = level self._max_buff_size = max_buff_size self._dev = weakref.ref(dev) self._attr = dev.get_device_attr().get_attr_by_name(attr_name) self._buff = LIFO(max_buff_size) def emit(self, record): output = self.getRecordMessage(record) self.appendBuffer(output) self.sendText(output) def getRecordMessage(self, record): return self.format(record).split('\n') def sendText(self, output): dev = self._dev() attr = self._attr if attr is None or dev is None: return dev.set_attribute(attr, output) def read(self, attr): """Read from the buffer and assign to the attribute value""" attr.set_value(self._buff.getCopy()) def clearBuffer(self): self._buff.clear() def appendBuffer(self, d): if operator.isSequenceType(d): if isinstance(d, (str, unicode)): self._buff.append(d) else: self._buff.extend(d) else: self._buff.append(str(d)) def sync(self): pass def finish(self): pass