4 # Copyright (C) 2001, 2002, 2006 James Troup <james@nocrew.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ################################################################################
22 import os, pwd, time, sys
25 ################################################################################
33 def __init__ (self, Cnf, program, debug=0):
34 "Initialize a new Logger object"
36 self.program = program
37 # Create the log directory if it doesn't exist
38 logdir = Cnf["Dir::Log"]
39 if not os.path.exists(logdir):
40 umask = os.umask(00000)
41 os.makedirs(logdir, 02775)
43 logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
48 logfile = utils.open_file(logfilename, 'a')
49 self.logfile = logfile
50 # Log the start of the program
51 user = pwd.getpwuid(os.getuid())[0]
52 self.log(["program start", user])
54 def log (self, details):
56 # Prepend the timestamp and program name
57 details.insert(0, self.program)
58 timestamp = time.strftime("%Y%m%d%H%M%S")
59 details.insert(0, timestamp)
60 # Force the contents of the list to be string.join-able
61 details = [ str(i) for i in details ]
62 # Write out the log in TSV
63 self.logfile.write("|".join(details)+'\n')
64 # Flush the output to enable tail-ing
68 "Close a Logger object"
69 self.log(["program end"])