fixbot/logwatch/sources.py
changeset 48 ba101beeb062
parent 40 b9fdb7710768
child 51 342850300d6a
equal deleted inserted replaced
47:81b37185209c 48:ba101beeb062
     1 from twisted.internet import reactor, protocol
     1 """
       
     2     Implementations of the various sources of log data
       
     3 """
       
     4 
       
     5 from twisted.internet import protocol
     2 from twisted.python import log
     6 from twisted.python import log
     3 
     7 
     4 from fixbot import fifo
     8 from fixbot import fifo
     5 
     9 
     6 class LogSource (object) :
    10 class LogSource (object) :
       
    11     """
       
    12         Reads lines of log data from some file or other source.
       
    13     """
       
    14 
     7     def __init__ (self, name, filters) :
    15     def __init__ (self, name, filters) :
       
    16         """
       
    17             name            - label lines read from this source
       
    18             filters         - LogFilter chain to pass lines through
       
    19         """
       
    20 
     8         # set later on
    21         # set later on
     9         self.module = None
    22         self.module = None
    10         
    23         
    11         # what filters to apply
    24         # what filters to apply
    12         self.filters = filters
    25         self.filters = filters
    23     def handleError (self, msg) :
    36     def handleError (self, msg) :
    24         log.err(msg)
    37         log.err(msg)
    25         self.module.error(msg)
    38         self.module.error(msg)
    26 
    39 
    27     def handleData (self, data) :
    40     def handleData (self, data) :
       
    41         """
       
    42             Buffer the given chunk of data, passing any full lines to handleLine
       
    43         """
       
    44 
    28         data = self.buf + data
    45         data = self.buf + data
    29         
    46         
    30         while "\n" in data :
    47         while "\n" in data :
    31             line, data = data.split("\n", 1)
    48             line, data = data.split("\n", 1)
    32 
    49 
    40         for filter in self.filters :
    57         for filter in self.filters :
    41             # let the filter process the line
    58             # let the filter process the line
    42             out = filter.test(line)
    59             out = filter.test(line)
    43 
    60 
    44             if out :
    61             if out :
       
    62                 # unpack
       
    63                 type, msg = out
       
    64 
    45                 # positive match, send
    65                 # positive match, send
    46                 log.msg("\t%s: %s" % (filter.event_type, out))
    66                 log.msg("\t%s: %s" % (type, msg))
    47                 self.module.sendEvent(filter.event_type, out)
    67                 
    48 
    68                 # drop until we have a module
       
    69                 if self.module :
       
    70                     self.module.sendEvent(type, msg)
       
    71                 
       
    72                 # ok, first hit does it
    49                 break
    73                 break
    50 
    74 
    51             elif out is False :
    75             elif out is False :
    52                 # negative match, stop processing
    76                 # negative match, stop processing
    53                 return
    77                 return
    54 
    78 
    55             else :  # None
    79             elif out is None :
    56                 # no match
    80                 # no match
    57                 continue
    81                 continue
       
    82 
       
    83             else :
       
    84                 raise ValueError(out)
    58 
    85 
    59 class File (LogSource, protocol.ProcessProtocol) :
    86 class File (LogSource, protocol.ProcessProtocol) :
    60     """
    87     """
    61         Stream lines from a regular file using /usr/bin/tail -f
    88         Stream lines from a regular file using /usr/bin/tail -f
    62     """
    89     """