6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2001, 2002, 2006 James Troup <james@nocrew.org>
8 @license: GNU General Public License version 2 or later
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 ################################################################################
33 ################################################################################
39 def __init__(self, *args, **kwargs):
40 self.__dict__ = self.__shared_state
42 if not getattr(self, 'initialised', False):
43 from daklib.config import Config
44 self.initialised = True
46 # To be backwards compatibile, dump the first argument if it's a
47 # Config object. TODO: Fix up all callers and remove this
48 if len(args) > 0 and isinstance(args[0], Config):
52 self.__setup(*args, **kwargs)
55 def __setup(self, program='unknown', debug=False, print_starting=True, include_pid=False):
56 "Initialize a new Logger object"
57 self.program = program
59 self.include_pid = include_pid
61 # Create the log directory if it doesn't exist
62 from daklib.config import Config
63 logdir = Config()["Dir::Log"]
64 if not os.path.exists(logdir):
65 umask = os.umask(00000)
66 os.makedirs(logdir, 02775)
70 logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
76 umask = os.umask(00002)
77 logfile = utils.open_file(logfilename, 'a')
80 self.logfile = logfile
83 self.log(["program start"])
85 def log (self, details):
87 # Prepend timestamp, program name, and user name
88 details.insert(0, utils.getusername())
89 details.insert(0, self.program)
90 timestamp = time.strftime("%Y%m%d%H%M%S")
91 details.insert(0, timestamp)
92 # Force the contents of the list to be string.join-able
93 details = [ str(i) for i in details ]
94 # Write out the log in TSV
95 self.logfile.write("|".join(details)+'\n')
96 # Flush the output to enable tail-ing
100 "Close a Logger object"
101 self.log(["program end"])