diff options
author | Joshua Watt <JPEWhacker@gmail.com> | 2020-03-09 11:33:46 -0500 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2020-03-13 11:29:23 +0000 |
commit | 6b9eacb404914c24126c4ee08db68b2503f376f0 (patch) | |
tree | 2252d18b376529bc43935e80e69052864964eba0 | |
parent | 143685de1b9b0386a4dc8d404bdf342b45b7c030 (diff) | |
download | poky-6b9eacb404914c24126c4ee08db68b2503f376f0.tar.gz |
bitbake: lib/bb/msg: Add helper to set logging config
Adds a helper function to setup the structure logging information in
bitbake. The helper function takes a default configuration and an
optional path to a user config file that can be merged into the default.
(Bitbake rev: 14c98d36b74c1599b4649078170b6e72df79ba2a)
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r-- | bitbake/lib/bb/msg.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py index cab079e333..6259af037f 100644 --- a/bitbake/lib/bb/msg.py +++ b/bitbake/lib/bb/msg.py | |||
@@ -13,6 +13,7 @@ Message handling infrastructure for bitbake | |||
13 | import sys | 13 | import sys |
14 | import copy | 14 | import copy |
15 | import logging | 15 | import logging |
16 | import logging.config | ||
16 | from itertools import groupby | 17 | from itertools import groupby |
17 | import bb | 18 | import bb |
18 | import bb.event | 19 | import bb.event |
@@ -227,3 +228,95 @@ def has_console_handler(logger): | |||
227 | if handler.stream in [sys.stderr, sys.stdout]: | 228 | if handler.stream in [sys.stderr, sys.stdout]: |
228 | return True | 229 | return True |
229 | return False | 230 | return False |
231 | |||
232 | def setLoggingConfig(defaultconfig, userconfigfile=None): | ||
233 | logconfig = copy.deepcopy(defaultconfig) | ||
234 | |||
235 | if userconfigfile: | ||
236 | with open(userconfigfile, 'r') as f: | ||
237 | if userconfigfile.endswith('.yml') or userconfigfile.endswith('.yaml'): | ||
238 | import yaml | ||
239 | userconfig = yaml.load(f) | ||
240 | elif userconfigfile.endswith('.json') or userconfigfile.endswith('.cfg'): | ||
241 | import json | ||
242 | userconfig = json.load(f) | ||
243 | else: | ||
244 | raise BaseException("Unrecognized file format: %s" % userconfigfile) | ||
245 | |||
246 | if userconfig.get('bitbake_merge', True): | ||
247 | # Merge config with the default config | ||
248 | if userconfig.get('version') != logconfig['version']: | ||
249 | raise BaseException("Bad user configuration version. Expected %r, got %r" % (logconfig['version'], userconfig.get('version'))) | ||
250 | |||
251 | # Set some defaults to make merging easier | ||
252 | userconfig.setdefault("loggers", {}) | ||
253 | |||
254 | # If a handler, formatter, or filter is defined in the user | ||
255 | # config, it will replace an existing one in the default config | ||
256 | for k in ("handlers", "formatters", "filters"): | ||
257 | logconfig.setdefault(k, {}).update(userconfig.get(k, {})) | ||
258 | |||
259 | seen_loggers = set() | ||
260 | for name, l in logconfig["loggers"].items(): | ||
261 | # If the merge option is set, merge the handlers and | ||
262 | # filters. Otherwise, if it is False, this logger won't get | ||
263 | # add to the set of seen loggers and will replace the | ||
264 | # existing one | ||
265 | if l.get('bitbake_merge', True): | ||
266 | ulogger = userconfig["loggers"].setdefault(name, {}) | ||
267 | ulogger.setdefault("handlers", []) | ||
268 | ulogger.setdefault("filters", []) | ||
269 | |||
270 | # Merge lists | ||
271 | l.setdefault("handlers", []).extend(ulogger["handlers"]) | ||
272 | l.setdefault("filters", []).extend(ulogger["filters"]) | ||
273 | |||
274 | # Replace other properties if present | ||
275 | if "level" in ulogger: | ||
276 | l["level"] = ulogger["level"] | ||
277 | |||
278 | if "propagate" in ulogger: | ||
279 | l["propagate"] = ulogger["propagate"] | ||
280 | |||
281 | seen_loggers.add(name) | ||
282 | |||
283 | # Add all loggers present in the user config, but not any that | ||
284 | # have already been processed | ||
285 | for name in set(userconfig["loggers"].keys()) - seen_loggers: | ||
286 | logconfig["loggers"][name] = userconfig["loggers"][name] | ||
287 | |||
288 | else: | ||
289 | # Replace the entire default config | ||
290 | logconfig = userconfig | ||
291 | |||
292 | # Convert all level parameters to integers in case users want to use the | ||
293 | # bitbake defined level names | ||
294 | for h in logconfig["handlers"].values(): | ||
295 | if "level" in h: | ||
296 | h["level"] = bb.msg.stringToLevel(h["level"]) | ||
297 | |||
298 | for l in logconfig["loggers"].values(): | ||
299 | if "level" in l: | ||
300 | l["level"] = bb.msg.stringToLevel(l["level"]) | ||
301 | |||
302 | logging.config.dictConfig(logconfig) | ||
303 | |||
304 | # The user may have specified logging domains they want at a higher debug | ||
305 | # level than the standard. | ||
306 | for name, l in logconfig["loggers"].items(): | ||
307 | if not name.startswith("BitBake."): | ||
308 | continue | ||
309 | |||
310 | if not "level" in l: | ||
311 | continue | ||
312 | |||
313 | curlevel = bb.msg.loggerDefaultDomains.get(name) | ||
314 | # Note: level parameter should already be a int because of conversion | ||
315 | # above | ||
316 | newlevel = int(l["level"]) | ||
317 | if curlevel is None or newlevel < curlevel: | ||
318 | bb.msg.loggerDefaultDomains[name] = newlevel | ||
319 | |||
320 | # TODO: I don't think that setting the global log level should be necessary | ||
321 | #if newlevel < bb.msg.loggerDefaultLogLevel: | ||
322 | # bb.msg.loggerDefaultLogLevel = newlevel | ||