diff src/core/log.py @ 1942:7f053e1f0b67

core (logs): taints: log taints can be specified using log_levels_taints_dict in sat.conf, where key is a level name, and value is a list of color or code names. For instance, the following conf would taint INFO level in bold/green and DEBUG in default color: log_levels_taints_dict = {"debug": [], "info": ["green", "bold"]} Unknown names are copied as raw strings.
author Goffi <goffi@goffi.org>
date Mon, 18 Apr 2016 18:35:17 +0200
parents 3fdacba9da68
children 7cbffd754b4a
line wrap: on
line diff
--- a/src/core/log.py	Mon Apr 18 18:33:59 2016 +0200
+++ b/src/core/log.py	Mon Apr 18 18:35:17 2016 +0200
@@ -159,7 +159,7 @@
     LOGGER_CLASS = Logger
     _color_location = False # True if color location is specified in fmt (with COLOR_START)
 
-    def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=None):
+    def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, levels_taints_dict=None, force_colors=False, backend_data=None):
         """Configure a backend
 
         @param level: one of C.LOG_LEVELS
@@ -178,7 +178,7 @@
         self.configureFormat(fmt)
         self.configureOutput(output)
         self.configureLogger(logger)
-        self.configureColors(colors, force_colors)
+        self.configureColors(colors, force_colors, levels_taints_dict)
         self.postTreatment()
         self.updateCurrentLogger()
 
@@ -221,8 +221,29 @@
         if logger:
             Logger.filter_name = FilterName(logger)
 
-    def configureColors(self, colors, force_colors):
-        pass
+    def configureColors(self, colors, force_colors, levels_taints_dict):
+        if colors:
+            # if color are used, we need to handle levels_taints_dict
+            for level in levels_taints_dict.keys():
+                # we wants levels in uppercase to correspond to contstants
+                levels_taints_dict[level.upper()] = levels_taints_dict[level]
+            taints = self.__class__.taints = {}
+            for level in C.LOG_LEVELS:
+                # we want use values and use constant value as default
+                taint_list = levels_taints_dict.get(level, C.LOG_OPT_TAINTS_DICT[1][level])
+                ansi_list = []
+                for elt in taint_list:
+                    elt = elt.upper()
+                    try:
+                        ansi = getattr(C, 'ANSI_FG_{}'.format(elt))
+                    except AttributeError:
+                        try:
+                            ansi = getattr(C, 'ANSI_{}'.format(elt))
+                        except AttributeError:
+                            # we use raw string if element is unknown
+                            ansi = elt
+                    ansi_list.append(ansi)
+                taints[level] = ''.join(ansi_list)
 
     def postTreatment(self):
         pass
@@ -289,29 +310,17 @@
         @param message: formatted message to log
         @return: message with ANSI escape codes for coloration
         """
-        if level == C.LOG_LVL_DEBUG:
-            start = (C.ANSI_FG_CYAN,)
-        elif level == C.LOG_LVL_INFO:
-            start = ()
-        elif level == C.LOG_LVL_WARNING:
-            start = (C.ANSI_FG_YELLOW,)
-        elif level == C.LOG_LVL_ERROR:
-            start = (C.ANSI_FG_RED,
-                   C.ANSI_BLINK,
-                   r'/!\ ',
-                   C.ANSI_BLINK_OFF)
-        elif level == C.LOG_LVL_CRITICAL:
-            start = (C.ANSI_BOLD,
-                   C.ANSI_FG_RED,
-                   'Guru Meditation ',
-                   C.ANSI_NORMAL_WEIGHT)
-        else:
-            start = ()
+
+        try:
+            start = cls.taints[level]
+        except KeyError:
+            start = ''
+
         if cls._color_location:
-            return message % {'color_start': ''.join(start),
+            return message % {'color_start': start,
                               'color_end': C.ANSI_RESET}
         else:
-            return '%s%s%s' % (''.join(start), message, C.ANSI_RESET)
+            return '%s%s%s' % (start, message, C.ANSI_RESET)
 
     @staticmethod
     def getProfile():