We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
logger: colorama is not outputting colors correctly on windows
version: `0.30.1`
![2019-03-07-12_47_49](https://user-images.githubusercontent.com/7363250/54006487-24e29280-4123-11e9-9d4f-28b7372c6b81.png)



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `dvc/logger.py`
Content:
```
1 """Manages logger for dvc repo."""
2 
3 from __future__ import unicode_literals
4 
5 from dvc.exceptions import DvcException
6 from dvc.utils.compat import str
7 from dvc.progress import progress_aware
8 
9 import re
10 import sys
11 import logging
12 import traceback
13 
14 from contextlib import contextmanager
15 
16 import colorama
17 
18 
19 @progress_aware
20 def info(message):
21     """Prints an info message."""
22     logger.info(message)
23 
24 
25 def debug(message):
26     """Prints a debug message."""
27     prefix = colorize("Debug", color="blue")
28 
29     out = "{prefix}: {message}".format(prefix=prefix, message=message)
30 
31     logger.debug(out)
32 
33 
34 @progress_aware
35 def warning(message, parse_exception=False):
36     """Prints a warning message."""
37     prefix = colorize("Warning", color="yellow")
38 
39     exception, stack_trace = None, ""
40     if parse_exception:
41         exception, stack_trace = _parse_exc()
42 
43     out = "{prefix}: {description}".format(
44         prefix=prefix, description=_description(message, exception)
45     )
46 
47     if stack_trace:
48         out += "\n{stack_trace}".format(stack_trace=stack_trace)
49 
50     logger.warning(out)
51 
52 
53 @progress_aware
54 def error(message=None):
55     """Prints an error message."""
56     prefix = colorize("Error", color="red")
57 
58     exception, stack_trace = _parse_exc()
59 
60     out = (
61         "{prefix}: {description}"
62         "\n"
63         "{stack_trace}"
64         "\n"
65         "{footer}".format(
66             prefix=prefix,
67             description=_description(message, exception),
68             stack_trace=stack_trace,
69             footer=_footer(),
70         )
71     )
72 
73     logger.error(out)
74 
75 
76 def box(message, border_color=None):
77     """Prints a message in a box.
78 
79     Args:
80         message (unicode): message to print.
81         border_color (unicode): name of a color to outline the box with.
82     """
83     lines = message.split("\n")
84     max_width = max(_visual_width(line) for line in lines)
85 
86     padding_horizontal = 5
87     padding_vertical = 1
88 
89     box_size_horizontal = max_width + (padding_horizontal * 2)
90 
91     chars = {"corner": "+", "horizontal": "-", "vertical": "|", "empty": " "}
92 
93     margin = "{corner}{line}{corner}\n".format(
94         corner=chars["corner"], line=chars["horizontal"] * box_size_horizontal
95     )
96 
97     padding_lines = [
98         "{border}{space}{border}\n".format(
99             border=colorize(chars["vertical"], color=border_color),
100             space=chars["empty"] * box_size_horizontal,
101         )
102         * padding_vertical
103     ]
104 
105     content_lines = [
106         "{border}{space}{content}{space}{border}\n".format(
107             border=colorize(chars["vertical"], color=border_color),
108             space=chars["empty"] * padding_horizontal,
109             content=_visual_center(line, max_width),
110         )
111         for line in lines
112     ]
113 
114     box_str = "{margin}{padding}{content}{padding}{margin}".format(
115         margin=colorize(margin, color=border_color),
116         padding="".join(padding_lines),
117         content="".join(content_lines),
118     )
119 
120     logger.info(box_str)
121 
122 
123 def level():
124     """Returns current log level."""
125     return logger.getEffectiveLevel()
126 
127 
128 def set_level(level_name):
129     """Sets log level.
130 
131     Args:
132         level_name (str): log level name. E.g. info, debug, warning, error,
133             critical.
134     """
135     if not level_name:
136         return
137 
138     levels = {
139         "info": logging.INFO,
140         "debug": logging.DEBUG,
141         "warning": logging.WARNING,
142         "error": logging.ERROR,
143         "critical": logging.CRITICAL,
144     }
145 
146     logger.setLevel(levels.get(level_name))
147 
148 
149 def be_quiet():
150     """Disables all messages except critical ones."""
151     logger.setLevel(logging.CRITICAL)
152 
153 
154 def be_verbose():
155     """Enables all messages."""
156     logger.setLevel(logging.DEBUG)
157 
158 
159 @contextmanager
160 def verbose():
161     """Enables verbose mode for the context."""
162     previous_level = level()
163     be_verbose()
164     yield
165     logger.setLevel(previous_level)
166 
167 
168 @contextmanager
169 def quiet():
170     """Enables quiet mode for the context."""
171     previous_level = level()
172     be_quiet()
173     yield
174     logger.setLevel(previous_level)
175 
176 
177 def is_quiet():
178     """Returns whether or not all messages except critical ones are
179     disabled.
180     """
181     return level() == logging.CRITICAL
182 
183 
184 def is_verbose():
185     """Returns whether or not all messages are enabled."""
186     return level() == logging.DEBUG
187 
188 
189 def colorize(message, color=None):
190     """Returns a message in a specified color."""
191     if not color:
192         return message
193 
194     colors = {
195         "green": colorama.Fore.GREEN,
196         "yellow": colorama.Fore.YELLOW,
197         "blue": colorama.Fore.BLUE,
198         "red": colorama.Fore.RED,
199     }
200 
201     return "{color}{message}{nc}".format(
202         color=colors.get(color, ""), message=message, nc=colorama.Fore.RESET
203     )
204 
205 
206 def _init_colorama():
207     colorama.init()
208 
209 
210 def set_default_level():
211     """Sets default log level."""
212     logger.setLevel(logging.INFO)
213 
214 
215 def _add_handlers():
216     formatter = "%(message)s"
217 
218     class _LogLevelFilter(logging.Filter):
219         # pylint: disable=too-few-public-methods
220         def filter(self, record):
221             return record.levelno <= logging.WARNING
222 
223     sh_out = logging.StreamHandler(sys.stdout)
224     sh_out.setFormatter(logging.Formatter(formatter))
225     sh_out.setLevel(logging.DEBUG)
226     sh_out.addFilter(_LogLevelFilter())
227 
228     sh_err = logging.StreamHandler(sys.stderr)
229     sh_err.setFormatter(logging.Formatter(formatter))
230     sh_err.setLevel(logging.ERROR)
231 
232     logger.addHandler(sh_out)
233     logger.addHandler(sh_err)
234 
235 
236 def _walk_exc(exc):
237     exc_list = [str(exc)]
238     tb_list = [traceback.format_exc()]
239 
240     # NOTE: parsing chained exceptions. See dvc/exceptions.py for more info.
241     while hasattr(exc, "cause") and exc.cause is not None:
242         exc_list.append(str(exc.cause))
243         if hasattr(exc, "cause_tb") and exc.cause_tb is not None:
244             tb_list.insert(0, str(exc.cause_tb))
245         exc = exc.cause
246 
247     return exc_list, tb_list
248 
249 
250 def _parse_exc():
251     exc = sys.exc_info()[1]
252     if not exc:
253         return (None, "")
254 
255     exc_list, tb_list = _walk_exc(exc)
256 
257     exception = ": ".join(exc_list)
258 
259     if is_verbose():
260         stack_trace = "{line}\n{stack_trace}{line}\n".format(
261             line=colorize("-" * 60, color="red"),
262             stack_trace="\n".join(tb_list),
263         )
264     else:
265         stack_trace = ""
266 
267     return (exception, stack_trace)
268 
269 
270 def _description(message, exception):
271     if exception and message:
272         description = "{message} - {exception}"
273     elif exception:
274         description = "{exception}"
275     elif message:
276         description = "{message}"
277     else:
278         raise DvcException(
279             "Unexpected error - either exception or message must be provided"
280         )
281 
282     return description.format(message=message, exception=exception)
283 
284 
285 def _footer():
286     return "{phrase} Hit us up at {url}, we are always happy to help!".format(
287         phrase=colorize("Having any troubles?", "yellow"),
288         url=colorize("https://dvc.org/support", "blue"),
289     )
290 
291 
292 def _visual_width(line):
293     """Get the the number of columns required to display a string"""
294 
295     return len(re.sub(colorama.ansitowin32.AnsiToWin32.ANSI_CSI_RE, "", line))
296 
297 
298 def _visual_center(line, width):
299     """Center align string according to it's visual width"""
300 
301     spaces = max(width - _visual_width(line), 0)
302     left_padding = int(spaces / 2)
303     right_padding = spaces - left_padding
304 
305     return (left_padding * " ") + line + (right_padding * " ")
306 
307 
308 logger = logging.getLogger("dvc")  # pylint: disable=invalid-name
309 
310 set_default_level()
311 _add_handlers()
312 _init_colorama()
313
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/dvc/logger.py b/dvc/logger.py
--- a/dvc/logger.py
+++ b/dvc/logger.py
@@ -203,16 +203,16 @@
     )
 
 
-def _init_colorama():
-    colorama.init()
-
-
 def set_default_level():
     """Sets default log level."""
     logger.setLevel(logging.INFO)
 
 
 def _add_handlers():
+    # NOTE: We need to initialize colorama before setting the stream handlers
+    #       so it can wrap stdout/stderr and convert color codes to Windows.
+    colorama.init()
+
     formatter = "%(message)s"
 
     class _LogLevelFilter(logging.Filter):
@@ -309,4 +309,3 @@
 
 set_default_level()
 _add_handlers()
-_init_colorama()

{"golden_diff": "diff --git a/dvc/logger.py b/dvc/logger.py\n--- a/dvc/logger.py\n+++ b/dvc/logger.py\n@@ -203,16 +203,16 @@\n     )\n \n \n-def _init_colorama():\n-    colorama.init()\n-\n-\n def set_default_level():\n     \"\"\"Sets default log level.\"\"\"\n     logger.setLevel(logging.INFO)\n \n \n def _add_handlers():\n+    # NOTE: We need to initialize colorama before setting the stream handlers\n+    #       so it can wrap stdout/stderr and convert color codes to Windows.\n+    colorama.init()\n+\n     formatter = \"%(message)s\"\n \n     class _LogLevelFilter(logging.Filter):\n@@ -309,4 +309,3 @@\n \n set_default_level()\n _add_handlers()\n-_init_colorama()\n", "issue": "logger: colorama is not outputting colors correctly on windows\nversion: `0.30.1`\r\n![2019-03-07-12_47_49](https://user-images.githubusercontent.com/7363250/54006487-24e29280-4123-11e9-9d4f-28b7372c6b81.png)\r\n\r\n\n", "before_files": [{"content": "\"\"\"Manages logger for dvc repo.\"\"\"\n\nfrom __future__ import unicode_literals\n\nfrom dvc.exceptions import DvcException\nfrom dvc.utils.compat import str\nfrom dvc.progress import progress_aware\n\nimport re\nimport sys\nimport logging\nimport traceback\n\nfrom contextlib import contextmanager\n\nimport colorama\n\n\n@progress_aware\ndef info(message):\n    \"\"\"Prints an info message.\"\"\"\n    logger.info(message)\n\n\ndef debug(message):\n    \"\"\"Prints a debug message.\"\"\"\n    prefix = colorize(\"Debug\", color=\"blue\")\n\n    out = \"{prefix}: {message}\".format(prefix=prefix, message=message)\n\n    logger.debug(out)\n\n\n@progress_aware\ndef warning(message, parse_exception=False):\n    \"\"\"Prints a warning message.\"\"\"\n    prefix = colorize(\"Warning\", color=\"yellow\")\n\n    exception, stack_trace = None, \"\"\n    if parse_exception:\n        exception, stack_trace = _parse_exc()\n\n    out = \"{prefix}: {description}\".format(\n        prefix=prefix, description=_description(message, exception)\n    )\n\n    if stack_trace:\n        out += \"\\n{stack_trace}\".format(stack_trace=stack_trace)\n\n    logger.warning(out)\n\n\n@progress_aware\ndef error(message=None):\n    \"\"\"Prints an error message.\"\"\"\n    prefix = colorize(\"Error\", color=\"red\")\n\n    exception, stack_trace = _parse_exc()\n\n    out = (\n        \"{prefix}: {description}\"\n        \"\\n\"\n        \"{stack_trace}\"\n        \"\\n\"\n        \"{footer}\".format(\n            prefix=prefix,\n            description=_description(message, exception),\n            stack_trace=stack_trace,\n            footer=_footer(),\n        )\n    )\n\n    logger.error(out)\n\n\ndef box(message, border_color=None):\n    \"\"\"Prints a message in a box.\n\n    Args:\n        message (unicode): message to print.\n        border_color (unicode): name of a color to outline the box with.\n    \"\"\"\n    lines = message.split(\"\\n\")\n    max_width = max(_visual_width(line) for line in lines)\n\n    padding_horizontal = 5\n    padding_vertical = 1\n\n    box_size_horizontal = max_width + (padding_horizontal * 2)\n\n    chars = {\"corner\": \"+\", \"horizontal\": \"-\", \"vertical\": \"|\", \"empty\": \" \"}\n\n    margin = \"{corner}{line}{corner}\\n\".format(\n        corner=chars[\"corner\"], line=chars[\"horizontal\"] * box_size_horizontal\n    )\n\n    padding_lines = [\n        \"{border}{space}{border}\\n\".format(\n            border=colorize(chars[\"vertical\"], color=border_color),\n            space=chars[\"empty\"] * box_size_horizontal,\n        )\n        * padding_vertical\n    ]\n\n    content_lines = [\n        \"{border}{space}{content}{space}{border}\\n\".format(\n            border=colorize(chars[\"vertical\"], color=border_color),\n            space=chars[\"empty\"] * padding_horizontal,\n            content=_visual_center(line, max_width),\n        )\n        for line in lines\n    ]\n\n    box_str = \"{margin}{padding}{content}{padding}{margin}\".format(\n        margin=colorize(margin, color=border_color),\n        padding=\"\".join(padding_lines),\n        content=\"\".join(content_lines),\n    )\n\n    logger.info(box_str)\n\n\ndef level():\n    \"\"\"Returns current log level.\"\"\"\n    return logger.getEffectiveLevel()\n\n\ndef set_level(level_name):\n    \"\"\"Sets log level.\n\n    Args:\n        level_name (str): log level name. E.g. info, debug, warning, error,\n            critical.\n    \"\"\"\n    if not level_name:\n        return\n\n    levels = {\n        \"info\": logging.INFO,\n        \"debug\": logging.DEBUG,\n        \"warning\": logging.WARNING,\n        \"error\": logging.ERROR,\n        \"critical\": logging.CRITICAL,\n    }\n\n    logger.setLevel(levels.get(level_name))\n\n\ndef be_quiet():\n    \"\"\"Disables all messages except critical ones.\"\"\"\n    logger.setLevel(logging.CRITICAL)\n\n\ndef be_verbose():\n    \"\"\"Enables all messages.\"\"\"\n    logger.setLevel(logging.DEBUG)\n\n\n@contextmanager\ndef verbose():\n    \"\"\"Enables verbose mode for the context.\"\"\"\n    previous_level = level()\n    be_verbose()\n    yield\n    logger.setLevel(previous_level)\n\n\n@contextmanager\ndef quiet():\n    \"\"\"Enables quiet mode for the context.\"\"\"\n    previous_level = level()\n    be_quiet()\n    yield\n    logger.setLevel(previous_level)\n\n\ndef is_quiet():\n    \"\"\"Returns whether or not all messages except critical ones are\n    disabled.\n    \"\"\"\n    return level() == logging.CRITICAL\n\n\ndef is_verbose():\n    \"\"\"Returns whether or not all messages are enabled.\"\"\"\n    return level() == logging.DEBUG\n\n\ndef colorize(message, color=None):\n    \"\"\"Returns a message in a specified color.\"\"\"\n    if not color:\n        return message\n\n    colors = {\n        \"green\": colorama.Fore.GREEN,\n        \"yellow\": colorama.Fore.YELLOW,\n        \"blue\": colorama.Fore.BLUE,\n        \"red\": colorama.Fore.RED,\n    }\n\n    return \"{color}{message}{nc}\".format(\n        color=colors.get(color, \"\"), message=message, nc=colorama.Fore.RESET\n    )\n\n\ndef _init_colorama():\n    colorama.init()\n\n\ndef set_default_level():\n    \"\"\"Sets default log level.\"\"\"\n    logger.setLevel(logging.INFO)\n\n\ndef _add_handlers():\n    formatter = \"%(message)s\"\n\n    class _LogLevelFilter(logging.Filter):\n        # pylint: disable=too-few-public-methods\n        def filter(self, record):\n            return record.levelno <= logging.WARNING\n\n    sh_out = logging.StreamHandler(sys.stdout)\n    sh_out.setFormatter(logging.Formatter(formatter))\n    sh_out.setLevel(logging.DEBUG)\n    sh_out.addFilter(_LogLevelFilter())\n\n    sh_err = logging.StreamHandler(sys.stderr)\n    sh_err.setFormatter(logging.Formatter(formatter))\n    sh_err.setLevel(logging.ERROR)\n\n    logger.addHandler(sh_out)\n    logger.addHandler(sh_err)\n\n\ndef _walk_exc(exc):\n    exc_list = [str(exc)]\n    tb_list = [traceback.format_exc()]\n\n    # NOTE: parsing chained exceptions. See dvc/exceptions.py for more info.\n    while hasattr(exc, \"cause\") and exc.cause is not None:\n        exc_list.append(str(exc.cause))\n        if hasattr(exc, \"cause_tb\") and exc.cause_tb is not None:\n            tb_list.insert(0, str(exc.cause_tb))\n        exc = exc.cause\n\n    return exc_list, tb_list\n\n\ndef _parse_exc():\n    exc = sys.exc_info()[1]\n    if not exc:\n        return (None, \"\")\n\n    exc_list, tb_list = _walk_exc(exc)\n\n    exception = \": \".join(exc_list)\n\n    if is_verbose():\n        stack_trace = \"{line}\\n{stack_trace}{line}\\n\".format(\n            line=colorize(\"-\" * 60, color=\"red\"),\n            stack_trace=\"\\n\".join(tb_list),\n        )\n    else:\n        stack_trace = \"\"\n\n    return (exception, stack_trace)\n\n\ndef _description(message, exception):\n    if exception and message:\n        description = \"{message} - {exception}\"\n    elif exception:\n        description = \"{exception}\"\n    elif message:\n        description = \"{message}\"\n    else:\n        raise DvcException(\n            \"Unexpected error - either exception or message must be provided\"\n        )\n\n    return description.format(message=message, exception=exception)\n\n\ndef _footer():\n    return \"{phrase} Hit us up at {url}, we are always happy to help!\".format(\n        phrase=colorize(\"Having any troubles?\", \"yellow\"),\n        url=colorize(\"https://dvc.org/support\", \"blue\"),\n    )\n\n\ndef _visual_width(line):\n    \"\"\"Get the the number of columns required to display a string\"\"\"\n\n    return len(re.sub(colorama.ansitowin32.AnsiToWin32.ANSI_CSI_RE, \"\", line))\n\n\ndef _visual_center(line, width):\n    \"\"\"Center align string according to it's visual width\"\"\"\n\n    spaces = max(width - _visual_width(line), 0)\n    left_padding = int(spaces / 2)\n    right_padding = spaces - left_padding\n\n    return (left_padding * \" \") + line + (right_padding * \" \")\n\n\nlogger = logging.getLogger(\"dvc\")  # pylint: disable=invalid-name\n\nset_default_level()\n_add_handlers()\n_init_colorama()\n", "path": "dvc/logger.py"}], "after_files": [{"content": "\"\"\"Manages logger for dvc repo.\"\"\"\n\nfrom __future__ import unicode_literals\n\nfrom dvc.exceptions import DvcException\nfrom dvc.utils.compat import str\nfrom dvc.progress import progress_aware\n\nimport re\nimport sys\nimport logging\nimport traceback\n\nfrom contextlib import contextmanager\n\nimport colorama\n\n\n@progress_aware\ndef info(message):\n    \"\"\"Prints an info message.\"\"\"\n    logger.info(message)\n\n\ndef debug(message):\n    \"\"\"Prints a debug message.\"\"\"\n    prefix = colorize(\"Debug\", color=\"blue\")\n\n    out = \"{prefix}: {message}\".format(prefix=prefix, message=message)\n\n    logger.debug(out)\n\n\n@progress_aware\ndef warning(message, parse_exception=False):\n    \"\"\"Prints a warning message.\"\"\"\n    prefix = colorize(\"Warning\", color=\"yellow\")\n\n    exception, stack_trace = None, \"\"\n    if parse_exception:\n        exception, stack_trace = _parse_exc()\n\n    out = \"{prefix}: {description}\".format(\n        prefix=prefix, description=_description(message, exception)\n    )\n\n    if stack_trace:\n        out += \"\\n{stack_trace}\".format(stack_trace=stack_trace)\n\n    logger.warning(out)\n\n\n@progress_aware\ndef error(message=None):\n    \"\"\"Prints an error message.\"\"\"\n    prefix = colorize(\"Error\", color=\"red\")\n\n    exception, stack_trace = _parse_exc()\n\n    out = (\n        \"{prefix}: {description}\"\n        \"\\n\"\n        \"{stack_trace}\"\n        \"\\n\"\n        \"{footer}\".format(\n            prefix=prefix,\n            description=_description(message, exception),\n            stack_trace=stack_trace,\n            footer=_footer(),\n        )\n    )\n\n    logger.error(out)\n\n\ndef box(message, border_color=None):\n    \"\"\"Prints a message in a box.\n\n    Args:\n        message (unicode): message to print.\n        border_color (unicode): name of a color to outline the box with.\n    \"\"\"\n    lines = message.split(\"\\n\")\n    max_width = max(_visual_width(line) for line in lines)\n\n    padding_horizontal = 5\n    padding_vertical = 1\n\n    box_size_horizontal = max_width + (padding_horizontal * 2)\n\n    chars = {\"corner\": \"+\", \"horizontal\": \"-\", \"vertical\": \"|\", \"empty\": \" \"}\n\n    margin = \"{corner}{line}{corner}\\n\".format(\n        corner=chars[\"corner\"], line=chars[\"horizontal\"] * box_size_horizontal\n    )\n\n    padding_lines = [\n        \"{border}{space}{border}\\n\".format(\n            border=colorize(chars[\"vertical\"], color=border_color),\n            space=chars[\"empty\"] * box_size_horizontal,\n        )\n        * padding_vertical\n    ]\n\n    content_lines = [\n        \"{border}{space}{content}{space}{border}\\n\".format(\n            border=colorize(chars[\"vertical\"], color=border_color),\n            space=chars[\"empty\"] * padding_horizontal,\n            content=_visual_center(line, max_width),\n        )\n        for line in lines\n    ]\n\n    box_str = \"{margin}{padding}{content}{padding}{margin}\".format(\n        margin=colorize(margin, color=border_color),\n        padding=\"\".join(padding_lines),\n        content=\"\".join(content_lines),\n    )\n\n    logger.info(box_str)\n\n\ndef level():\n    \"\"\"Returns current log level.\"\"\"\n    return logger.getEffectiveLevel()\n\n\ndef set_level(level_name):\n    \"\"\"Sets log level.\n\n    Args:\n        level_name (str): log level name. E.g. info, debug, warning, error,\n            critical.\n    \"\"\"\n    if not level_name:\n        return\n\n    levels = {\n        \"info\": logging.INFO,\n        \"debug\": logging.DEBUG,\n        \"warning\": logging.WARNING,\n        \"error\": logging.ERROR,\n        \"critical\": logging.CRITICAL,\n    }\n\n    logger.setLevel(levels.get(level_name))\n\n\ndef be_quiet():\n    \"\"\"Disables all messages except critical ones.\"\"\"\n    logger.setLevel(logging.CRITICAL)\n\n\ndef be_verbose():\n    \"\"\"Enables all messages.\"\"\"\n    logger.setLevel(logging.DEBUG)\n\n\n@contextmanager\ndef verbose():\n    \"\"\"Enables verbose mode for the context.\"\"\"\n    previous_level = level()\n    be_verbose()\n    yield\n    logger.setLevel(previous_level)\n\n\n@contextmanager\ndef quiet():\n    \"\"\"Enables quiet mode for the context.\"\"\"\n    previous_level = level()\n    be_quiet()\n    yield\n    logger.setLevel(previous_level)\n\n\ndef is_quiet():\n    \"\"\"Returns whether or not all messages except critical ones are\n    disabled.\n    \"\"\"\n    return level() == logging.CRITICAL\n\n\ndef is_verbose():\n    \"\"\"Returns whether or not all messages are enabled.\"\"\"\n    return level() == logging.DEBUG\n\n\ndef colorize(message, color=None):\n    \"\"\"Returns a message in a specified color.\"\"\"\n    if not color:\n        return message\n\n    colors = {\n        \"green\": colorama.Fore.GREEN,\n        \"yellow\": colorama.Fore.YELLOW,\n        \"blue\": colorama.Fore.BLUE,\n        \"red\": colorama.Fore.RED,\n    }\n\n    return \"{color}{message}{nc}\".format(\n        color=colors.get(color, \"\"), message=message, nc=colorama.Fore.RESET\n    )\n\n\ndef set_default_level():\n    \"\"\"Sets default log level.\"\"\"\n    logger.setLevel(logging.INFO)\n\n\ndef _add_handlers():\n    # NOTE: We need to initialize colorama before setting the stream handlers\n    #       so it can wrap stdout/stderr and convert color codes to Windows.\n    colorama.init()\n\n    formatter = \"%(message)s\"\n\n    class _LogLevelFilter(logging.Filter):\n        # pylint: disable=too-few-public-methods\n        def filter(self, record):\n            return record.levelno <= logging.WARNING\n\n    sh_out = logging.StreamHandler(sys.stdout)\n    sh_out.setFormatter(logging.Formatter(formatter))\n    sh_out.setLevel(logging.DEBUG)\n    sh_out.addFilter(_LogLevelFilter())\n\n    sh_err = logging.StreamHandler(sys.stderr)\n    sh_err.setFormatter(logging.Formatter(formatter))\n    sh_err.setLevel(logging.ERROR)\n\n    logger.addHandler(sh_out)\n    logger.addHandler(sh_err)\n\n\ndef _walk_exc(exc):\n    exc_list = [str(exc)]\n    tb_list = [traceback.format_exc()]\n\n    # NOTE: parsing chained exceptions. See dvc/exceptions.py for more info.\n    while hasattr(exc, \"cause\") and exc.cause is not None:\n        exc_list.append(str(exc.cause))\n        if hasattr(exc, \"cause_tb\") and exc.cause_tb is not None:\n            tb_list.insert(0, str(exc.cause_tb))\n        exc = exc.cause\n\n    return exc_list, tb_list\n\n\ndef _parse_exc():\n    exc = sys.exc_info()[1]\n    if not exc:\n        return (None, \"\")\n\n    exc_list, tb_list = _walk_exc(exc)\n\n    exception = \": \".join(exc_list)\n\n    if is_verbose():\n        stack_trace = \"{line}\\n{stack_trace}{line}\\n\".format(\n            line=colorize(\"-\" * 60, color=\"red\"),\n            stack_trace=\"\\n\".join(tb_list),\n        )\n    else:\n        stack_trace = \"\"\n\n    return (exception, stack_trace)\n\n\ndef _description(message, exception):\n    if exception and message:\n        description = \"{message} - {exception}\"\n    elif exception:\n        description = \"{exception}\"\n    elif message:\n        description = \"{message}\"\n    else:\n        raise DvcException(\n            \"Unexpected error - either exception or message must be provided\"\n        )\n\n    return description.format(message=message, exception=exception)\n\n\ndef _footer():\n    return \"{phrase} Hit us up at {url}, we are always happy to help!\".format(\n        phrase=colorize(\"Having any troubles?\", \"yellow\"),\n        url=colorize(\"https://dvc.org/support\", \"blue\"),\n    )\n\n\ndef _visual_width(line):\n    \"\"\"Get the the number of columns required to display a string\"\"\"\n\n    return len(re.sub(colorama.ansitowin32.AnsiToWin32.ANSI_CSI_RE, \"\", line))\n\n\ndef _visual_center(line, width):\n    \"\"\"Center align string according to it's visual width\"\"\"\n\n    spaces = max(width - _visual_width(line), 0)\n    left_padding = int(spaces / 2)\n    right_padding = spaces - left_padding\n\n    return (left_padding * \" \") + line + (right_padding * \" \")\n\n\nlogger = logging.getLogger(\"dvc\")  # pylint: disable=invalid-name\n\nset_default_level()\n_add_handlers()\n", "path": "dvc/logger.py"}]}