Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # ex:ts=4:sw=4:sts=4:et |
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 3 | """ |
| 4 | BitBake 'msg' implementation |
| 5 | |
| 6 | Message handling infrastructure for bitbake |
| 7 | |
| 8 | """ |
| 9 | |
| 10 | # Copyright (C) 2006 Richard Purdie |
| 11 | # |
| 12 | # This program is free software; you can redistribute it and/or modify |
| 13 | # it under the terms of the GNU General Public License version 2 as |
| 14 | # published by the Free Software Foundation. |
| 15 | # |
| 16 | # This program is distributed in the hope that it will be useful, |
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | # GNU General Public License for more details. |
| 20 | # |
| 21 | # You should have received a copy of the GNU General Public License along |
| 22 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | |
| 25 | import sys |
| 26 | import copy |
| 27 | import logging |
| 28 | import collections |
| 29 | from itertools import groupby |
| 30 | import warnings |
| 31 | import bb |
| 32 | import bb.event |
| 33 | |
| 34 | class BBLogFormatter(logging.Formatter): |
| 35 | """Formatter which ensures that our 'plain' messages (logging.INFO + 1) are used as is""" |
| 36 | |
| 37 | DEBUG3 = logging.DEBUG - 2 |
| 38 | DEBUG2 = logging.DEBUG - 1 |
| 39 | DEBUG = logging.DEBUG |
| 40 | VERBOSE = logging.INFO - 1 |
| 41 | NOTE = logging.INFO |
| 42 | PLAIN = logging.INFO + 1 |
| 43 | ERROR = logging.ERROR |
| 44 | WARNING = logging.WARNING |
| 45 | CRITICAL = logging.CRITICAL |
| 46 | |
| 47 | levelnames = { |
| 48 | DEBUG3 : 'DEBUG', |
| 49 | DEBUG2 : 'DEBUG', |
| 50 | DEBUG : 'DEBUG', |
| 51 | VERBOSE: 'NOTE', |
| 52 | NOTE : 'NOTE', |
| 53 | PLAIN : '', |
| 54 | WARNING : 'WARNING', |
| 55 | ERROR : 'ERROR', |
| 56 | CRITICAL: 'ERROR', |
| 57 | } |
| 58 | |
| 59 | color_enabled = False |
| 60 | BASECOLOR, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(29,38) |
| 61 | |
| 62 | COLORS = { |
| 63 | DEBUG3 : CYAN, |
| 64 | DEBUG2 : CYAN, |
| 65 | DEBUG : CYAN, |
| 66 | VERBOSE : BASECOLOR, |
| 67 | NOTE : BASECOLOR, |
| 68 | PLAIN : BASECOLOR, |
| 69 | WARNING : YELLOW, |
| 70 | ERROR : RED, |
| 71 | CRITICAL: RED, |
| 72 | } |
| 73 | |
| 74 | BLD = '\033[1;%dm' |
| 75 | STD = '\033[%dm' |
| 76 | RST = '\033[0m' |
| 77 | |
| 78 | def getLevelName(self, levelno): |
| 79 | try: |
| 80 | return self.levelnames[levelno] |
| 81 | except KeyError: |
| 82 | self.levelnames[levelno] = value = 'Level %d' % levelno |
| 83 | return value |
| 84 | |
| 85 | def format(self, record): |
| 86 | record.levelname = self.getLevelName(record.levelno) |
| 87 | if record.levelno == self.PLAIN: |
| 88 | msg = record.getMessage() |
| 89 | else: |
| 90 | if self.color_enabled: |
| 91 | record = self.colorize(record) |
| 92 | msg = logging.Formatter.format(self, record) |
| 93 | |
| 94 | if hasattr(record, 'bb_exc_info'): |
| 95 | etype, value, tb = record.bb_exc_info |
| 96 | formatted = bb.exceptions.format_exception(etype, value, tb, limit=5) |
| 97 | msg += '\n' + ''.join(formatted) |
| 98 | return msg |
| 99 | |
| 100 | def colorize(self, record): |
| 101 | color = self.COLORS[record.levelno] |
| 102 | if self.color_enabled and color is not None: |
| 103 | record = copy.copy(record) |
| 104 | record.levelname = "".join([self.BLD % color, record.levelname, self.RST]) |
| 105 | record.msg = "".join([self.STD % color, record.msg, self.RST]) |
| 106 | return record |
| 107 | |
| 108 | def enable_color(self): |
| 109 | self.color_enabled = True |
| 110 | |
| 111 | class BBLogFilter(object): |
| 112 | def __init__(self, handler, level, debug_domains): |
| 113 | self.stdlevel = level |
| 114 | self.debug_domains = debug_domains |
| 115 | loglevel = level |
| 116 | for domain in debug_domains: |
| 117 | if debug_domains[domain] < loglevel: |
| 118 | loglevel = debug_domains[domain] |
| 119 | handler.setLevel(loglevel) |
| 120 | handler.addFilter(self) |
| 121 | |
| 122 | def filter(self, record): |
| 123 | if record.levelno >= self.stdlevel: |
| 124 | return True |
| 125 | if record.name in self.debug_domains and record.levelno >= self.debug_domains[record.name]: |
| 126 | return True |
| 127 | return False |
| 128 | |
| 129 | class BBLogFilterStdErr(BBLogFilter): |
| 130 | def filter(self, record): |
| 131 | if not BBLogFilter.filter(self, record): |
| 132 | return False |
| 133 | if record.levelno >= logging.ERROR: |
| 134 | return True |
| 135 | return False |
| 136 | |
| 137 | class BBLogFilterStdOut(BBLogFilter): |
| 138 | def filter(self, record): |
| 139 | if not BBLogFilter.filter(self, record): |
| 140 | return False |
| 141 | if record.levelno < logging.ERROR: |
| 142 | return True |
| 143 | return False |
| 144 | |
| 145 | # Message control functions |
| 146 | # |
| 147 | |
| 148 | loggerDefaultDebugLevel = 0 |
| 149 | loggerDefaultVerbose = False |
| 150 | loggerVerboseLogs = False |
| 151 | loggerDefaultDomains = [] |
| 152 | |
| 153 | def init_msgconfig(verbose, debug, debug_domains=None): |
| 154 | """ |
| 155 | Set default verbosity and debug levels config the logger |
| 156 | """ |
| 157 | bb.msg.loggerDefaultDebugLevel = debug |
| 158 | bb.msg.loggerDefaultVerbose = verbose |
| 159 | if verbose: |
| 160 | bb.msg.loggerVerboseLogs = True |
| 161 | if debug_domains: |
| 162 | bb.msg.loggerDefaultDomains = debug_domains |
| 163 | else: |
| 164 | bb.msg.loggerDefaultDomains = [] |
| 165 | |
| 166 | def constructLogOptions(): |
| 167 | debug = loggerDefaultDebugLevel |
| 168 | verbose = loggerDefaultVerbose |
| 169 | domains = loggerDefaultDomains |
| 170 | |
| 171 | if debug: |
| 172 | level = BBLogFormatter.DEBUG - debug + 1 |
| 173 | elif verbose: |
| 174 | level = BBLogFormatter.VERBOSE |
| 175 | else: |
| 176 | level = BBLogFormatter.NOTE |
| 177 | |
| 178 | debug_domains = {} |
| 179 | for (domainarg, iterator) in groupby(domains): |
| 180 | dlevel = len(tuple(iterator)) |
| 181 | debug_domains["BitBake.%s" % domainarg] = logging.DEBUG - dlevel + 1 |
| 182 | return level, debug_domains |
| 183 | |
| 184 | def addDefaultlogFilter(handler, cls = BBLogFilter): |
| 185 | level, debug_domains = constructLogOptions() |
| 186 | |
| 187 | cls(handler, level, debug_domains) |
| 188 | |
| 189 | # |
| 190 | # Message handling functions |
| 191 | # |
| 192 | |
| 193 | def fatal(msgdomain, msg): |
| 194 | if msgdomain: |
| 195 | logger = logging.getLogger("BitBake.%s" % msgdomain) |
| 196 | else: |
| 197 | logger = logging.getLogger("BitBake") |
| 198 | logger.critical(msg) |
| 199 | sys.exit(1) |