#!/usr/bin/python
#
# Copyright 2016 BMC Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import apachelog
import sys
import time


def monitor_file(f):
    """
    Reads a line from a file when available
    :param f: open file
    :return: a line from the file
    """
    # Go to the end of the file
    f.seek(0, 2)

    # Loop waiting for lines to be written
    while True:
        log_line = f.readline()
        # If there is nothing to read then wait a bit
        # and try again
        if not log_line:
            time.sleep(0.1)
            continue
        # We have a line return the line
        yield log_line


# %b - Size
# %h - Remote IP or Host
# %l - Remote Log Name
# %r - Request
# %>s - HTTP Status Code
# %t - eventTime
# %u - Remote User
# %{Referer}i - Referer
# %{User-agent}i - UserAgent

# log_format = r'%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}\i"'
# p = apachelog.parser(log_format)


def parse_apache_line(parser, line):
    s = None
    try:
        s = parser.parse(line)
    except apachelog.ApacheLogParserError:
        sys.stderr.write("Unable to parse %s" % line)
    return s

@@ -614,16 +614,26 @@
 achelog%0A
+import os%0A
 import s
@@ -1653,8 +1653,690 @@
 eturn s%0A
+%0A%0Aclass LogfileParser(object):%0A    def __init__(self, path):%0A        %22%22%22%0A        Constructs a Logfile parser instance given a path to a log file%0A        :param path:%0A        :return:%0A        %22%22%22%0A        # Opened handle to our log file%0A        self.log_file = open(path, %22r%22)%0A%0A    def monitor_file(self):%0A        lines = monitor_file(self.log_file)%0A        for line in lines:%0A            self.parse_line(line.strip())%0A%0A    def parse_line(self, line):%0A        print(line)%0A%0Aif __name__ == '__main__':%0A    if len(sys.argv) == 2:%0A        parser = LogfileParser()%0A        parser.monitor_file()%0A    else:%0A        sys.stderr.write(%22usage: %7B0%7D %3Cpath%3E%22.format(os.path.baseline(sys.argv%5B0%5D)))%0A

Add the LogfileParser class