pvl/syslog/parser.py
changeset 74 952ee07efd7a
parent 70 c8ec745a2aaa
child 101 6b27d7010bd4
equal deleted inserted replaced
73:ef01c4639689 74:952ee07efd7a
   117 
   117 
   118         # the message, including possible tag/pid
   118         # the message, including possible tag/pid
   119         +   r"(?P<message>(?P<tag>(?P<program>[^:\]]+)(?:\[(?P<pid>\d+)\])?: )?(?P<text>.*))\n?"
   119         +   r"(?P<message>(?P<tag>(?P<program>[^:\]]+)(?:\[(?P<pid>\d+)\])?: )?(?P<text>.*))\n?"
   120     )
   120     )
   121 
   121 
   122     def __init__ (self, raw=False) :
   122     def __init__ (self, raw=False, facility=None) :
   123         """
   123         """
   124             Using given underlying line source.
   124             Using given underlying line source.
   125         """
   125         """
   126 
   126 
   127         self.raw = raw
   127         self.raw = raw
       
   128         self.facility = facility
   128 
   129 
   129     def parse_pri (self, match) :
   130     def parse_pri (self, match) :
   130         """
   131         """
   131             Parse pri/facility/severity.
   132             Parse pri/facility/severity.
   132         """
   133         """
   133 
   134 
   134         pri = match.group('pri')
   135         pri = match.group('pri')
   135         facility = match.group('facility')
   136         facility = match.group('facility') or self.facility
   136         severity = match.group('severity')
   137         severity = match.group('severity')
   137         
   138         
   138         if pri.isdigit() :
   139         if pri and pri.isdigit() :
   139             pri = int(pri)
   140             pri = int(pri)
   140             facility, severity = divmod(pri, 8)
   141             facility, severity = divmod(pri, 8)
   141 
   142 
   142         return dict(
   143         return dict(
   143             pri         = pri,
   144             pri         = pri,
   179             Parse given input line into SyslogMessage.
   180             Parse given input line into SyslogMessage.
   180         """
   181         """
   181 
   182 
   182         # ignore whitespace
   183         # ignore whitespace
   183         line = line.strip()
   184         line = line.strip()
   184 
       
   185         # debug
       
   186         log.debug("%s", line)
       
   187 
   185 
   188         # timestamp?
   186         # timestamp?
   189         if self.raw :
   187         if self.raw :
   190             # from defaults
   188             # from defaults
   191             return dict(
   189             return dict(
   212                 pid         = match.group('pid'),
   210                 pid         = match.group('pid'),
   213                 msg         = match.group('text'),
   211                 msg         = match.group('text'),
   214             )
   212             )
   215            
   213            
   216             # facility/severity prefix?
   214             # facility/severity prefix?
   217             if match.group('pri') :
   215             item.update(self.parse_pri(match))
   218                 item.update(self.parse_pri(match))
       
   219 
   216 
   220             return item
   217             return item
   221     
   218     
   222     def process (self, lines) :
   219     def process (self, lines) :
   223         """
   220         """
   225         """
   222         """
   226 
   223 
   227         for line in lines :
   224         for line in lines :
   228             item = self.parse(line)
   225             item = self.parse(line)
   229 
   226 
       
   227             log.debug("%s", item)
       
   228 
   230             if item :
   229             if item :
   231                 yield item
   230                 yield item
   232 
   231 
   233     __call__ = process
   232     __call__ = process
   234 
   233