]> git.decadent.org.uk Git - dak.git/blob - logging.py
lots and lots of python 2.1 changes. rene: remove bogus argument handling. katie...
[dak.git] / logging.py
1 #!/usr/bin/env python
2
3 # Logging functions
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 $
6
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.
11
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.
16
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
20
21 ################################################################################
22
23 import os, pwd, time;
24 import utils;
25
26 ################################################################################
27
28 class Logger:
29     "Logger object"
30     Cnf = None;
31     logfile = None;
32     program = None;
33
34     def __init__ (self, Cnf, program):
35         "Initialize a new Logger object"
36         self.Cnf = Cnf;
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);
43         # Open the logfile
44         logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"));
45         logfile = utils.open_file(logfilename, 'a');
46         # Seek to the end of the logfile
47         logfile.seek(0,2);
48         self.logfile = logfile;
49         # Log the start of the program
50         user = pwd.getpwuid(os.getuid())[0];
51         self.log(["program start", user]);
52
53     def log (self, details):
54         "Log an event"
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
64         self.logfile.flush();
65
66     def close (self):
67         "Close a Logger object"
68         self.log(["program end"]);
69         self.logfile.flush();
70         self.logfile.close();