Skip to content

Commit

Permalink
Merge pull request #2019 from bdarnell/colorama-fix
Browse files Browse the repository at this point in the history
log: Fix color logging detection
  • Loading branch information
bdarnell committed Apr 20, 2017
2 parents 0f31bd6 + 57680da commit ccf2e1e
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions tornado/log.py
Expand Up @@ -54,18 +54,21 @@


def _stderr_supports_color():
color = False
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
if curses:
try:
try:
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
if curses:
curses.setupterm()
if curses.tigetnum("colors") > 0:
color = True
except Exception:
pass
elif colorama:
color = True
return color
return True
elif colorama:
if sys.stderr is getattr(colorama.initialise, 'wrapped_stderr',
object()):
return True
except Exception:
# Very broad exception handling because it's always better to
# fall back to non-colored logs than to break at startup.
pass
return False


def _safe_unicode(s):
Expand Down Expand Up @@ -146,12 +149,12 @@ def __init__(self, fmt=DEFAULT_FORMAT, datefmt=DEFAULT_DATE_FORMAT,
for levelno, code in colors.items():
self._colors[levelno] = unicode_type(curses.tparm(fg_color, code), "ascii")
self._normal = unicode_type(curses.tigetstr("sgr0"), "ascii")
elif sys.stderr is getattr(colorama, 'wrapped_stderr', object()):
else:
# If curses is not present (currently we'll only get here for
# colorama on windows), assume hard-coded ANSI color codes.
for levelno, code in colors.items():
self._colors[levelno] = '\033[2;3%dm' % code
self._normal = '\033[0m'
else:
raise RuntimeError("No supported color terminal library")
else:
self._normal = ''

Expand Down

0 comments on commit ccf2e1e

Please sign in to comment.