Ticket #638: twistd.2.patch
| File twistd.2.patch, 3.7 KB (added by harshaw, 7 years ago) |
|---|
-
twistd.py
8 8 from twisted.application import app, service 9 9 from twisted.scripts import mktap 10 10 from twisted import copyright 11 from twisted.plugin import getPlugins 11 12 12 13 import os, errno, sys 13 14 … … 25 26 "after binding ports, retaining the option to regain " 26 27 "privileges in cases such as spawning processes. " 27 28 "Use with caution.)"], 29 ['listlogobservers',None, 30 "List the available log observers on your system" 31 ] 28 32 ] 29 33 30 34 optParameters = [ … … 36 40 'Chroot to a supplied directory before running'], 37 41 ['uid', 'u', None, "The uid to run as."], 38 42 ['gid', 'g', None, "The gid to run as."], 43 ['logobserver',None,None, 44 "use a custom logobserver plugin." 45 ] 39 46 ] 40 47 zsh_altArgDescr = {"prefix":"Use the given prefix when syslogging (default: twisted)", 41 48 "pidfile":"Name of the pidfile (default: twistd.pid)",} … … 98 105 log.msg("Failed to unlink PID file:") 99 106 log.deferr() 100 107 101 def startLogging(logfilename, sysLog, prefix, nodaemon): 108 def 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 119 def startLogging(logfilename, logObserver,sysLog, prefix, nodaemon): 102 120 if logfilename == '-': 103 121 if not nodaemon: 104 122 print 'daemons cannot log to stdout' 105 123 os._exit(1) 106 124 logFile = sys.stdout 107 125 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) 109 131 elif nodaemon and not logfilename: 110 132 logFile = sys.stdout 111 133 else: … … 121 143 signal.signal(signal.SIGUSR1, rotateLog) 122 144 123 145 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) 125 152 sys.stdout.flush() 126 153 127 154 … … 181 208 shedPrivileges(config['euid'], uid, gid) 182 209 app.startApplication(application, not config['no_save']) 183 210 211 def 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() 184 222 185 223 def runApp(config): 186 224 checkPID(config['pidfile']) 225 checkLogObservers(config['listlogobservers']) 187 226 passphrase = app.getPassphrase(config['encrypted']) 188 227 app.installReactor(config['reactor']) 189 228 config['nodaemon'] = config['nodaemon'] or config['debug'] 190 229 oldstdout = sys.stdout 191 230 oldstderr = sys.stderr 192 startLogging(config['logfile'], config[' syslog'], config['prefix'],231 startLogging(config['logfile'], config['logobserver'],config['syslog'], config['prefix'], 193 232 config['nodaemon']) 194 233 app.initialLog() 195 234 application = app.getApplication(config, passphrase)
