ia64/xen-unstable

changeset 11150:939f75570a15

Set close-on-exec flag on /var/log/xend.log logging file.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
author kfraser@localhost.localdomain
date Tue Aug 15 16:27:26 2006 +0100 (2006-08-15)
parents 5db6984e4503
children e65bb0de33cd
files tools/python/xen/xend/XendLogging.py
line diff
     1.1 --- a/tools/python/xen/xend/XendLogging.py	Tue Aug 15 16:26:39 2006 +0100
     1.2 +++ b/tools/python/xen/xend/XendLogging.py	Tue Aug 15 16:27:26 2006 +0100
     1.3 @@ -21,6 +21,7 @@ import tempfile
     1.4  import types
     1.5  import logging
     1.6  import logging.handlers
     1.7 +import fcntl
     1.8  
     1.9  from xen.xend.server import params
    1.10  
    1.11 @@ -49,6 +50,27 @@ DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
    1.12  
    1.13  logfilename = None
    1.14  
    1.15 +class XendRotatingFileHandler(logging.handlers.RotatingFileHandler):
    1.16 +
    1.17 +    def __init__(self, fname, mode, maxBytes, backupCount):
    1.18 +        logging.handlers.RotatingFileHandler.__init__(self, fname, mode, maxBytes, backupCount)
    1.19 +        self.setCloseOnExec()
    1.20 +
    1.21 +    def doRollover(self):
    1.22 +        logging.handlers.RotatingFileHandler.doRollover()
    1.23 +        self.setCloseOnExec()
    1.24 +
    1.25 +    # NB yes accessing 'self.stream' violates OO encapsulation somewhat,
    1.26 +    # but python logging API gives no other way to access the file handle
    1.27 +    # and the entire python logging stack is already full of OO encapsulation
    1.28 +    # violations. The other alternative is copy-and-paste duplicating the
    1.29 +    # entire FileHandler, StreamHandler & RotatingFileHandler classes which
    1.30 +    # is even worse
    1.31 +    def setCloseOnExec(self):
    1.32 +        flags = fcntl.fcntl(self.stream.fileno(), fcntl.F_GETFD)
    1.33 +        flags |= fcntl.FD_CLOEXEC
    1.34 +        fcntl.fcntl(self.stream.fileno(), fcntl.F_SETFD, flags)
    1.35 +        
    1.36  
    1.37  def init(filename, level):
    1.38      """Initialise logging.  Logs to the given filename, and logs to stderr if
    1.39 @@ -58,9 +80,9 @@ def init(filename, level):
    1.40      global logfilename
    1.41  
    1.42      def openFileHandler(fname):
    1.43 -        return logging.handlers.RotatingFileHandler(fname, mode = 'a',
    1.44 -                                                    maxBytes = MAX_BYTES,
    1.45 -                                                    backupCount = BACKUP_COUNT)
    1.46 +        return XendRotatingFileHandler(fname, mode = 'a',
    1.47 +                                       maxBytes = MAX_BYTES,
    1.48 +                                       backupCount = BACKUP_COUNT)
    1.49  
    1.50      # Rather unintuitively, getLevelName will get the number corresponding to
    1.51      # a level name, as well as getting the name corresponding to a level