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)
44 logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
49 umask = os.umask(00002)
50 logfile = utils.open_file(logfilename, 'a')
52 self.logfile = logfile
53 # Log the start of the program
54 user = pwd.getpwuid(os.getuid())[0]
55 self.log(["program start", user])
57 def log (self, details):
59 # Prepend the timestamp and program name
60 details.insert(0, self.program)
61 timestamp = time.strftime("%Y%m%d%H%M%S")
62 details.insert(0, timestamp)
63 # Force the contents of the list to be string.join-able
64 details = [ str(i) for i in details ]
65 # Write out the log in TSV
66 self.logfile.write("|".join(details)+'\n')
67 # Flush the output to enable tail-ing
71 "Close a Logger object"
72 self.log(["program end"])