From 155b7f871253935f1cfb2d34ee5cc45d8a35d948 Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Wed, 5 Jun 2019 21:31:33 -0400 Subject: [PATCH] avocado.utils.datadrainer: introduce line logger This drainer will wait for a complete new line, buffering incomplete lines as needed. When complete lines are found, it will log them via a standard Python logging API. Signed-off-by: Cleber Rosa --- avocado/utils/datadrainer.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/avocado/utils/datadrainer.py b/avocado/utils/datadrainer.py index ea9b00dd..54134b67 100644 --- a/avocado/utils/datadrainer.py +++ b/avocado/utils/datadrainer.py @@ -165,3 +165,28 @@ class BufferFDDrainer(FDDrainer): Returns the buffer data, as bytes """ return self._data.getvalue() + + +class LineLogger(FDDrainer): + + name = 'avocado.utils.datadrainer.LineLogger' + + def __init__(self, source, stop_check=None, name=None, logger=None): + super(LineLogger, self).__init__(source, stop_check, name) + self._logger = logger + self._buffer = io.BytesIO() + + def write(self, data): + if b'\n' not in data: + self._buffer.write(data) + return + data = self._buffer.getvalue() + data + lines = data.split(b'\n') + if not lines[-1].endswith(b'\n'): + self._buffer.close() + self._buffer = io.BytesIO() + self._buffer.write(lines[-1]) + for line in lines: + line = line.decode(errors='replace').rstrip('\n') + if line: + self._logger.debug(line) -- GitLab