Ticket #638: twistd.patch

File twistd.patch, 3.7 KB (added by harshaw, 10 years ago)

twistd patch

  • twistd.py

     
    88from twisted.application import app, service
    99from twisted.scripts import mktap
    1010from twisted import copyright
     11from twisted.plugin import getPlugins
    1112
    1213import os, errno, sys
    1314
     
    2526                 "after binding ports, retaining the option to regain "
    2627                 "privileges in cases such as spawning processes. "
    2728                 "Use with caution.)"],
     29                ['listlogobservers',None,
     30                 "List the available log observers on your system"
     31                ]
    2832               ]
    2933
    3034    optParameters = [
     
    3640                      'Chroot to a supplied directory before running'],
    3741                     ['uid', 'u', None, "The uid to run as."],
    3842                     ['gid', 'g', None, "The gid to run as."],
     43                     ['logobserver',None,None,
     44                      "use a custom logobserver plugin."
     45                     ]
    3946                    ]
    4047    zsh_altArgDescr = {"prefix":"Use the given prefix when syslogging (default: twisted)",
    4148                       "pidfile":"Name of the pidfile (default: twistd.pid)",}
     
    98105        log.msg("Failed to unlink PID file:")
    99106        log.deferr()
    100107
    101 def startLogging(logfilename, sysLog, prefix, nodaemon):
     108def getCustomLogger(logObserver):
     109    """
     110    lookup a custom log plugin and match against the name of the plugin class.
     111    """
     112    for logger in getPlugins(log.ILogObserver):
     113        if logger.__class__.__name__ == logObserver:
     114            return logger
     115    print 'log plugin not found.  did you try --listlogobservers?'
     116    sys.exit()
     117   
     118
     119def startLogging(logfilename, logObserver,sysLog, prefix, nodaemon):
    102120    if logfilename == '-':
    103121        if not nodaemon:
    104122            print 'daemons cannot log to stdout'
    105123            os._exit(1)
    106124        logFile = sys.stdout
    107125    elif sysLog:
    108         syslog.startLogging(prefix)
     126        if logObserver:
     127            print 'logobserver plugin can not be used with syslog'
     128            sys.exit()
     129        else:
     130            syslog.startLogging(prefix)
    109131    elif nodaemon and not logfilename:
    110132        logFile = sys.stdout
    111133    else:
     
    121143            signal.signal(signal.SIGUSR1, rotateLog)
    122144       
    123145    if not sysLog:
    124         log.startLogging(logFile)
     146        if logObserver:
     147            customlogger = getCustomLogger(logObserver)
     148            customlogger.setFile(logFile)
     149            log.startLoggingWithObserver(customlogger.emit,logFile)
     150        else:
     151            log.startLogging(logFile)
    125152    sys.stdout.flush()
    126153
    127154
     
    181208    shedPrivileges(config['euid'], uid, gid)
    182209    app.startApplication(application, not config['no_save'])
    183210
     211def checkLogObservers(listobs):
     212    if not listobs:
     213        return
     214    print 'your system has the following log file plugins installed: '
     215    any = False
     216    for logobs in getPlugins(log.ILogObserver):
     217        any = True
     218        print '%s:%s' % (logobs.__class__.__name__,repr(logobs))
     219    if not any:
     220        print 'No log plugins found.'
     221    sys.exit()
    184222
    185223def runApp(config):
    186224    checkPID(config['pidfile'])
     225    checkLogObservers(config['listlogobservers'])
    187226    passphrase = app.getPassphrase(config['encrypted'])
    188227    app.installReactor(config['reactor'])
    189228    config['nodaemon'] = config['nodaemon'] or config['debug']
    190229    oldstdout = sys.stdout
    191230    oldstderr = sys.stderr
    192     startLogging(config['logfile'], config['syslog'], config['prefix'],
     231    startLogging(config['logfile'], config['logobserver'],config['syslog'], config['prefix'],
    193232                 config['nodaemon'])
    194233    app.initialLog()
    195234    application = app.getApplication(config, passphrase)