pipe_handler.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. An handler for the stdout and the stderr output
  3. @author: olivier.massot, 05-2020
  4. """
  5. import logging
  6. import os
  7. import threading
  8. class PipeHandler(threading.Thread):
  9. """ Handle the stderr output from a Popen object """
  10. def __init__(self, logger_name, default_level=logging.INFO):
  11. """ Setup the object with a logger and a loglevel
  12. and start the thread
  13. """
  14. threading.Thread.__init__(self)
  15. self.daemon = False
  16. self.fdRead, self.fdWrite = os.pipe()
  17. self.pipeReader = os.fdopen(self.fdRead)
  18. self.start()
  19. self.logger = logging.getLogger(logger_name)
  20. self.default_level = default_level
  21. def fileno(self):
  22. """ Return the write file descriptor of the pipe
  23. """
  24. return self.fdWrite
  25. def process(self, line):
  26. """ Process the last line that was read
  27. """
  28. self.logger.log(self.default_level, line)
  29. def run(self):
  30. """ Run the thread, logging everything.
  31. """
  32. for line in iter(self.pipeReader.readline, ''):
  33. self.process(line.strip('\n'))
  34. self.pipeReader.close()
  35. def close(self):
  36. """ Close the write end of the pipe.
  37. """
  38. os.close(self.fdWrite)