diff src/core/log.py @ 1010:73a0b7f94674

primitivus: use of new logging system: - default output is \\memory - logs can be seen with :messages command - now useless writeLog method has been removed
author Goffi <goffi@goffi.org>
date Mon, 05 May 2014 20:12:19 +0200
parents d70d4fe5c5f8
children c8771279497e
line wrap: on
line diff
--- a/src/core/log.py	Mon May 05 18:58:34 2014 +0200
+++ b/src/core/log.py	Mon May 05 20:12:19 2014 +0200
@@ -169,6 +169,18 @@
         log_record.name = dict_record['name']
         return self.filter(log_record) == 1
 
+def memoryGet(size=None):
+    """Return buffered logs
+
+    @param size: number of logs to return
+    """
+    if not C.LOG_OPT_OUTPUT_MEMORY in _handlers:
+        raise ValueError('memory output is not used')
+    if _backend == C.LOG_BACKEND_STANDARD:
+        mem_handler = _handlers[C.LOG_OPT_OUTPUT_MEMORY]
+        return (log_msg for log_msg in mem_handler.buffer[size if size is None else -size:])
+    else:
+        raise NotImplementedError
 
 def _getProfile():
     """Try to find profile value using introspection"""
@@ -509,7 +521,11 @@
 
 class ConfigureStandard(Configure):
 
-    def __init__(self, level=None, fmt=C.LOG_OPT_FORMAT[1], output=C.LOG_OPT_OUTPUT[1], logger=None, colors=False, force_colors=False):
+    def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
+        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)
 
     def preTreatment(self):
@@ -559,7 +575,7 @@
 
     def configureColors(self, colors, force_colors):
         import sys
-        self.with_color = colors & (sys.stdout.isatty() or force_colors)
+        self.formatter.with_color = colors & (sys.stdout.isatty() or force_colors)
         if not colors and force_colors:
             raise ValueError("force_colors can't be used if colors is False")
 
@@ -579,8 +595,11 @@
                     hdlr = logging.StreamHandler()
                     self._addHandler(root_logger, hdlr)
                 elif handler == C.LOG_OPT_OUTPUT_MEMORY:
-                    import logging.handlers
-                    hdlr = logging.handlers.BufferingHandler(options)
+                    from logging.handlers import BufferingHandler
+                    class SatMemoryHandler(BufferingHandler):
+                        def emit(self, record):
+                            super(SatMemoryHandler, self).emit(self.format(record))
+                    hdlr = SatMemoryHandler(options)
                     _handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later
                     self._addHandler(root_logger, hdlr)
                 elif handler == C.LOG_OPT_OUTPUT_FILE:
@@ -640,23 +659,31 @@
             level = C.LOG_LVL_INFO
         options[LEVEL] = level
 
-def satConfigure(backend=C.LOG_BACKEND_TWISTED):
+def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=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
+    @param backend: backend to use, it can be:
+        - C.LOG_BACKEND_BASIC: print based backend
+        - C.LOG_BACKEND_TWISTED: Twisted logging backend
+        - C.LOG_BACKEND_STANDARD: standard logging backend
+    @param const: Const class to use instead of sat.core.constants.Const (mainly used to change default values)
     """
+    if const is not None:
+        global C
+        C = const
     import ConfigParser
     import os
     log_conf = {}
     config = ConfigParser.SafeConfigParser()
     config.read(C.CONFIG_FILES)
-    for opt_name, opt_default in C.LOG_OPTIONS:
+    for opt_name, opt_default in C.LOG_OPTIONS():
         try:
             log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))]
         except KeyError:
             try:
-                log_conf[opt_name] = config.get('DEFAULT', C.LOG_OPT_PREFIX + opt_name)
-            except ConfigParser.NoOptionError:
+                log_conf[opt_name] = config.get(C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name)
+            except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
                 log_conf[opt_name] = opt_default
 
     _parseOptions(log_conf)