Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file rotate detect #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/tailer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import sys
import time
import os

if sys.version_info < (3,):
range = xrange
Expand All @@ -15,6 +16,7 @@ class Tailer(object):
def __init__(self, file, read_size=1024, end=False):
self.read_size = read_size
self.file = file
self.cur_file_ino = os.fstat(self.file.fileno()).st_ino
self.start_pos = self.file.tell()
if end:
self.seek_end()
Expand Down Expand Up @@ -150,6 +152,14 @@ def head(self, lines=10):
else:
return []

def _check_rotate(self):
if os.stat(self.file.name).st_ino != self.cur_file_ino:
new = open(self.file.name, "r")
self.file.close()
self.file = new
self.file.seek(0, 2)
self.cur_file_ino = os.fstat(self.file.fileno()).st_ino

def follow(self, delay=1.0):
"""\
Iterator generator that returns lines as data is added to the file.
Expand Down Expand Up @@ -180,6 +190,7 @@ def follow(self, delay=1.0):
trailing = True
self.seek(where)
time.sleep(delay)
self._check_rotate()

def __iter__(self):
return self.follow()
Expand Down