Logger is singleton now, utilize Logger & OptionsParser
authorKonstantin Stepanov <kstep@p-nut.info>
Sun, 28 Nov 2010 17:29:13 +0000 (19:29 +0200)
committerKonstantin Stepanov <kstep@p-nut.info>
Mon, 29 Nov 2010 03:31:35 +0000 (05:31 +0200)
dbuscron.py
dbuscron/command.py
dbuscron/logger.py

index a66ff68..7a775e7 100755 (executable)
@@ -15,7 +15,18 @@ import sys
 
 if __name__ == '__main__':
 
-    daemon = (len(sys.argv) < 2) or (sys.argv[1] != '-f')
+    from dbuscron import Logger, OptionsParser
+
+    options = OptionsParser('fvc:l:')
+    daemon = not options.f
+
+    logout = sys.stderr
+    if options.l:
+        logout = open(options.l, 'wb')
+
+    log = Logger(__name__, out=logout)
+    log.level = options.v + Logger.ERROR
+
     if daemon:
         from dbuscron.daemonize import daemonize
         daemonize(
@@ -27,12 +38,13 @@ if __name__ == '__main__':
 
     bus = DbusBus()
     commands = Commands()
-    crontab = CrontabParser('/etc/dbuscrontab')
+    crontab = CrontabParser(options.c or '/etc/dbuscrontab')
 
     for rule, cmd in crontab:
         matcher = DbusRule(**rule)
         command = Command(cmd)
         matcher.register()
+        log.info('%s %s' % (matcher, command))
         commands.add(matcher, command)
 
     commands.environ = crontab.environ
index 3713c17..c48df51 100644 (file)
@@ -1,6 +1,8 @@
 
 import os
 from dbuscron.bus import get_dbus_message_type, dbus_to_str
+from dbuscron.logger import Logger
+log = Logger(__name__)
 
 class Command(object):
     def __init__(self, cmd):
@@ -29,6 +31,7 @@ class Command(object):
                 )
         env.update(dbus_env)
         result = os.spawnvpe(os.P_WAIT, self.__file, self.__args, env)
+        log.info('run %s %s %s %s' % (self.__file, self.__args, dbus_env, result))
         return result
 
     @property
@@ -58,6 +61,7 @@ class Commands(object):
     def handler(self, bus, message):
         for rule, command in self.__commands.iteritems():
             if rule.match(bus, message):
+                log.info('matched %s %s' % (rule, command))
                 command(bus, message, self.__environ)
                 return
 
index 4a69941..eccbdc0 100644 (file)
@@ -8,12 +8,19 @@ class Logger(object):
     __out = None
     __name = None
 
+    __instance = None
+
     DEBUG = 4
     INFO = 3
     WARNING = 2
     ERROR = 1
     PANIC = 0
 
+    def __new__(cls, name, out=sys.stderr):
+        if cls.__instance is None:
+            cls.__instance = super(Logger, cls).__new__(cls)
+        return cls.__instance
+
     def __init__(self, name, out=sys.stderr):
         self.__out = out
         self.__name = name