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