Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: build/android/pylib/utils/logging_utils.py

Issue 1418833005: [gms updater] Fixes and tests to prepare activation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use SDK/library appropriately Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/play_services/utils.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import contextlib 5 import contextlib
6 import logging 6 import logging
7 import os 7 import os
8 import sys 8 import sys
9 9
10 from pylib import constants 10 from pylib import constants
(...skipping 18 matching lines...) Expand all
29 """ 29 """
30 # pylint does not see members added dynamically in the constructor. 30 # pylint does not see members added dynamically in the constructor.
31 # pylint: disable=no-member 31 # pylint: disable=no-member
32 color_map = { 32 color_map = {
33 logging.DEBUG: colorama.Fore.CYAN, 33 logging.DEBUG: colorama.Fore.CYAN,
34 logging.WARNING: colorama.Fore.YELLOW, 34 logging.WARNING: colorama.Fore.YELLOW,
35 logging.ERROR: colorama.Fore.RED, 35 logging.ERROR: colorama.Fore.RED,
36 logging.CRITICAL: colorama.Back.RED + colorama.Style.BRIGHT, 36 logging.CRITICAL: colorama.Back.RED + colorama.Style.BRIGHT,
37 } 37 }
38 38
39 def __init__(self, force_color=False):
40 super(ColorStreamHandler, self).__init__()
41 self.force_color = force_color
42
39 @property 43 @property
40 def is_tty(self): 44 def is_tty(self):
41 isatty = getattr(self.stream, 'isatty', None) 45 isatty = getattr(self.stream, 'isatty', None)
42 return isatty and isatty() 46 return isatty and isatty()
43 47
44 #override 48 #override
45 def format(self, record): 49 def format(self, record):
46 message = logging.StreamHandler.format(self, record) 50 message = logging.StreamHandler.format(self, record)
47 if self.is_tty: 51 if self.force_color or self.is_tty:
48 return self.Colorize(message, record.levelno) 52 return self.Colorize(message, record.levelno)
53 return message
49 54
50 def Colorize(self, message, log_level): 55 def Colorize(self, message, log_level):
51 try: 56 try:
52 return self.color_map[log_level] + message + colorama.Style.RESET_ALL 57 return self.color_map[log_level] + message + colorama.Style.RESET_ALL
53 except KeyError: 58 except KeyError:
54 return message 59 return message
55 60
56 @staticmethod 61 @staticmethod
57 def MakeDefault(): 62 def MakeDefault(force_color=False):
58 """ 63 """
59 Replaces the default logging handlers with a coloring handler. To use 64 Replaces the default logging handlers with a coloring handler. To use
60 a colorizing handler at the same time as others, either register them 65 a colorizing handler at the same time as others, either register them
61 after this call, or add the ColorStreamHandler on the logger using 66 after this call, or add the ColorStreamHandler on the logger using
62 Logger.addHandler() 67 Logger.addHandler()
68
69 Args:
70 force_color: Set to True to bypass the tty check and always colorize.
63 """ 71 """
64 # If the existing handlers aren't removed, messages are duplicated 72 # If the existing handlers aren't removed, messages are duplicated
65 logging.getLogger().handlers = [] 73 logging.getLogger().handlers = []
66 logging.getLogger().addHandler(ColorStreamHandler()) 74 logging.getLogger().addHandler(ColorStreamHandler(force_color))
67 75
68 76
69 @contextlib.contextmanager 77 @contextlib.contextmanager
70 def SuppressLogging(level=logging.ERROR): 78 def SuppressLogging(level=logging.ERROR):
71 """Momentarilly suppress logging events from all loggers. 79 """Momentarilly suppress logging events from all loggers.
72 80
73 TODO(jbudorick): This is not thread safe. Log events from other threads might 81 TODO(jbudorick): This is not thread safe. Log events from other threads might
74 also inadvertently dissapear. 82 also inadvertently dissapear.
75 83
76 Example: 84 Example:
77 85
78 with logging_utils.SuppressLogging(): 86 with logging_utils.SuppressLogging():
79 # all but CRITICAL logging messages are suppressed 87 # all but CRITICAL logging messages are suppressed
80 logging.info('just doing some thing') # not shown 88 logging.info('just doing some thing') # not shown
81 logging.critical('something really bad happened') # still shown 89 logging.critical('something really bad happened') # still shown
82 90
83 Args: 91 Args:
84 level: logging events with this or lower levels are suppressed. 92 level: logging events with this or lower levels are suppressed.
85 """ 93 """
86 logging.disable(level) 94 logging.disable(level)
87 yield 95 yield
88 logging.disable(logging.NOTSET) 96 logging.disable(logging.NOTSET)
OLDNEW
« no previous file with comments | « build/android/play_services/utils.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698