changeset 1129:08f50fdac21b

core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
author Goffi <goffi@goffi.org>
date Mon, 25 Aug 2014 17:21:03 +0200
parents c7fe4fe66fbc
children adea30ca0b51
files src/core/log.py src/core/log_config.py
diffstat 2 files changed, 11 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/log.py	Mon Aug 25 17:21:03 2014 +0200
+++ b/src/core/log.py	Mon Aug 25 17:21:03 2014 +0200
@@ -155,7 +155,7 @@
 class ConfigureBase(object):
     LOGGER_CLASS = Logger
 
-    def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
+    def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=None):
         """Configure a backend
 
         @param level: one of C.LOG_LEVELS
@@ -168,6 +168,7 @@
         @param colors: if True use ANSI colors to show log levels
         @param force_colors: if True ANSI colors are used even if stdout is not a tty
         """
+        self.backend_data = backend_data
         self.preTreatment()
         self.configureLevel(level)
         self.configureFormat(fmt)
--- a/src/core/log_config.py	Mon Aug 25 17:21:03 2014 +0200
+++ b/src/core/log_config.py	Mon Aug 25 17:21:03 2014 +0200
@@ -188,17 +188,11 @@
 
         if C.LOG_OPT_OUTPUT_DEFAULT in log.handlers:
             # default output is already managed, we just add output to stdout if we are in debug or nodaemon mode
-
-            def postOptions(options): # called after options are parsed
-                if options.get('nodaemon', False) or options.get('debug', False):
-                    addObserver(self.twisted_log.FileLogObserver(sys.stdout).emit)
-
-            # XXX: this hack is to know if we are in debug or nodaemon mode
-            #      any better method welcomed
-            from twisted.scripts import twistd
-            # we monkey patch ServerOptions to be sure options.get is done after options are fully parsed
-            # XXX: only works with twistd plugins, not with .tac files !
-            twistd.ServerOptions.postOptions = postOptions
+            if self.backend_data is None:
+                raise ValueError("You must pass options as backend_data with Twisted backend")
+            options = self.backend_data
+            if options.get('nodaemon', False) or options.get('debug', False):
+                addObserver(self.twisted_log.FileLogObserver(sys.stdout).emit)
         else:
             # \\default is not in the output, so we remove current observers
             self.cleared_observers = self.log_publisher.observers
@@ -241,12 +235,12 @@
 
 class ConfigureStandard(ConfigureBasic):
 
-    def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
+    def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=None):
         if fmt is None:
             fmt = C.LOG_OPT_FORMAT[1]
         if output is None:
             output = C.LOG_OPT_OUTPUT[1]
-        super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors)
+        super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors, backend_data)
 
     def preTreatment(self):
         """We use logging methods directly, instead of using Logger"""
@@ -380,7 +374,7 @@
             level = C.LOG_LVL_INFO
         options[LEVEL] = level
 
-def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None):
+def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None, backend_data=None):
     """Configure logging system for SàT, can be used by frontends
 
     logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
@@ -409,4 +403,4 @@
                 log_conf[opt_name] = opt_default
 
     _parseOptions(log_conf)
-    configure(backend, **log_conf)
+    configure(backend, backend_data=backend_data, **log_conf)