--- /dev/null
+"""
+Utility classes.
+
+2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+"""
+
+class LiveLogStatus(object):
+ """
+ Status information regarding live log processing. Stored in the
+ '.lstatus' file.
+ """
+
+ def __init__(self, position=0):
+ self.position = 0
+
+
+class LiveLogFile(object):
+ """
+ Encapsulate file that is currently updated/appended as it is used
+ by a log file.
+ A live status file is used for storing status information (such as
+ current position in log file processing). The live status file name is the
+ log file name with ".lstatus" appended to it.
+ """
+
+ def __init__(self, log_file_name):
+ self.log_file_name = log_file_name
+ self.status_file_name = log_file_name + ".lstatus"
+
+ self.log_file = open(self.log_file_name, "tt")
+ self.status_file = open(self.status_file_name, "ab+")
+ self.status_file.seek(0)
+
+ self.load_status()
+ self.log_file.seek(self.status.position)
+
+ def close(self):
+ self.log_file.close()
+ self.status_file.close()
+
+ def load_status(self):
+ self.status_file.seek(0)
+
+ # If line is empty (file is just created), position is 0
+ line = self.status_file.readline()
+ try:
+ position = int(line)
+ except TypeError, e:
+ position = 0
+ self.status = LiveLogStatus(position)
+
+ self.status_file.seek(0)
+
+ def store_status(self):
+ self.status_file.seek(0)
+ self.status.position = self.log_file.tell()
+ self.status_file.write(str(self.status.position) + "\n")
+ self.status_file.seek(0)
+
+ def readline(self):
+ current_position = self.log_file.tell()
+ line = self.log_file.readline()
+
+ # In case of complete line, update status and return it.
+ if "\n" in line:
+ self.store_status()
+ return line
+
+ # In case of incomplete line, rewind and return "".
+ self.log_file.seek(current_position)
+ return ""