]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-deploy.git/commitdiff
make sure we can support color for logging 157/head
authorAlfredo Deza <alfredo.deza@inktank.com>
Wed, 29 Jan 2014 21:24:43 +0000 (16:24 -0500)
committerAlfredo Deza <alfredo.deza@inktank.com>
Wed, 29 Jan 2014 21:24:43 +0000 (16:24 -0500)
Signed-off-by: Alfredo Deza <alfredo.deza@inktank.com>
ceph_deploy/util/log.py

index 3b4e8ad54503e174dd565e5ed18ffa90e681e914..2a236befdf85dbc99a4455d3110e144edeee4bc8 100644 (file)
@@ -1,4 +1,5 @@
 import logging
+import sys
 
 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
 
@@ -18,6 +19,19 @@ BASE_COLOR_FORMAT = "[$BOLD%(name)s$RESET][%(color_levelname)-17s] %(message)s"
 BASE_FORMAT = "%(asctime)s [%(name)s][%(levelname)-6s] %(message)s"
 
 
+def supports_color():
+    """
+    Returns True if the running system's terminal supports color, and False
+    otherwise.
+    """
+    unsupported_platform = (sys.platform in ('win32', 'Pocket PC'))
+    # isatty is not always implemented, #6223.
+    is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
+    if unsupported_platform or not is_a_tty:
+        return False
+    return True
+
+
 def color_message(message):
     message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ)
     return message
@@ -36,9 +50,11 @@ class ColoredFormatter(logging.Formatter):
     def format(self, record):
         levelname = record.levelname
         truncated_level = record.levelname[:6]
-        if levelname in COLORS:
+        if levelname in COLORS and supports_color():
             levelname_color = COLOR_SEQ % (30 + COLORS[levelname]) + truncated_level + RESET_SEQ
             record.color_levelname = levelname_color
+        else:
+            record.color_levelname = levelname
         return logging.Formatter.format(self, record)