Source

bmutils / bmtail.py

Full commit
#!/usr/bin/env python
import multiprocessing
import sys

from bmutils import logWorker

__author__ = "Peter Nixon"
__copyright__ = "Copyright (C) 2012 Peter Nixon"
__license__ = "Public Domain"
__version__ = "1.2"

DEBUG = False
        
def _main(files, options):
    logQueue = multiprocessing.Queue()

    for filepath in files:
        # Start a logWorker process for each file
        p = multiprocessing.Process(target=logWorker, args=(filepath, logQueue, options.code))
        p.daemon = True
        p.start()

    try:
        while (logQueue):
            # Loop through the log queue and print the relevant fields
            log = logQueue.get()
            print log.timestamp, log.msisdn, log.client, log.server, log.method, log.status, log.url
            
    except KeyboardInterrupt:
        pass    # Escape silently
    finally:
        # Cleanup on the way out
        for filepath in files:
            p.terminate()
            p.join()
        
def main():
    from optparse import OptionParser

    parser = OptionParser(usage='usage: %prog [options] filename')

    parser.add_option("-c", "--code", dest="code", default=None, type="int",
                      help='Filter output on HTTP Status code (default: None)')
                      
    parser.add_option('-n', '--lines', dest='lines', default=10, type='int',
                      help='output the last N lines, instead of the last 10')

    parser.add_option('-s', '--sleep-interval', dest='sleep', default=1.0, metavar='S', type='float',
                      help='with  -f,  sleep  for  approximately  S  seconds between iterations')

    parser.add_option('', '--test', dest='test', default=False, action='store_true',
                      help='Run some basic tests')

    (options, args) = parser.parse_args()

    if not len(args) > 0:
        parser.print_help()
        sys.exit(1)
    else:
        _main(args, options)
        
if __name__ == '__main__':
    if DEBUG: multiprocessing.log_to_stderr(logging.DEBUG)
    main()