]> git.decadent.org.uk Git - dak.git/blob - logging.py
sync
[dak.git] / logging.py
1 # Logging functions
2 # Copyright (C) 2001  James Troup <james@nocrew.org>
3 # $Id: logging.py,v 1.1 2001-07-07 03:59:45 troup Exp $
4
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.
9
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.
14
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
18     
19 ################################################################################
20
21 import os, pwd, string, time
22 import utils
23
24 ################################################################################
25
26 class Logger:
27     "Logger object"
28     Cnf = None;
29     logfile = None;
30     program = None;
31
32     def __init__ (self, Cnf, program):
33         "Initialize a new Logger object"
34         self.Cnf = Cnf;
35         self.program = program;
36         # Create the log directory if it doesn't exist
37         logdir = Cnf["Dir::LogDir"];
38         if not os.path.exists(logdir):
39             umask = os.umask(00000);
40             os.makedirs(logdir, 02775);
41         # Open the logfile
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
45         logfile.seek(0,2); 
46         self.logfile = logfile;
47         # Log the start of the program
48         user = pwd.getpwuid(os.getuid())[0];
49         self.log(["program start", user]);
50
51     def log (self, details):
52         "Log an event"
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
62         self.logfile.flush();
63
64     def close (self):
65         "Close a Logger object"
66         self.log(["program end"]);
67         self.logfile.flush();
68         self.logfile.close();