1
from __future__ import absolute_import
5
import logging.handlers
11
import dummy_threading as threading
13
from pip.compat import WINDOWS
14
from pip.utils import ensure_dir
17
from pip._vendor import colorama
18
# Lots of different errors can come from this, including SystemError and
24
_log_state = threading.local()
25
_log_state.indentation = 0
28
@contextlib.contextmanager
29
def indent_log(num=2):
31
A context manager which will cause the log output to be indented for any
32
log messages emitted inside it.
34
_log_state.indentation += num
38
_log_state.indentation -= num
41
def get_indentation():
42
return getattr(_log_state, 'indentation', 0)
45
class IndentingFormatter(logging.Formatter):
47
def format(self, record):
49
Calls the standard formatter, but will indent all of the log messages
50
by our current indentation level.
52
formatted = logging.Formatter.format(self, record)
54
(" " * get_indentation()) + line
55
for line in formatted.splitlines(True)
60
def _color_wrap(*colors):
62
return "".join(list(colors) + [inp, colorama.Style.RESET_ALL])
66
class ColorizedStreamHandler(logging.StreamHandler):
68
# Don't build up a list of colors if we don't have colorama
71
# This needs to be in order from highest logging level to lowest.
72
(logging.ERROR, _color_wrap(colorama.Fore.RED)),
73
(logging.WARNING, _color_wrap(colorama.Fore.YELLOW)),
78
def __init__(self, stream=None):
79
logging.StreamHandler.__init__(self, stream)
81
if WINDOWS and colorama:
82
self.stream = colorama.AnsiToWin32(self.stream)
84
def should_color(self):
85
# Don't colorize things if we do not have colorama
90
self.stream if not isinstance(self.stream, colorama.AnsiToWin32)
91
else self.stream.wrapped
94
# If the stream is a tty we should color it
95
if hasattr(real_stream, "isatty") and real_stream.isatty():
98
# If we have an ASNI term we should color it
99
if os.environ.get("TERM") == "ANSI":
102
# If anything else we should not color it
105
def format(self, record):
106
msg = logging.StreamHandler.format(self, record)
108
if self.should_color():
109
for level, color in self.COLORS:
110
if record.levelno >= level:
117
class BetterRotatingFileHandler(logging.handlers.RotatingFileHandler):
120
ensure_dir(os.path.dirname(self.baseFilename))
121
return logging.handlers.RotatingFileHandler._open(self)
124
class MaxLevelFilter(logging.Filter):
126
def __init__(self, level):
129
def filter(self, record):
130
return record.levelno < self.level