From ec2c9312c36782c61b38e1c3bcdbe932685a9794 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 8 Apr 2017 23:59:17 +0100 Subject: [PATCH 1/1] hippotatd: support syslog Signed-off-by: Ian Jackson --- hippotatd | 34 ++++++++++++++++++++++++++++++++++ hippotatlib/__init__.py | 5 +++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/hippotatd b/hippotatd index 0ef6369..525e4a1 100755 --- a/hippotatd +++ b/hippotatd @@ -37,6 +37,7 @@ import twisted.internet from twisted.web.server import NOT_DONE_YET import twisted.web.static +import twisted.python.syslog import hippotatlib.ownsource from hippotatlib.ownsource import SourceShipmentPreparer @@ -341,6 +342,32 @@ def catch_termination(): for sig in (signal.SIGINT, signal.SIGTERM): signal.signal(sig, partial(signal_handler, sig.name)) +def daemonise(): + global syslogfacility + if opts.daemon and opts.syslogfacility is None: + opts.syslogfacility = 'daemon' + + if opts.syslogfacility is not None: + facilnum = syslog.__dict__['LOG_' + opts.syslogfacility.upper()] + syslog.openlog('hippotatd', + facility=facilnum, + logoption=syslog.LOG_PID) + def emit(event): + m = twisted.logger.formatEvent(event) + #print(repr(event), m, file=org_stderr) + level = event.get('log_level') + if event.get('dflag',None) is not None: sl = syslog.LOG_DEBUG + elif level == LogLevel.critical : sl = syslog.LOG_CRIT + elif level == LogLevel.error : sl = syslog.LOG_ERR + elif level == LogLevel.warn : sl = syslog.LOG_WARNING + else : sl = syslog.LOG_INFO + syslog.syslog(sl,m) + glp = twisted.logger.globalLogPublisher + glp.addObserver(emit) + log_debug(DBG.INIT, 'starting to log to syslog') + + glp.removeObserver(hippotatlib.file_log_observer) + optparser.add_option('--ownsource', default=2, action='store_const', dest='ownsource', const=2, help='source download fully enabled (default)') @@ -353,8 +380,15 @@ optparser.add_option('--no-ownsource', action='store_const', dest='ownsource', const=0, help='source download disabled (for testing only)') +optparser.add_option('--syslog-facility', + nargs=1, type='string',action='store', + metavar='FACILITY', dest='syslogfacility', + default=None, + help='log to syslog, with specified facility') + common_startup(process_cfg) catch_termination() ipif = start_ipif(c.ipif_command, (lambda p,s,d: route(p,"[ipif]",s,d))) start_http() +daemonise() common_run() diff --git a/hippotatlib/__init__.py b/hippotatlib/__init__.py index 701a5f2..7bdf4ea 100644 --- a/hippotatlib/__init__.py +++ b/hippotatlib/__init__.py @@ -650,12 +650,13 @@ just `+': all DFLAGs. stdsomething_obs = twisted.logger.FilteringLogObserver( stderr_obs, [pred], stdout_obs ) - log_observer = twisted.logger.FilteringLogObserver( + global file_log_observer + file_log_observer = twisted.logger.FilteringLogObserver( stdsomething_obs, [LogNotBoringTwisted()] ) #log_observer = stdsomething_obs twisted.logger.globalLogBeginner.beginLoggingTo( - [ log_observer, crash_on_critical ] + [ file_log_observer, crash_on_critical ] ) def common_run(): -- 2.30.2