add #test logfile, improve handling of low-logfile situations
authorTero Marttila <terom@fixme.fi>
Tue, 10 Feb 2009 03:48:51 +0200
changeset 81 745032a57803
parent 80 a0662cff1d9d
child 82 afd3120ec71e
add #test logfile, improve handling of low-logfile situations
config.py
log_source.py
--- a/config.py	Tue Feb 10 03:22:43 2009 +0200
+++ b/config.py	Tue Feb 10 03:48:51 2009 +0200
@@ -31,6 +31,7 @@
 
 # the log parser that we use
 LOG_PARSER                      = IrssiParser(LOG_TIMEZONE, LOG_TIMESTAMP_FMT)
+LOG_PARSER_FULLTS               = IrssiParser(LOG_TIMEZONE, '%Y%m%d%H%M%S')
 
 # the statically defined channel list
 LOG_CHANNELS                    = ChannelList([
@@ -41,6 +42,10 @@
     LogChannel('openttd',   "OFTC",     "#openttd", 
         LogDirectory(relpath('logs/openttd'),   LOG_TIMEZONE, LOG_PARSER, LOG_CHARSET, LOG_FILENAME_FMT)
     ),
+
+    LogChannel('test',      "TEST",     "#test",
+        LogDirectory(relpath('/home/terom/irclogs/test'),  LOG_TIMEZONE, LOG_PARSER_FULLTS, LOG_CHARSET, LOG_FILENAME_FMT)
+    )
 ])
 
 # date format for URLs
--- a/log_source.py	Tue Feb 10 03:22:43 2009 +0200
+++ b/log_source.py	Tue Feb 10 03:48:51 2009 +0200
@@ -130,7 +130,7 @@
 
         # open
         self.file = open(path, 'rb')
-    
+
     def __iter__ (self) :
         """
             Yields a series of unicode lines, as read from the top of the file
@@ -345,10 +345,11 @@
         
         # default to now
         if not dt :
-            dt = datetime.datetime.now(pytz.utc)
-        
-        # convert to target timezone
-        dtz = dt.astimezone(self.tz)
+            dtz = self.tz.localize(datetime.datetime.now())
+
+        else :
+            # convert to target timezone
+            dtz = dt.astimezone(self.tz)
 
         # our timedelta
         ONE_DAY = datetime.timedelta(1)
@@ -372,6 +373,9 @@
         # start counting at zero...
         file_count = 0
 
+        # have we found any files at all so far?
+        have_found = False
+
         # iterate backwards over days
         for day in self._iter_date_reverse(dt) :
             # stop if we've handled enough files by now
@@ -386,14 +390,23 @@
             
             # no logfile there?
             if not logfile :
-                # if we didn't find any logfiles at all, terminate rudely
+                # hit our limit?
                 if file_count > max_files :
-                    raise Exception("No recent logfiles found")
-                
+                    # if we didn't find any logfiles at all, terminate rudely
+                    if not have_found :
+                        raise Exception("No recent logfiles found")
+                    
+                    else :
+                        # stop looking, deal with what we've got
+                        return
+
                 else :
                     # skip to next day
                     continue
             
+            # mark have_found
+            have_found = True
+
             # yield it
             yield logfile
 
@@ -402,20 +415,18 @@
             Uses _iter_backwards + _get_logfile_date to read the yield the given lines from as many logfiles as needed
         """
 
-        # iterate over logfiles
-        iter = self._iter_logfile_reverse()
-        
         # read the events into here
         lines = []
         
-        # loop until done
-        while len(lines) < count :
-            # next logfile
-            logfile = iter.next()
-
+        # start reading in those logfiles
+        for logfile in self._iter_logfile_reverse() :
             # read the events
             # XXX: use a queue
             lines = list(logfile.read_latest(count)) + lines
+
+            # done?
+            if len(lines) >= count :
+                break
         
         # return the events
         return lines