stat #print(new.st_ino, ' = ', cur.st_ino) if ( ( self. file, "head" ) except IOError as e : #print("FILE DOES NOT EXIST") return False new = self. file, start_pos = "head" ) except IOError : return False return True # save current values fh = self. """ #print("Reopening", self.file, ".", end="") # if we don't have an opened file already then try to open it now if not self. If it doesn't appear to have changed (been rotated) then the current file handle is not changed. file = file def reopen ( self ): """ Attempt to reopen the current file. Not sure if this can actually # happen, but better safe than sorry. # This avoids a potential race condition if the file was being rotated # in the process of opening the file. SEEK_END ) # if we passed the end of the file rewind to the actual end. SEEK_SET ) else : # Absolute position (from end) fh. SEEK_END ) # End of file elif start_pos = "start" or start_pos = "head" : #fh.seek(0, os.SEEK_SET) # Beginning of file pass elif start_pos is not None : if start_pos >= 0 : # Absolute position fh. encoding ) # seek to the initial position in the file we want to start reading if start_pos = "end" or start_pos = "tail" : fh. last_count = 0 def open ( self, file, start_pos = "head" ): """Open the file to tail and initialize our state.""" fh = open ( file, "r", encoding = self. open ( file, start_pos = start_pos ) # initialize some internal vars self. encoding = encoding # will throw exception if it fails. max_wait = max_wait #self.max_buffer_size = max_buffer_size #self.min_interval = min_interval self. """ def _init_ ( self, file, # filename to monitor start_pos = "end", # where to initially start reading from #max_buffer_size=16384, # Max buffer size hint (Not exact file.readlines) interval = 0.1, # sleep time to wait if no data is present (dynamically changes) #min_interval=0.01, # min sleep time max_interval = 5, # max sleep time max_wait = 60, # max time to wait with no data before reopening file reopen_check = "inode", # how to check if file is different (inode or time) - inode does not work on win32 encoding = "utf-8" # file encoding ): self. Inspiration came from the perl module File::Tail. Example: from filetail import FileTail tail = FileTail("/var/log/syslog") for line in tail: # do something pass """ import os import sys from stat import * from math import floor from time import sleep, time class FileTail ( object ): """ Tail a file, even if its rotated/truncated. The longer the amount of time goes by w/o new data the longer the sleep interval will be (up to "max_interval") and starts at "interval". A simple algorithm is used to dynamically sleep when no new data is available in the file. Transparently handles files that get rotated or trucated. If you've already got an event loop going elsewhere (e.g., because this is a Tk application) then you don't need the vwait forever.""" $Id: filetail.py 1512 16:14:29Z morrissj $ Python3 module for tailing a file such as a system log that grows continuously. Note the vwait forever at the end that runs the event loop so that timer callbacks scheduled with after can actually be run. proc processLogLine ]Īfter $tenMinutesInMillis Saving the location is done with chan tell and restoring the location is done with chan seek. Assuming that the log file is only being appended to, you can simply save where you are before closing it and restore that location back when you reopen.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |