programming:python:fifologger
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | programming:python:fifologger [2008/12/18 20:23] (current) – created crustymonkey | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Fifologger package ====== | ||
+ | ===== What is it? ===== | ||
+ | The '' | ||
+ | ===== The Script ===== | ||
+ | <code python> | ||
+ | # | ||
+ | |||
+ | # $Id: fifologger.py 86 2008-12-18 20:10:39Z jay $ | ||
+ | |||
+ | import os , time , stat | ||
+ | |||
+ | __all__ = [' | ||
+ | |||
+ | """ | ||
+ | You can use the FifoLog class to log messages to a fifo. | ||
+ | |||
+ | Usage is simple: | ||
+ | | ||
+ | import fifologger | ||
+ | fifo = '/ | ||
+ | l = fifologger.FifoLog(fifo) | ||
+ | for i in xrange(1000): | ||
+ | l.log(' | ||
+ | time.sleep(1) | ||
+ | l.close() | ||
+ | |||
+ | Then, just simply run: | ||
+ | |||
+ | $ cat / | ||
+ | |||
+ | And you should see the "hello world" messages printed to your term. | ||
+ | """ | ||
+ | |||
+ | |||
+ | class FifoError(Exception): | ||
+ | pass | ||
+ | |||
+ | class FifoLog(): | ||
+ | """ | ||
+ | This class is set up to make it easy to log to a FIFO under a *nix | ||
+ | based operating system. | ||
+ | to the read end of the pipe whenever you want to hop on a | ||
+ | log stream, but you don't have the overhead of rotating log | ||
+ | files and reattaching to new logs, etc. | ||
+ | |||
+ | Note that if there is not a process connected to the read end | ||
+ | of the pipe, all messages sent to this logger will be | ||
+ | dropped until a reader connects. | ||
+ | """ | ||
+ | def __init__(self , fifoPath , showTime=True): | ||
+ | self._fifoPath = fifoPath | ||
+ | self._fd = -1 | ||
+ | self._showTime = showTime | ||
+ | |||
+ | def log(self , msg): | ||
+ | """ | ||
+ | Logs the message to the fifo. Returns True if a reader was | ||
+ | connected and it was successfully logged, False otherwise. | ||
+ | """ | ||
+ | if self._fd < 0: | ||
+ | # We don't have an open handle, attempt to open the fifo | ||
+ | try: | ||
+ | self._openFifo() | ||
+ | except FifoError: | ||
+ | # FifoErrors are bad, send those all the way up | ||
+ | raise | ||
+ | except Exception , err: | ||
+ | # A simple connection error just means there is no attached | ||
+ | # reader, just reset the file descriptor and return False | ||
+ | self._fd = -1 | ||
+ | return False | ||
+ | if self._showTime: | ||
+ | # Add a time.ctime() to the beginning of the msg | ||
+ | msg = '%s: %s' % (time.ctime() , msg) | ||
+ | msg = msg.strip() | ||
+ | retries = 0 | ||
+ | while retries < 3: | ||
+ | # Try to write to the fifo 3 times, then fail | ||
+ | try: | ||
+ | os.write(self._fd , ' | ||
+ | except Exception , err: | ||
+ | retries += 1 | ||
+ | else: | ||
+ | return True | ||
+ | return False | ||
+ | |||
+ | def close(): | ||
+ | """ | ||
+ | Closes down the write end of the pipe, if it is open | ||
+ | """ | ||
+ | if self._fd > 0: | ||
+ | os.close(self._fd) | ||
+ | |||
+ | def isFifo(self , path): | ||
+ | """ | ||
+ | Returns a bool indicating whether the given path is a fifo | ||
+ | """ | ||
+ | s = os.stat(path) | ||
+ | if s[0] & stat.S_IFIFO: | ||
+ | return True | ||
+ | return False | ||
+ | |||
+ | def _openFifo(self): | ||
+ | """ | ||
+ | Attempts to open the fifo | ||
+ | """ | ||
+ | if os.path.exists(self._fifoPath) and \ | ||
+ | not self.isFifo(self._fifoPath): | ||
+ | # We have a file with the same name as the fifo path, but | ||
+ | # it is not a fifo, raise an exception | ||
+ | raise FifoError , 'The file, %s, is not a fifo' % self._fifoPath | ||
+ | elif not os.path.exists(self._fifoPath): | ||
+ | # Nothing exists at the path so create the fifo | ||
+ | os.mkfifo(self._fifoPath) | ||
+ | # Attempt to open the write end of the pipe | ||
+ | self._fd = os.open(self._fifoPath , os.O_WRONLY | os.O_NONBLOCK) | ||
+ | |||
+ | if __name__ == ' | ||
+ | # Run a test | ||
+ | fifo = '/ | ||
+ | l = FifoLog(fifo) | ||
+ | for i in xrange(1000): | ||
+ | l.log(' | ||
+ | time.sleep(1) | ||
+ | l.close() | ||
+ | </ | ||
+ | |||
+ | ===== Usage ===== | ||
+ | <code python> | ||
+ | import fifologger | ||
+ | fifo = '/ | ||
+ | l = fifologger.FifoLog(fifo) | ||
+ | for i in xrange(1000): | ||
+ | l.log(' | ||
+ | time.sleep(1) | ||
+ | l.close() | ||
+ | </ | ||
+ | |||
+ | ===== Install ===== | ||
+ | Simply {{: |
programming/python/fifologger.txt · Last modified: 2008/12/18 20:23 by crustymonkey