| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- """
- An handler for the stdout and the stderr output
- @author: olivier.massot, 05-2020
- """
- import logging
- import os
- import threading
- class PipeHandler(threading.Thread):
- """ Handle the stderr output from a Popen object """
- def __init__(self, logger_name, default_level=logging.INFO):
- """ Setup the object with a logger and a loglevel
- and start the thread
- """
- threading.Thread.__init__(self)
- self.daemon = False
- self.fdRead, self.fdWrite = os.pipe()
- self.pipeReader = os.fdopen(self.fdRead)
- self.start()
- self.logger = logging.getLogger(logger_name)
- self.default_level = default_level
- def fileno(self):
- """ Return the write file descriptor of the pipe
- """
- return self.fdWrite
- def process(self, line):
- """ Process the last line that was read
- """
- self.logger.log(self.default_level, line)
- def run(self):
- """ Run the thread, logging everything.
- """
- for line in iter(self.pipeReader.readline, ''):
- self.process(line.strip('\n'))
- self.pipeReader.close()
- def close(self):
- """ Close the write end of the pipe.
- """
- os.close(self.fdWrite)
|