60f491be2d8226035b382e78ff97c2784ca3066c
[saartuer.git] / libtuer.py
1 import logging, logging.handlers, os, time, queue, threading
2
3 # logging function
4 class Logger:
5         def __init__ (self):
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)
12         
13         def log (self, lvl, what):
14                 thestr = "%s[%d]: %s" % (self.name,os.getpid(),what)
15                 print (thestr)
16                 self.logger.log(lvl, thestr)
17         
18         def debug(self, what):
19                 self.log(logging.DEBUG, what)
20         def info(self, 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)
28
29 logger = Logger()
30
31 # Threaded callback class
32 class ThreadFunction():
33         _CALL = 0
34         _TERM = 1
35         
36         def __init__(self, f):
37                 self._f = f
38                 self._q = queue.Queue()
39                 self._t = threading.Thread(target=self._thread_func)
40                 self._t.start()
41         
42         def _thread_func(self):
43                 while True:
44                         (cmd, data) = self._q.get()
45                         # run command
46                         if cmd == _CALL:
47                                 try:
48                                         self._f(*data)
49                                 except Exception:
50                                         logger.error("ThreadFunction: Got exception out of handler thread: %s" % str(e))
51                         elif cmd == _TERM:
52                                 assert data is None
53                                 break
54                         else:
55                                 logger.error("ThreadFunction: Command %d does not exist" % cmd)
56         
57         def __call__(self, *arg):
58                 self._q.put((self._CALL, arg))
59         
60         def stop(self):
61                 self._q.put((_TERM, None))
62                 self._t.join()
63
64 # Thread timer-repeater class: Call a function every <sleep_time> seconds
65 class ThreadRepeater():
66         def __init__(self, f, sleep_time):
67                 self._f = f
68                 self._stop = False
69                 self._sleep_time = sleep_time
70                 self._t = threading.Thread(target=self._thread_func)
71                 self._t.start()
72         
73         def _thread_func():
74                 while True:
75                         if self._stop:
76                                 break
77                         self._f()
78                         time.sleep(sleep_time)
79         
80         def stop(self):
81                 self._stop = True
82                 self._t.join()