1 import logging, logging.handlers, os, time, queue, threading
6 import __main__ as main
7 self.name = os.path.basename(main.__file__)
8 self.logger = logging.getLogger(self.name)
9 self.logger.setLevel(logging.INFO)
10 self.handler = logging.handlers.SysLogHandler(address = '/dev/log', facility = logging.handlers.SysLogHandler.LOG_LOCAL0)
11 self.logger.addHandler(self.handler)
13 def log (self, lvl, what):
14 thestr = "%s[%d]: %s" % (self.name,os.getpid(),what)
16 self.logger.log(lvl, thestr)
18 def debug(self, what):
19 self.log(logging.DEBUG, what)
21 self.log(logging.INFO, what)
22 def warning(self, what):
23 self.log(logging.WARNING, what)
24 def error(self, what):
25 self.log(logging.ERROR, what)
26 def critical(self, what):
27 self.log(logging.CRITICAL, what)
31 # Threaded callback class
32 class ThreadFunction():
36 def __init__(self, f):
38 self._q = queue.Queue()
39 self._t = threading.Thread(target=self._thread_func)
42 def _thread_func(self):
44 (cmd, data) = self._q.get()
50 logger.error("ThreadFunction: Got exception out of handler thread: %s" % str(e))
55 logger.error("ThreadFunction: Command %d does not exist" % cmd)
57 def __call__(self, *arg):
58 self._q.put((self._CALL, arg))
61 self._q.put((_TERM, None))
64 # Thread timer-repeater class: Call a function every <sleep_time> seconds
65 class ThreadRepeater():
66 def __init__(self, f, sleep_time):
69 self._sleep_time = sleep_time
70 self._t = threading.Thread(target=self._thread_func)
78 time.sleep(sleep_time)