Skip to content

Instantly share code, notes, and snippets.

@siennathesane
Created December 2, 2015 03:33
Show Gist options
  • Select an option

  • Save siennathesane/54671512a22d4b8f6f72 to your computer and use it in GitHub Desktop.

Select an option

Save siennathesane/54671512a22d4b8f6f72 to your computer and use it in GitHub Desktop.
Send subprocess output to a logger.
class LogPipe(threading.Thread):
"""
Piping subprocess output to a logger.
"""
def __init__(self, level):
"""
Setup the object with a logger and a loglevel and start the thread
"""
threading.Thread.__init__(self)
self.daemon = False
self.level = level
self.fdRead, self.fdWrite = pipe()
self.pipeReader = fdopen(self.fdRead)
self.start()
def fileno(self):
"""
Return the write file descriptor of the pipe
"""
return self.fdWrite
def run(self):
"""
Run the thread, logging everything.
"""
for line in iter(self.pipeReader.readline, ''):
logging.log(self.level, line.strip('\n'))
self.pipeReader.close()
def close(self):
"""
Close the write end of the pipe.
"""
close(self.fdWrite)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment