From 0332a294da5e935eacf802173034525d2709c1b4 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Fri, 26 Aug 2011 13:34:47 +0300 Subject: [PATCH] ppf/new: Add util.py. util.py contains the LiveLogFile for live analysis of log files. A specialized status file holds the current position of processing the log file. In case of error, the analyzer would restart, read the status file and resume parsing from where it left off. --- ppf/new/util.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 ppf/new/util.py diff --git a/ppf/new/util.py b/ppf/new/util.py new file mode 100644 index 0000000..d807af5 --- /dev/null +++ b/ppf/new/util.py @@ -0,0 +1,71 @@ +""" +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 "" -- 2.20.1