summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/msg.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/msg.py')
-rw-r--r--bitbake/lib/bb/msg.py182
1 files changed, 182 insertions, 0 deletions
diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py
new file mode 100644
index 0000000000..59769707e0
--- /dev/null
+++ b/bitbake/lib/bb/msg.py
@@ -0,0 +1,182 @@
1# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4BitBake 'msg' implementation
5
6Message 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
25import sys
26import copy
27import logging
28import collections
29from itertools import groupby
30import warnings
31import bb
32import bb.event
33
34class 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
111class 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
130
131# Message control functions
132#
133
134loggerDefaultDebugLevel = 0
135loggerDefaultVerbose = False
136loggerVerboseLogs = False
137loggerDefaultDomains = []
138
139def init_msgconfig(verbose, debug, debug_domains = []):
140 """
141 Set default verbosity and debug levels config the logger
142 """
143 bb.msg.loggerDefaultDebugLevel = debug
144 bb.msg.loggerDefaultVerbose = verbose
145 if verbose:
146 bb.msg.loggerVerboseLogs = True
147 bb.msg.loggerDefaultDomains = debug_domains
148
149def constructLogOptions():
150 debug = loggerDefaultDebugLevel
151 verbose = loggerDefaultVerbose
152 domains = loggerDefaultDomains
153
154 if debug:
155 level = BBLogFormatter.DEBUG - debug + 1
156 elif verbose:
157 level = BBLogFormatter.VERBOSE
158 else:
159 level = BBLogFormatter.NOTE
160
161 debug_domains = {}
162 for (domainarg, iterator) in groupby(domains):
163 dlevel = len(tuple(iterator))
164 debug_domains["BitBake.%s" % domainarg] = logging.DEBUG - dlevel + 1
165 return level, debug_domains
166
167def addDefaultlogFilter(handler):
168 level, debug_domains = constructLogOptions()
169
170 BBLogFilter(handler, level, debug_domains)
171
172#
173# Message handling functions
174#
175
176def fatal(msgdomain, msg):
177 if msgdomain:
178 logger = logging.getLogger("BitBake.%s" % msgdomain)
179 else:
180 logger = logging.getLogger("BitBake")
181 logger.critical(msg)
182 sys.exit(1)