X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdaklog.py;h=a698cbc53fc5d754fb3ec2392aab73b299c3e1eb;hb=391f5ec09a119131dc846b796ca791f4cecc69e4;hp=fb33b0bdab18160525e1c3183d828f1559abba0e;hpb=d5c5b6de82b57502530c135cd2ff4d0f23ed0767;p=dak.git diff --git a/daklib/daklog.py b/daklib/daklog.py index fb33b0bd..a698cbc5 100644 --- a/daklib/daklog.py +++ b/daklib/daklog.py @@ -24,6 +24,7 @@ Logging functions ################################################################################ +import fcntl import os import pwd import time @@ -32,34 +33,44 @@ 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, print_starting=True): - "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.makedirs(logdir, 0o2775) 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) + umask = os.umask(0o0002) logfile = utils.open_file(logfilename, 'a') os.umask(umask) + self.logfile = logfile - if print_starting: - self.log(["program start"]) def log (self, details): "Log an event" @@ -70,13 +81,14 @@ class Logger: details.insert(0, timestamp) # Force the contents of the list to be string.join-able details = [ str(i) for i in details ] + fcntl.lockf(self.logfile, fcntl.LOCK_EX) # Write out the log in TSV self.logfile.write("|".join(details)+'\n') # Flush the output to enable tail-ing self.logfile.flush() + fcntl.lockf(self.logfile, fcntl.LOCK_UN) def close (self): "Close a Logger object" self.log(["program end"]) - self.logfile.flush() self.logfile.close()