Merge branch 'master' of ralfj.de:saartuer
[saartuer.git] / libtuer.py
index 60f491be2d8226035b382e78ff97c2784ca3066c..1f8a034713795b1d32c10cc8695db0d19692df69 100644 (file)
@@ -1,17 +1,15 @@
-import logging, logging.handlers, os, time, queue, threading
+import logging, logging.handlers, os, time, queue, threading, subprocess
 
 # logging function
 class Logger:
        def __init__ (self):
 
 # logging function
 class Logger:
        def __init__ (self):
-               import __main__ as main
-               self.name = os.path.basename(main.__file__)
-               self.logger = logging.getLogger(self.name)
+               self.logger = logging.getLogger("tuerd")
                self.logger.setLevel(logging.INFO)
                self.handler = logging.handlers.SysLogHandler(address = '/dev/log', facility = logging.handlers.SysLogHandler.LOG_LOCAL0)
                self.logger.addHandler(self.handler)
        
        def log (self, lvl, what):
                self.logger.setLevel(logging.INFO)
                self.handler = logging.handlers.SysLogHandler(address = '/dev/log', facility = logging.handlers.SysLogHandler.LOG_LOCAL0)
                self.logger.addHandler(self.handler)
        
        def log (self, lvl, what):
-               thestr = "%s[%d]: %s" % (self.name,os.getpid(),what)
+               thestr = "%s[%d]: %s" % ("osspd", os.getpid(), what)
                print (thestr)
                self.logger.log(lvl, thestr)
        
                print (thestr)
                self.logger.log(lvl, thestr)
        
@@ -28,6 +26,17 @@ class Logger:
 
 logger = Logger()
 
 
 logger = Logger()
 
+# run a command asynchronously and log the return value if not 0
+# prefix must be a string identifying the code position where the call came from
+def fire_and_forget (cmd, log, prefix):
+       def _fire_and_forget ():
+               with open("/dev/null", "w") as fnull:
+                       retcode = subprocess.call(cmd, stdout=fnull, stderr=fnull)
+                       if retcode is not 0:
+                               log("%sReturn code %d at command: %s" % (prefix,retcode,str(cmd)))
+       t = threading.Thread(target=_fire_and_forget)
+       t.start()
+
 # Threaded callback class
 class ThreadFunction():
        _CALL = 0
 # Threaded callback class
 class ThreadFunction():
        _CALL = 0