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