~ibmcharmers/charms/trusty/ibm-dsm-enterprise/devel

« back to all changes in this revision

Viewing changes to .tox/py35/lib/python3.5/site-packages/pip/utils/logging.py

  • Committer: anita nayak
  • Date: 2016-12-08 14:10:42 UTC
  • Revision ID: anitanayak@in.ibm.com-20161208141042-jyicg7udd6liy6s3
Initial Check in for IBM DSM for trusty

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import absolute_import
 
2
 
 
3
import contextlib
 
4
import logging
 
5
import logging.handlers
 
6
import os
 
7
 
 
8
try:
 
9
    import threading
 
10
except ImportError:
 
11
    import dummy_threading as threading
 
12
 
 
13
from pip.compat import WINDOWS
 
14
from pip.utils import ensure_dir
 
15
 
 
16
try:
 
17
    from pip._vendor import colorama
 
18
# Lots of different errors can come from this, including SystemError and
 
19
# ImportError.
 
20
except Exception:
 
21
    colorama = None
 
22
 
 
23
 
 
24
_log_state = threading.local()
 
25
_log_state.indentation = 0
 
26
 
 
27
 
 
28
@contextlib.contextmanager
 
29
def indent_log(num=2):
 
30
    """
 
31
    A context manager which will cause the log output to be indented for any
 
32
    log messages emitted inside it.
 
33
    """
 
34
    _log_state.indentation += num
 
35
    try:
 
36
        yield
 
37
    finally:
 
38
        _log_state.indentation -= num
 
39
 
 
40
 
 
41
def get_indentation():
 
42
    return getattr(_log_state, 'indentation', 0)
 
43
 
 
44
 
 
45
class IndentingFormatter(logging.Formatter):
 
46
 
 
47
    def format(self, record):
 
48
        """
 
49
        Calls the standard formatter, but will indent all of the log messages
 
50
        by our current indentation level.
 
51
        """
 
52
        formatted = logging.Formatter.format(self, record)
 
53
        formatted = "".join([
 
54
            (" " * get_indentation()) + line
 
55
            for line in formatted.splitlines(True)
 
56
        ])
 
57
        return formatted
 
58
 
 
59
 
 
60
def _color_wrap(*colors):
 
61
    def wrapped(inp):
 
62
        return "".join(list(colors) + [inp, colorama.Style.RESET_ALL])
 
63
    return wrapped
 
64
 
 
65
 
 
66
class ColorizedStreamHandler(logging.StreamHandler):
 
67
 
 
68
    # Don't build up a list of colors if we don't have colorama
 
69
    if colorama:
 
70
        COLORS = [
 
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)),
 
74
        ]
 
75
    else:
 
76
        COLORS = []
 
77
 
 
78
    def __init__(self, stream=None):
 
79
        logging.StreamHandler.__init__(self, stream)
 
80
 
 
81
        if WINDOWS and colorama:
 
82
            self.stream = colorama.AnsiToWin32(self.stream)
 
83
 
 
84
    def should_color(self):
 
85
        # Don't colorize things if we do not have colorama
 
86
        if not colorama:
 
87
            return False
 
88
 
 
89
        real_stream = (
 
90
            self.stream if not isinstance(self.stream, colorama.AnsiToWin32)
 
91
            else self.stream.wrapped
 
92
        )
 
93
 
 
94
        # If the stream is a tty we should color it
 
95
        if hasattr(real_stream, "isatty") and real_stream.isatty():
 
96
            return True
 
97
 
 
98
        # If we have an ASNI term we should color it
 
99
        if os.environ.get("TERM") == "ANSI":
 
100
            return True
 
101
 
 
102
        # If anything else we should not color it
 
103
        return False
 
104
 
 
105
    def format(self, record):
 
106
        msg = logging.StreamHandler.format(self, record)
 
107
 
 
108
        if self.should_color():
 
109
            for level, color in self.COLORS:
 
110
                if record.levelno >= level:
 
111
                    msg = color(msg)
 
112
                    break
 
113
 
 
114
        return msg
 
115
 
 
116
 
 
117
class BetterRotatingFileHandler(logging.handlers.RotatingFileHandler):
 
118
 
 
119
    def _open(self):
 
120
        ensure_dir(os.path.dirname(self.baseFilename))
 
121
        return logging.handlers.RotatingFileHandler._open(self)
 
122
 
 
123
 
 
124
class MaxLevelFilter(logging.Filter):
 
125
 
 
126
    def __init__(self, level):
 
127
        self.level = level
 
128
 
 
129
    def filter(self, record):
 
130
        return record.levelno < self.level