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