chiark / gitweb /
hippotatd: support syslog
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 8 Apr 2017 22:59:17 +0000 (23:59 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 8 Apr 2017 23:00:39 +0000 (00:00 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
hippotatd
hippotatlib/__init__.py

index 0ef6369..525e4a1 100755 (executable)
--- 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()
index 701a5f2..7bdf4ea 100644 (file)
@@ -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():