4 # Copyright (C) 2001, 2002 James Troup <james@nocrew.org>
5 # $Id: logging.py,v 1.3 2002-10-16 02:47:32 troup 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 ################################################################################
26 ################################################################################
34 def __init__ (self, Cnf, program):
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"));
45 logfile = utils.open_file(logfilename, 'a');
46 # Seek to the end of the logfile
48 self.logfile = logfile;
49 # Log the start of the program
50 user = pwd.getpwuid(os.getuid())[0];
51 self.log(["program start", user]);
53 def log (self, details):
55 # Prepend the timestamp and program name
56 details.insert(0, self.program);
57 timestamp = time.strftime("%Y%m%d%H%M%S");
58 details.insert(0, timestamp);
59 # Force the contents of the list to be string.join-able
60 details = map(str, details);
61 # Write out the log in TSV
62 self.logfile.write("~".join(details)+'\n');
63 # Flush the output to enable tail-ing
67 "Close a Logger object"
68 self.log(["program end"]);