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.py196
1 files changed, 196 insertions, 0 deletions
diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py
new file mode 100644
index 0000000..d79768d
--- /dev/null
+++ b/bitbake/lib/bb/msg.py
@@ -0,0 +1,196 @@
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
129class 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
137class 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
148loggerDefaultDebugLevel = 0
149loggerDefaultVerbose = False
150loggerVerboseLogs = False
151loggerDefaultDomains = []
152
153def init_msgconfig(verbose, debug, debug_domains = []):
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 bb.msg.loggerDefaultDomains = debug_domains
162
163def constructLogOptions():
164 debug = loggerDefaultDebugLevel
165 verbose = loggerDefaultVerbose
166 domains = loggerDefaultDomains
167
168 if debug:
169 level = BBLogFormatter.DEBUG - debug + 1
170 elif verbose:
171 level = BBLogFormatter.VERBOSE
172 else:
173 level = BBLogFormatter.NOTE
174
175 debug_domains = {}
176 for (domainarg, iterator) in groupby(domains):
177 dlevel = len(tuple(iterator))
178 debug_domains["BitBake.%s" % domainarg] = logging.DEBUG - dlevel + 1
179 return level, debug_domains
180
181def addDefaultlogFilter(handler, cls = BBLogFilter):
182 level, debug_domains = constructLogOptions()
183
184 cls(handler, level, debug_domains)
185
186#
187# Message handling functions
188#
189
190def fatal(msgdomain, msg):
191 if msgdomain:
192 logger = logging.getLogger("BitBake.%s" % msgdomain)
193 else:
194 logger = logging.getLogger("BitBake")
195 logger.critical(msg)
196 sys.exit(1)