]> git.decadent.org.uk Git - dak.git/blob - dak/lib/logging.py
Stop using silly names, and migrate to a saner directory structure.
[dak.git] / dak / lib / 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.4 2005-11-15 09:50:32 ajt 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, sys;
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, debug=0):
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 = None
46         if debug:
47             logfile = sys.stderr
48         else:
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]);
54
55     def log (self, details):
56         "Log an event"
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
66         self.logfile.flush();
67
68     def close (self):
69         "Close a Logger object"
70         self.log(["program end"]);
71         self.logfile.flush();
72         self.logfile.close();