diff options
-rw-r--r-- | LogActio/__init__.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/LogActio/__init__.py b/LogActio/__init__.py index 6456b91..5a7c167 100644 --- a/LogActio/__init__.py +++ b/LogActio/__init__.py @@ -74,6 +74,7 @@ class WatcherThread(threading.Thread): self.start() def run(self): + fp = None # This is started by threading.Thread try: fp = fp = open(self.__logfile, "r") @@ -87,8 +88,21 @@ class WatcherThread(threading.Thread): where = fp.tell() line = fp.readline() if len(line) == 0: + # Before sleeping, grab a copy of the file size. + # If it has become truncated, we need to reopen the file + filesize_before = os.stat(self.__logfile).st_size time.sleep(self.__polltime) - fp.seek(where) + filesize_after = os.stat(self.__logfile).st_size + if filesize_after < filesize_before: + # Reopen is needed. + fp.close() + time.sleep(1) # Just in case + fp = open(self.__logfile) + # We will not go to end of file, so + # we catch all changes in the log file + # since the truncation point + else: + fp.seek(where) continue now = int(time.time()) |