pvl.irker-syslog: implement a crude mainloop using syslog, to detect EOF on irker
authorTero Marttila <terom@paivola.fi>
Sun, 13 Jan 2013 00:25:59 +0200
changeset 113 49e13576d77c
parent 112 8127c0f4223d
child 114 2e88e1d8e604
pvl.irker-syslog: implement a crude mainloop using syslog, to detect EOF on irker
bin/pvl.irker-syslog
--- a/bin/pvl.irker-syslog	Sun Jan 13 00:25:01 2013 +0200
+++ b/bin/pvl.irker-syslog	Sun Jan 13 00:25:59 2013 +0200
@@ -73,27 +73,49 @@
         irker[options.irker_target]
 
     log.info("Process syslog messages...")
-    for item in syslog.main() :
-        match, rulepath, apply = rules.apply(item)
-        
-        log.debug("%s: %s: %s", item, rulepath, apply)
-        
-        target = apply.get('irk', options.irker_target)
-        
-        tag = '/'.join(str(rule) for rule in reversed(rulepath[:-1]))
-        text = apply.get('text')
+
+    # customized mainloop that supports irker.irk
+    while True :
+        if irker.irk.recv :
+            reading = (irker.irk, )
+        else :
+            reading = ()
+
+        poll = syslog.poll(reading=reading) or () # timeout -> ()
+
+        if irker.irk in poll :
+            # irk
+            try :
+                for msg in irker.irk :
+                    log.info("irk: %s", msg)
+
+            except EOFError :
+                # exit
+                log.error("irk: EOF")
+                return 1
+
+        # syslogs, even on timeout
+        for item in syslog :
+            match, rulepath, apply = rules.apply(item)
+            
+            log.debug("%s: %s: %s", item, rulepath, apply)
+            
+            target = apply.get('irk', options.irker_target)
+            
+            tag = '/'.join(str(rule) for rule in reversed(rulepath[:-1]))
+            text = apply.get('text')
+            
+            log.info("%s: %s: %s", target, tag, text)
+
+            if not text :
+                # XXX: plain irk = ... in rule is broken, as it always applies, and skips any further rules
+                continue
+            
+            if irker and target :
+                irker[target]('[' + tag + '] ' + text)
+            else :
+                print tag, text
         
-        log.info("%s: %s: %s", target, tag, text)
-
-        if not text :
-            # XXX: plain irk = ... in rule is broken, as it always applies, and skips any further rules
-            continue
-        
-        if irker and target :
-            irker[target]('[' + tag + '] ' + text)
-        else :
-            print tag, text
-    
     # done
     log.info("Exiting...")
     return 0