X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdaklog.py;h=856dc84103b385f5be41f799c3543ad8ad433d3b;hb=e2baddaa67c0f22d42aff35dde78f09c72ab63a5;hp=0cca205e96bf83b6e1cad2c7f785e650c814b848;hpb=1c35448b880358d020e81339657e3435fdda9434;p=dak.git diff --git a/daklib/daklog.py b/daklib/daklog.py old mode 100755 new mode 100644 index 0cca205e..856dc841 --- a/daklib/daklog.py +++ b/daklib/daklog.py @@ -32,39 +32,60 @@ import utils ################################################################################ -class Logger: +class Logger(object): "Logger object" - Cnf = None - logfile = None - program = None + __shared_state = {} - def __init__ (self, Cnf, program, debug=0): + def __init__(self, *args, **kwargs): + self.__dict__ = self.__shared_state + + if not getattr(self, 'initialised', False): + from daklib.config import Config + self.initialised = True + + # To be backwards compatibile, dump the first argument if it's a + # Config object. TODO: Fix up all callers and remove this + if len(args) > 0 and isinstance(args[0], Config): + args = list(args) + args.pop(0) + + self.__setup(*args, **kwargs) + + + def __setup(self, program='unknown', debug=False, print_starting=True, include_pid=False): "Initialize a new Logger object" - self.Cnf = Cnf self.program = program + self.debug = debug + self.include_pid = include_pid + # Create the log directory if it doesn't exist - logdir = Cnf["Dir::Log"] + from daklib.config import Config + logdir = Config()["Dir::Log"] if not os.path.exists(logdir): umask = os.umask(00000) os.makedirs(logdir, 02775) os.umask(umask) + # Open the logfile logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m")) logfile = None + if debug: logfile = sys.stderr else: umask = os.umask(00002) logfile = utils.open_file(logfilename, 'a') os.umask(umask) + self.logfile = logfile - # Log the start of the program - user = pwd.getpwuid(os.getuid())[0] - self.log(["program start", user]) + + if print_starting: + self.log(["program start"]) def log (self, details): "Log an event" - # Prepend the timestamp and program name + # Prepend timestamp, program name, and user name + details.insert(0, utils.getusername()) details.insert(0, self.program) timestamp = time.strftime("%Y%m%d%H%M%S") details.insert(0, timestamp)