1"""
2Logging functions
4@contact: Debian FTP Master <ftpmaster@debian.org>
5@copyright: 2001, 2002, 2006 James Troup <james@nocrew.org>
6@license: GNU General Public License version 2 or later
7"""
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23################################################################################
25import fcntl
26import os
27import time
28import sys
29import traceback
30from . import utils
32################################################################################
35class Logger:
36 "Logger object"
37 __shared_state = {}
39 def __init__(self, program='unknown', debug=False, print_starting=True, include_pid=False):
40 self.__dict__ = self.__shared_state
42 self.program = program
43 self.debug = debug
44 self.include_pid = include_pid
46 if not getattr(self, 'logfile', None):
47 self._open_log(debug)
49 if print_starting:
50 self.log(["program start"])
52 def _open_log(self, debug) -> None:
53 # Create the log directory if it doesn't exist
54 from daklib.config import Config
55 logdir = Config()["Dir::Log"]
56 if not os.path.exists(logdir):
57 umask = os.umask(00000)
58 os.makedirs(logdir, 0o2775)
59 os.umask(umask)
61 # Open the logfile
62 logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
63 logfile = None
65 if debug: 65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true
66 logfile = sys.stderr
67 else:
68 umask = os.umask(0o0002)
69 logfile = open(logfilename, 'a')
70 os.umask(umask)
72 self.logfile = logfile
74 def log(self, details: list[str]) -> None:
75 "Log an event"
76 # Prepend timestamp, program name, and user name
77 details.insert(0, utils.getusername())
78 details.insert(0, self.program)
79 timestamp = time.strftime("%Y%m%d%H%M%S")
80 details.insert(0, timestamp)
81 # Force the contents of the list to be string.join-able
82 details = [str(i) for i in details]
83 fcntl.lockf(self.logfile, fcntl.LOCK_EX)
84 # Write out the log in TSV
85 self.logfile.write("|".join(details) + '\n')
86 # Flush the output to enable tail-ing
87 self.logfile.flush()
88 fcntl.lockf(self.logfile, fcntl.LOCK_UN)
90 def log_traceback(self, info, ex) -> None:
91 "Log an exception with a traceback"
92 self.log([info, repr(ex)])
93 for line in traceback.format_exc().split('\n')[:-1]:
94 self.log(['traceback', line])
96 def close(self) -> None:
97 "Close a Logger object"
98 self.log(["program end"])
99 self.logfile.close()