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 sys
28import time
29import traceback
31from . import utils
33################################################################################
36class Logger:
37 "Logger object"
39 __shared_state = {}
41 def __init__(
42 self, program="unknown", debug=False, print_starting=True, include_pid=False
43 ):
44 self.__dict__ = self.__shared_state
46 self.program = program
47 self.debug = debug
48 self.include_pid = include_pid
50 if not getattr(self, "logfile", None):
51 self._open_log(debug)
53 if print_starting:
54 self.log(["program start"])
56 def _open_log(self, debug) -> None:
57 # Create the log directory if it doesn't exist
58 from daklib.config import Config
60 logdir = Config()["Dir::Log"]
61 if not os.path.exists(logdir):
62 umask = os.umask(00000)
63 os.makedirs(logdir, 0o2775)
64 os.umask(umask)
66 # Open the logfile
67 logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
68 logfile = None
70 if debug: 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never true
71 logfile = sys.stderr
72 else:
73 umask = os.umask(0o0002)
74 logfile = open(logfilename, "a")
75 os.umask(umask)
77 self.logfile = logfile
79 def log(self, details: list[str]) -> None:
80 "Log an event"
81 # Prepend timestamp, program name, and user name
82 details.insert(0, utils.getusername())
83 details.insert(0, self.program)
84 timestamp = time.strftime("%Y%m%d%H%M%S")
85 details.insert(0, timestamp)
86 # Force the contents of the list to be string.join-able
87 details = [str(i) for i in details]
88 fcntl.lockf(self.logfile, fcntl.LOCK_EX)
89 # Write out the log in TSV
90 self.logfile.write("|".join(details) + "\n")
91 # Flush the output to enable tail-ing
92 self.logfile.flush()
93 fcntl.lockf(self.logfile, fcntl.LOCK_UN)
95 def log_traceback(self, info, ex) -> None:
96 "Log an exception with a traceback"
97 self.log([info, repr(ex)])
98 for line in traceback.format_exc().split("\n")[:-1]:
99 self.log(["traceback", line])
101 def close(self) -> None:
102 "Close a Logger object"
103 self.log(["program end"])
104 self.logfile.close()