]> git.decadent.org.uk Git - dak.git/blob - daklib/daklog.py
Add by-hash support
[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 fcntl
28 import os
29 import pwd
30 import time
31 import sys
32 import utils
33
34 ################################################################################
35
36 class Logger(object):
37     "Logger object"
38     __shared_state = {}
39
40     def __init__(self, program='unknown', debug=False, print_starting=True, include_pid=False):
41         self.__dict__ = self.__shared_state
42
43         self.program = program
44         self.debug = debug
45         self.include_pid = include_pid
46
47         if not getattr(self, 'logfile', None):
48             self._open_log(debug)
49
50         if print_starting:
51             self.log(["program start"])
52
53     def _open_log(self, debug):
54         # Create the log directory if it doesn't exist
55         from daklib.config import Config
56         logdir = Config()["Dir::Log"]
57         if not os.path.exists(logdir):
58             umask = os.umask(00000)
59             os.makedirs(logdir, 0o2775)
60             os.umask(umask)
61
62         # Open the logfile
63         logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
64         logfile = None
65
66         if debug:
67             logfile = sys.stderr
68         else:
69             umask = os.umask(0o0002)
70             logfile = utils.open_file(logfilename, 'a')
71             os.umask(umask)
72
73         self.logfile = logfile
74
75     def log (self, details):
76         "Log an event"
77         # Prepend timestamp, program name, and user name
78         details.insert(0, utils.getusername())
79         details.insert(0, self.program)
80         timestamp = time.strftime("%Y%m%d%H%M%S")
81         details.insert(0, timestamp)
82         # Force the contents of the list to be string.join-able
83         details = [ str(i) for i in details ]
84         fcntl.lockf(self.logfile, fcntl.LOCK_EX)
85         # Write out the log in TSV
86         self.logfile.write("|".join(details)+'\n')
87         # Flush the output to enable tail-ing
88         self.logfile.flush()
89         fcntl.lockf(self.logfile, fcntl.LOCK_UN)
90
91     def close (self):
92         "Close a Logger object"
93         self.log(["program end"])
94         self.logfile.close()