log_source.py
changeset 63 416560b82116
parent 55 5667d2bbdc50
child 64 cdb6403c2498
--- a/log_source.py	Mon Feb 09 07:15:19 2009 +0200
+++ b/log_source.py	Mon Feb 09 07:32:11 2009 +0200
@@ -31,7 +31,14 @@
         """
 
         abstract
-        
+    
+    def get_search (self, query) :
+        """
+            Search the logs for the given query
+        """
+
+        abstract
+
 class LogFile (object) :
     """
         A file containing LogEvents
@@ -286,41 +293,58 @@
             # one day sdrawkcab
             dtz -= ONE_DAY
     
-    def get_latest (self, count) :
+    def _iter_logfile_reverse (self, dt=None, max_files=100) :
         """
-            Uses _iter_backwards + _get_logfile_date to read the yield the given lines from as many logfiles as needed
+            Yields a series of LogFile objects, iterating backwards in time starting at the given datetime, or the
+            current date, if none given.
+
+            Reads/probes at most max_files files.
         """
         
-        # iterate backwards from now
-        day_iter = self._iter_date_reverse()
-
-        # number of files read
-        files = 0
-
-        # only read up to 100 files or so
-        MAX_FILES = 100
+        # start counting at zero...
+        file_count = 0
 
-        # read the events into here
-        lines = []
-        
-        # loop until done
-        while len(lines) < count :
+        # iterate backwards over days
+        for day in self._iter_date_reverse(dt) :
+            # stop if we've handled enough files by now
+            if file_count > max_files :
+                break
+            
+            # try and open the next logfile
             logfile = None
-
-            # get next logfile
-            files += 1
             
-            # open
-            logfile = self._get_logfile_date(day_iter.next())
+            file_count += 1
+            logfile = self._get_logfile_date(day)
             
+            # no logfile there?
             if not logfile :
-                if files > MAX_FILES :
+                # if we didn't find any logfiles at all, terminate rudely
+                if file_count > max_files :
                     raise Exception("No recent logfiles found")
                 
                 else :
                     # skip to next day
                     continue
             
+            # yield it
+            yield logfile
+
+    def get_latest (self, count) :
+        """
+            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()
+
             # read the events
             # XXX: use a queue
             lines = list(logfile.read_latest(count)) + lines
@@ -382,3 +406,18 @@
         # return set
         return days
 
+    def get_search (self, query) :
+        """
+            Just inspect the latest logfile
+        """
+
+        # one logfile
+        logfile = self._iter_logfile_reverse().next()
+
+        # inspect each line
+        for line in logfile.read_full() :
+            # XXX: use proper LogQuery stuff
+            if query in line.data :
+                yield line
+        
+