daklog.py 2.7 KB
Newer Older
1 2
#!/usr/bin/env python

J
various  
Joerg Jaspert 已提交
3 4 5 6 7 8 9
"""
Logging functions

@contact: Debian FTP Master <ftpmaster@debian.org>
@copyright: 2001, 2002, 2006  James Troup <james@nocrew.org>
@license: GNU General Public License version 2 or later
"""
J
James Troup 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
J
James Troup 已提交
24

J
James Troup 已提交
25 26
################################################################################

J
various  
Joerg Jaspert 已提交
27 28 29 30
import os
import pwd
import time
import sys
31
import utils
J
James Troup 已提交
32 33 34 35 36

################################################################################

class Logger:
    "Logger object"
37 38 39
    Cnf = None
    logfile = None
    program = None
J
James Troup 已提交
40

41
    def __init__ (self, Cnf, program, debug=0, print_starting=True):
J
James Troup 已提交
42
        "Initialize a new Logger object"
43 44
        self.Cnf = Cnf
        self.program = program
J
James Troup 已提交
45
        # Create the log directory if it doesn't exist
46
        logdir = Cnf["Dir::Log"]
J
James Troup 已提交
47
        if not os.path.exists(logdir):
48 49
            umask = os.umask(00000)
            os.makedirs(logdir, 02775)
50
            os.umask(umask)
J
James Troup 已提交
51
        # Open the logfile
52
        logfilename = "%s/%s" % (logdir, time.strftime("%Y-%m"))
53 54 55 56 57 58 59
        logfile = None
        if debug:
            logfile = sys.stderr
        else:
            umask = os.umask(00002)
            logfile = utils.open_file(logfilename, 'a')
            os.umask(umask)
60
        self.logfile = logfile
61 62
        if print_starting:
            self.log(["program start"])
J
James Troup 已提交
63 64 65

    def log (self, details):
        "Log an event"
66 67
        # Prepend timestamp, program name, and user name
        details.insert(0, utils.getusername())
68 69 70
        details.insert(0, self.program)
        timestamp = time.strftime("%Y%m%d%H%M%S")
        details.insert(0, timestamp)
J
James Troup 已提交
71
        # Force the contents of the list to be string.join-able
72
        details = [ str(i) for i in details ]
J
James Troup 已提交
73
        # Write out the log in TSV
74
        self.logfile.write("|".join(details)+'\n')
J
James Troup 已提交
75
        # Flush the output to enable tail-ing
76
        self.logfile.flush()
J
James Troup 已提交
77 78 79

    def close (self):
        "Close a Logger object"
80 81 82
        self.log(["program end"])
        self.logfile.flush()
        self.logfile.close()