]> git.decadent.org.uk Git - dak.git/blob - daklib/daklog.py
Merge branch 'merge'
[dak.git] / daklib / daklog.py
1 #!/usr/bin/env python
2
3 """
4 Logging functions
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2001, 2002, 2006  James Troup <james@nocrew.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 ################################################################################
26
27 import os
28 import pwd
29 import time
30 import sys
31 import utils
32
33 ################################################################################
34
35 class Logger:
36     "Logger object"
37     Cnf = None
38     logfile = None
39     program = None
40
41     def __init__ (self, Cnf, program, debug=0):
42         "Initialize a new Logger object"
43         self.Cnf = Cnf
44         self.program = program
45         # Create the log directory if it doesn't exist
46         logdir = Cnf["Dir::Log"]
47         if not os.path.exists(logdir):
48             umask = os.umask(00000)
49             os.makedirs(logdir, 02775)
50             os.umask(umask)
51         # Open the logfile
52         logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
53         logfile = None
54         if debug:
55             logfile = sys.stderr
56         else:
57             umask = os.umask(00002)
58             logfile = utils.open_file(logfilename, 'a')
59             os.umask(umask)
60         self.logfile = logfile
61         self.log(["program start"])
62
63     def log (self, details):
64         "Log an event"
65         # Prepend timestamp, program name, and user name
66         details.insert(0, utils.getusername())
67         details.insert(0, self.program)
68         timestamp = time.strftime("%Y%m%d%H%M%S")
69         details.insert(0, timestamp)
70         # Force the contents of the list to be string.join-able
71         details = [ str(i) for i in details ]
72         # Write out the log in TSV
73         self.logfile.write("|".join(details)+'\n')
74         # Flush the output to enable tail-ing
75         self.logfile.flush()
76
77     def close (self):
78         "Close a Logger object"
79         self.log(["program end"])
80         self.logfile.flush()
81         self.logfile.close()