1 import logging, logging.handlers, os, time, queue, threading, subprocess
6 self.logger = logging.getLogger("tuerd")
7 self.logger.setLevel(logging.INFO)
8 self.handler = logging.handlers.SysLogHandler(address = '/dev/log', facility = logging.handlers.SysLogHandler.LOG_LOCAL0)
9 self.logger.addHandler(self.handler)
11 def log (self, lvl, what):
12 thestr = "%s[%d]: %s" % ("osspd", os.getpid(), what)
14 self.logger.log(lvl, thestr)
16 def debug(self, what):
17 self.log(logging.DEBUG, what)
19 self.log(logging.INFO, what)
20 def warning(self, what):
21 self.log(logging.WARNING, what)
22 def error(self, what):
23 self.log(logging.ERROR, what)
24 def critical(self, what):
25 self.log(logging.CRITICAL, what)
29 # run a command asynchronously and log the return value if not 0
30 # prefix must be a string identifying the code position where the call came from
31 def fire_and_forget (cmd, log, prefix):
32 def _fire_and_forget ():
33 with open("/dev/null", "w") as fnull:
34 retcode = subprocess.call(cmd, stdout=fnull, stderr=fnull)
36 log("%sReturn code %d at command: %s" % (prefix,retcode,str(cmd)))
37 t = threading.Thread(target=_fire_and_forget)
40 # Threaded callback class
41 class ThreadFunction():
45 def __init__(self, f):
47 self._q = queue.Queue()
48 self._t = threading.Thread(target=self._thread_func)
51 def _thread_func(self):
53 (cmd, data) = self._q.get()
59 logger.error("ThreadFunction: Got exception out of handler thread: %s" % str(e))
64 logger.error("ThreadFunction: Command %d does not exist" % cmd)
66 def __call__(self, *arg):
67 self._q.put((self._CALL, arg))
70 self._q.put((_TERM, None))
73 # Thread timer-repeater class: Call a function every <sleep_time> seconds
74 class ThreadRepeater():
75 def __init__(self, f, sleep_time):
78 self._sleep_time = sleep_time
79 self._t = threading.Thread(target=self._thread_func)
87 time.sleep(sleep_time)