log_source.py
changeset 81 745032a57803
parent 78 85345abbd46a
child 82 afd3120ec71e
equal deleted inserted replaced
80:a0662cff1d9d 81:745032a57803
   128         self.charset = charset
   128         self.charset = charset
   129         self.sep = sep
   129         self.sep = sep
   130 
   130 
   131         # open
   131         # open
   132         self.file = open(path, 'rb')
   132         self.file = open(path, 'rb')
   133     
   133 
   134     def __iter__ (self) :
   134     def __iter__ (self) :
   135         """
   135         """
   136             Yields a series of unicode lines, as read from the top of the file
   136             Yields a series of unicode lines, as read from the top of the file
   137         """
   137         """
   138         
   138         
   343             given *datetime*, or the the current date, if none given
   343             given *datetime*, or the the current date, if none given
   344         """
   344         """
   345         
   345         
   346         # default to now
   346         # default to now
   347         if not dt :
   347         if not dt :
   348             dt = datetime.datetime.now(pytz.utc)
   348             dtz = self.tz.localize(datetime.datetime.now())
   349         
   349 
   350         # convert to target timezone
   350         else :
   351         dtz = dt.astimezone(self.tz)
   351             # convert to target timezone
       
   352             dtz = dt.astimezone(self.tz)
   352 
   353 
   353         # our timedelta
   354         # our timedelta
   354         ONE_DAY = datetime.timedelta(1)
   355         ONE_DAY = datetime.timedelta(1)
   355         
   356         
   356         # iterate unto infinity
   357         # iterate unto infinity
   369             Reads/probes at most max_files files.
   370             Reads/probes at most max_files files.
   370         """
   371         """
   371         
   372         
   372         # start counting at zero...
   373         # start counting at zero...
   373         file_count = 0
   374         file_count = 0
       
   375 
       
   376         # have we found any files at all so far?
       
   377         have_found = False
   374 
   378 
   375         # iterate backwards over days
   379         # iterate backwards over days
   376         for day in self._iter_date_reverse(dt) :
   380         for day in self._iter_date_reverse(dt) :
   377             # stop if we've handled enough files by now
   381             # stop if we've handled enough files by now
   378             if file_count > max_files :
   382             if file_count > max_files :
   384             file_count += 1
   388             file_count += 1
   385             logfile = self._get_logfile_date(day)
   389             logfile = self._get_logfile_date(day)
   386             
   390             
   387             # no logfile there?
   391             # no logfile there?
   388             if not logfile :
   392             if not logfile :
   389                 # if we didn't find any logfiles at all, terminate rudely
   393                 # hit our limit?
   390                 if file_count > max_files :
   394                 if file_count > max_files :
   391                     raise Exception("No recent logfiles found")
   395                     # if we didn't find any logfiles at all, terminate rudely
   392                 
   396                     if not have_found :
       
   397                         raise Exception("No recent logfiles found")
       
   398                     
       
   399                     else :
       
   400                         # stop looking, deal with what we've got
       
   401                         return
       
   402 
   393                 else :
   403                 else :
   394                     # skip to next day
   404                     # skip to next day
   395                     continue
   405                     continue
   396             
   406             
       
   407             # mark have_found
       
   408             have_found = True
       
   409 
   397             # yield it
   410             # yield it
   398             yield logfile
   411             yield logfile
   399 
   412 
   400     def get_latest (self, count) :
   413     def get_latest (self, count) :
   401         """
   414         """
   402             Uses _iter_backwards + _get_logfile_date to read the yield the given lines from as many logfiles as needed
   415             Uses _iter_backwards + _get_logfile_date to read the yield the given lines from as many logfiles as needed
   403         """
   416         """
   404 
   417 
   405         # iterate over logfiles
       
   406         iter = self._iter_logfile_reverse()
       
   407         
       
   408         # read the events into here
   418         # read the events into here
   409         lines = []
   419         lines = []
   410         
   420         
   411         # loop until done
   421         # start reading in those logfiles
   412         while len(lines) < count :
   422         for logfile in self._iter_logfile_reverse() :
   413             # next logfile
       
   414             logfile = iter.next()
       
   415 
       
   416             # read the events
   423             # read the events
   417             # XXX: use a queue
   424             # XXX: use a queue
   418             lines = list(logfile.read_latest(count)) + lines
   425             lines = list(logfile.read_latest(count)) + lines
       
   426 
       
   427             # done?
       
   428             if len(lines) >= count :
       
   429                 break
   419         
   430         
   420         # return the events
   431         # return the events
   421         return lines
   432         return lines
   422 
   433 
   423     def get_date (self, dt) :
   434     def get_date (self, dt) :