Created
December 2, 2015 03:33
-
-
Save siennathesane/54671512a22d4b8f6f72 to your computer and use it in GitHub Desktop.
Send subprocess output to a logger.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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