X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdaklog.py;h=7ececa560ebc74f5405dfc8f73a571b77691a9ca;hb=a4eef13f8450fe8b109f4fafc074d50f0fda0a7c;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..7ececa56 --- a/daklib/daklog.py +++ b/daklib/daklog.py @@ -32,39 +32,49 @@ import utils ################################################################################ -class Logger: +class Logger(object): "Logger object" - Cnf = None - logfile = None - program = None + __shared_state = {} + + def __init__(self, program='unknown', debug=False, print_starting=True, include_pid=False): + self.__dict__ = self.__shared_state - def __init__ (self, Cnf, program, debug=0): - "Initialize a new Logger object" - self.Cnf = Cnf self.program = program + self.debug = debug + self.include_pid = include_pid + + if not getattr(self, 'logfile', None): + self._open_log(debug) + + if print_starting: + self.log(["program start"]) + + def _open_log(self, debug): # 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]) 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)