X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdaklog.py;fp=daklib%2Fdaklog.py;h=0cca205e96bf83b6e1cad2c7f785e650c814b848;hb=307823ca2367f65ccfbbc7c0bd5054f2883b9c9c;hp=0000000000000000000000000000000000000000;hpb=10c964315953c1da8a3f388c4fd9c11c94fa8af4;p=dak.git diff --git a/daklib/daklog.py b/daklib/daklog.py new file mode 100755 index 00000000..0cca205e --- /dev/null +++ b/daklib/daklog.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +""" +Logging functions + +@contact: Debian FTP Master +@copyright: 2001, 2002, 2006 James Troup +@license: GNU General Public License version 2 or later +""" + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +################################################################################ + +import os +import pwd +import time +import sys +import utils + +################################################################################ + +class Logger: + "Logger object" + Cnf = None + logfile = None + program = None + + def __init__ (self, Cnf, program, debug=0): + "Initialize a new Logger object" + self.Cnf = Cnf + self.program = program + # Create the log directory if it doesn't exist + logdir = Cnf["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 + details.insert(0, self.program) + timestamp = time.strftime("%Y%m%d%H%M%S") + details.insert(0, timestamp) + # Force the contents of the list to be string.join-able + details = [ str(i) for i in details ] + # Write out the log in TSV + self.logfile.write("|".join(details)+'\n') + # Flush the output to enable tail-ing + self.logfile.flush() + + def close (self): + "Close a Logger object" + self.log(["program end"]) + self.logfile.flush() + self.logfile.close()