def __exit__(self, exc_type, exc_val, exc_tb): self._defer_publish = False if exc_type: try: self._exception(exc_type, exc_val, exc_tb, message=None, fargs=(), data={}) except Exception as e: note('action_exit', 'got %r while already handling exception %r', e, exc_val) pass # TODO: still have to create end_event else: if self.end_event: self.logger.on_end(self.end_event) else: # now that _defer_publish=False, this will also publish self.success() self.logger.context.set_active_parent(self.logger, self.parent_action) if self._reraise is False: return True # ignore exception return
def flush(self): stream_flush = getattr(self.stream, 'flush', None) if not callable(stream_flush): return try: stream_flush() except Exception as e: note('stream_flush', 'got %r on %r.flush()', e, self)
def close(self): if self.stream is None: return try: self.flush() self.stream.close() except Exception as e: note('file_close', 'got %r on %r.close()', e, self)
def emit_entry(self, action, entry): try: entry = entry.encode(self.encoding, self.errors) except UnicodeDecodeError as ude: # Note that this is a *decode* error, meaning a bytestring # found its way through and implicit decoding is happening. note('emit_encode', 'got %r on %s.emit_entry();' ' expected decoded text, not %r', ude, self, entry) raise try: self.stream.write(entry) if self.sep: self.stream.write(self.sep) self.flush() except Exception as e: note('stream_emit', 'got %r on %r.emit_entry()', e, self)
def emit_entry(self, event, entry): try: entry = entry.encode(self.encoding, self.errors) except UnicodeDecodeError as ude: # Note that this is a *decode* error, meaning a bytestring # found its way through and implicit decoding is happening. note('emit_encode', 'got %r on %s.emit_entry();' ' expected decoded text, not %r', ude, self, entry) raise try: self.stream.write(entry + self.sep if self.sep else entry) self.flush() except Exception as e: note('stream_emit', 'got %r on %r.emit_entry()', e, self) if (type(e) is IOError and self._reopen_stale and getattr(e, 'errno', None) and e.errno == errno.ESTALE): name = getattr(self.stream, 'name', None) if name and name not in ('<stdout>', '<stderr>'): note('stream_emit', 'reopening stale stream to %r', name) # append in binary mode if the original mode was binary mode = 'ab' if 'b' in getattr(self.stream, 'mode', 'ab') else 'a' self.stream = open(name, mode) # retry writing once self.stream.write(entry + self.sep if self.sep else entry) self.flush() return