summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb
diff options
context:
space:
mode:
authorChris Larson <chris_larson@mentor.com>2010-06-09 16:17:29 -0700
committerRichard Purdie <rpurdie@linux.intel.com>2011-01-04 14:46:33 +0000
commit4855548ffbbb6b972f6b71d61d2b29e4758acdc7 (patch)
tree259485fe325ac4770002b4d8c53288be71bc8fc8 /bitbake/lib/bb
parent3e57e63b2d16fa3f1ec37a904e8538e00a301fdd (diff)
downloadpoky-4855548ffbbb6b972f6b71d61d2b29e4758acdc7.tar.gz
Use the python logging module under the hood for bb.msg
(Bitbake rev: 47ca82397bc395b598c6b68b24cdee9e0d8a76d8) Signed-off-by: Chris Larson <chris_larson@mentor.com> Signed-off-by: Richard Purdie <rpurdie@linux.intel.com>
Diffstat (limited to 'bitbake/lib/bb')
-rw-r--r--bitbake/lib/bb/__init__.py10
-rw-r--r--bitbake/lib/bb/event.py51
-rw-r--r--bitbake/lib/bb/msg.py134
-rw-r--r--bitbake/lib/bb/ui/knotty.py12
-rw-r--r--bitbake/lib/bb/utils.py11
5 files changed, 138 insertions, 80 deletions
diff --git a/bitbake/lib/bb/__init__.py b/bitbake/lib/bb/__init__.py
index 88adfc1dfa..8cda4255bc 100644
--- a/bitbake/lib/bb/__init__.py
+++ b/bitbake/lib/bb/__init__.py
@@ -28,8 +28,18 @@ if sys.version_info < (2, 6, 0):
28 raise RuntimeError("Sorry, python 2.6.0 or later is required for this version of bitbake") 28 raise RuntimeError("Sorry, python 2.6.0 or later is required for this version of bitbake")
29 29
30import os 30import os
31import logging
31import bb.msg 32import bb.msg
32 33
34class NullHandler(logging.Handler):
35 def emit(self, record):
36 pass
37
38logging.raiseExceptions = False
39logger = logging.getLogger("BitBake")
40logger.addHandler(NullHandler())
41logger.setLevel(logging.INFO)
42
33if "BBDEBUG" in os.environ: 43if "BBDEBUG" in os.environ:
34 level = int(os.environ["BBDEBUG"]) 44 level = int(os.environ["BBDEBUG"])
35 if level: 45 if level:
diff --git a/bitbake/lib/bb/event.py b/bitbake/lib/bb/event.py
index 45458c2d63..c04ffd5ac1 100644
--- a/bitbake/lib/bb/event.py
+++ b/bitbake/lib/bb/event.py
@@ -24,8 +24,9 @@ BitBake build tools.
24 24
25import os, sys 25import os, sys
26import warnings 26import warnings
27import bb.utils
28import pickle 27import pickle
28import logging
29import bb.utils
29 30
30# This is the pid for which we should generate the event. This is set when 31# This is the pid for which we should generate the event. This is set when
31# the runqueue forks off. 32# the runqueue forks off.
@@ -56,7 +57,7 @@ bb.utils._context["Handled"] = Handled
56 57
57def fire_class_handlers(event, d): 58def fire_class_handlers(event, d):
58 import bb.msg 59 import bb.msg
59 if isinstance(event, bb.msg.MsgBase): 60 if isinstance(event, MsgBase):
60 return 61 return
61 62
62 for handler in _handlers: 63 for handler in _handlers:
@@ -298,3 +299,49 @@ class DepTreeGenerated(Event):
298 def __init__(self, depgraph): 299 def __init__(self, depgraph):
299 Event.__init__(self) 300 Event.__init__(self)
300 self._depgraph = depgraph 301 self._depgraph = depgraph
302
303class MsgBase(Event):
304 """Base class for messages"""
305
306 def __init__(self, msg):
307 self._message = msg
308 Event.__init__(self)
309
310class MsgDebug(MsgBase):
311 """Debug Message"""
312
313class MsgNote(MsgBase):
314 """Note Message"""
315
316class MsgWarn(MsgBase):
317 """Warning Message"""
318
319class MsgError(MsgBase):
320 """Error Message"""
321
322class MsgFatal(MsgBase):
323 """Fatal Message"""
324
325class MsgPlain(MsgBase):
326 """General output"""
327
328class LogHandler(logging.Handler):
329 """Dispatch logging messages as bitbake events"""
330
331 messages = (
332 (logging.DEBUG, MsgDebug),
333 (logging.INFO, MsgNote),
334 (logging.WARNING, MsgWarn),
335 (logging.ERROR, MsgError),
336 (logging.CRITICAL, MsgFatal),
337 )
338
339 def emit(self, record):
340 for level, msgclass in self.messages:
341 if record.levelno <= level:
342 msg = self.format(record)
343 fire(msgclass(msg), None)
344 if bb.event.useStdout:
345 print(record.levelname + ": " + record.getMessage())
346 break
347
diff --git a/bitbake/lib/bb/msg.py b/bitbake/lib/bb/msg.py
index 21692d930d..254ba07a04 100644
--- a/bitbake/lib/bb/msg.py
+++ b/bitbake/lib/bb/msg.py
@@ -23,12 +23,26 @@ Message handling infrastructure for bitbake
23# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 23# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 24
25import sys 25import sys
26import logging
26import collections 27import collections
28from itertools import groupby
27import bb 29import bb
28import bb.event 30import bb.event
29 31
30debug_level = collections.defaultdict(lambda: 0) 32class Loggers(dict):
31verbose = False 33 def __getitem__(self, key):
34 if key in self:
35 return dict.__getitem__(self, key)
36 else:
37 log = logging.getLogger("BitBake.%s" % domain._fields[key])
38 dict.__setitem__(self, key, log)
39 return log
40
41class DebugLevel(dict):
42 def __getitem__(self, key):
43 if key == "default":
44 key = domain.Default
45 return get_debug_level(key)
32 46
33def _NamedTuple(name, fields): 47def _NamedTuple(name, fields):
34 Tuple = collections.namedtuple(name, " ".join(fields)) 48 Tuple = collections.namedtuple(name, " ".join(fields))
@@ -49,96 +63,84 @@ domain = _NamedTuple("Domain", (
49 "TaskData", 63 "TaskData",
50 "Util")) 64 "Util"))
51 65
52 66logger = logging.getLogger("BitBake")
53class MsgBase(bb.event.Event): 67loggers = Loggers()
54 """Base class for messages""" 68debug_level = DebugLevel()
55
56 def __init__(self, msg):
57 self._message = msg
58 bb.event.Event.__init__(self)
59
60class MsgDebug(MsgBase):
61 """Debug Message"""
62
63class MsgNote(MsgBase):
64 """Note Message"""
65
66class MsgWarn(MsgBase):
67 """Warning Message"""
68
69class MsgError(MsgBase):
70 """Error Message"""
71
72class MsgFatal(MsgBase):
73 """Fatal Message"""
74
75class MsgPlain(MsgBase):
76 """General output"""
77 69
78# 70#
79# Message control functions 71# Message control functions
80# 72#
81 73
82def set_debug_level(level): 74def set_debug_level(level):
83 for d in domain: 75 for log in loggers.itervalues():
84 debug_level[d] = level 76 log.setLevel(logging.NOTSET)
85 debug_level[domain.Default] = level 77
78 if level:
79 logger.setLevel(logging.DEBUG - level + 1)
80 else:
81 logger.setLevel(logging.INFO)
86 82
87def get_debug_level(msgdomain = domain.Default): 83def get_debug_level(msgdomain = domain.Default):
88 return debug_level[msgdomain] 84 if not msgdomain:
85 level = logger.getEffectiveLevel()
86 else:
87 level = loggers[msgdomain].getEffectiveLevel()
88 return max(0, logging.DEBUG - level + 1)
89 89
90def set_verbose(level): 90def set_verbose(level):
91 verbose = level 91 if level:
92 92 logger.setLevel(logging.INFO - 1)
93def set_debug_domains(strdomains): 93 else:
94 for domainstr in strdomains: 94 logger.setLevel(logging.INFO)
95 for d in domain: 95
96 if domain._fields[d] == domainstr: 96def set_debug_domains(domainargs):
97 debug_level[d] += 1 97 for (domainarg, iterator) in groupby(domainargs):
98 for index, msgdomain in enumerate(domain._fields):
99 if msgdomain == domainarg:
100 level = len(tuple(iterator))
101 if level:
102 loggers[index].setLevel(logging.DEBUG - level + 1)
98 break 103 break
99 else: 104 else:
100 warn(None, "Logging domain %s is not valid, ignoring" % domainstr) 105 warn(None, "Logging domain %s is not valid, ignoring" % domainarg)
101 106
102# 107#
103# Message handling functions 108# Message handling functions
104# 109#
105 110
106def debug(level, msgdomain, msg, fn = None): 111def debug(level, msgdomain, msg, fn = None):
112 level = logging.DEBUG - (level - 1)
107 if not msgdomain: 113 if not msgdomain:
108 msgdomain = domain.Default 114 logger.log(level, msg)
109 115 else:
110 if debug_level[msgdomain] >= level: 116 loggers[msgdomain].log(level, msg)
111 bb.event.fire(MsgDebug(msg), None)
112 if bb.event.useStdout:
113 print('DEBUG: %s' % (msg))
114 117
118def plain(msg, fn = None):
119 logger.log(logging.INFO + 1, msg)
120
115def note(level, msgdomain, msg, fn = None): 121def note(level, msgdomain, msg, fn = None):
122 level = logging.INFO - (level - 1)
116 if not msgdomain: 123 if not msgdomain:
117 msgdomain = domain.Default 124 logger.log(level, msg)
118 125 else:
119 if level == 1 or verbose or debug_level[msgdomain] >= 1: 126 loggers[msgdomain].log(level, msg)
120 bb.event.fire(MsgNote(msg), None) 127
121 if bb.event.useStdout:
122 print('NOTE: %s' % (msg))
123
124def warn(msgdomain, msg, fn = None): 128def warn(msgdomain, msg, fn = None):
125 bb.event.fire(MsgWarn(msg), None) 129 if not msgdomain:
126 if bb.event.useStdout: 130 logger.warn(msg)
127 print('WARNING: %s' % (msg)) 131 else:
132 loggers[msgdomain].warn(msg)
128 133
129def error(msgdomain, msg, fn = None): 134def error(msgdomain, msg, fn = None):
130 bb.event.fire(MsgError(msg), None) 135 if not msgdomain:
131 if bb.event.useStdout: 136 logger.error(msg)
132 print('ERROR: %s' % (msg)) 137 else:
138 loggers[msgdomain].error(msg)
133 139
134def fatal(msgdomain, msg, fn = None): 140def fatal(msgdomain, msg, fn = None):
135 bb.event.fire(MsgFatal(msg), None) 141 if not msgdomain:
136 142 logger.critical(msg)
137 if bb.event.useStdout: 143 else:
138 print('FATAL: %s' % (msg)) 144 loggers[msgdomain].critical(msg)
139 sys.exit(1) 145 sys.exit(1)
140 146
141def plain(msg, fn = None):
142 bb.event.fire(MsgPlain(msg), None)
143 if bb.event.useStdout:
144 print(msg)
diff --git a/bitbake/lib/bb/ui/knotty.py b/bitbake/lib/bb/ui/knotty.py
index 858a00b568..9162c79f6a 100644
--- a/bitbake/lib/bb/ui/knotty.py
+++ b/bitbake/lib/bb/ui/knotty.py
@@ -72,23 +72,23 @@ def init(server, eventHandler):
72 print("%s: %s (pid %s)" % (tasknum, activetasks[task]["title"], task)) 72 print("%s: %s (pid %s)" % (tasknum, activetasks[task]["title"], task))
73 tasknum = tasknum + 1 73 tasknum = tasknum + 1
74 74
75 if isinstance(event, bb.msg.MsgPlain): 75 if isinstance(event, bb.event.MsgPlain):
76 print(event._message) 76 print(event._message)
77 continue 77 continue
78 if isinstance(event, bb.msg.MsgDebug): 78 if isinstance(event, bb.event.MsgDebug):
79 print('DEBUG: ' + event._message) 79 print('DEBUG: ' + event._message)
80 continue 80 continue
81 if isinstance(event, bb.msg.MsgNote): 81 if isinstance(event, bb.event.MsgNote):
82 print('NOTE: ' + event._message) 82 print('NOTE: ' + event._message)
83 continue 83 continue
84 if isinstance(event, bb.msg.MsgWarn): 84 if isinstance(event, bb.event.MsgWarn):
85 print('WARNING: ' + event._message) 85 print('WARNING: ' + event._message)
86 continue 86 continue
87 if isinstance(event, bb.msg.MsgError): 87 if isinstance(event, bb.event.MsgError):
88 return_value = 1 88 return_value = 1
89 print('ERROR: ' + event._message) 89 print('ERROR: ' + event._message)
90 continue 90 continue
91 if isinstance(event, bb.msg.MsgFatal): 91 if isinstance(event, bb.event.MsgFatal):
92 return_value = 1 92 return_value = 1
93 print('FATAL: ' + event._message) 93 print('FATAL: ' + event._message)
94 continue 94 continue
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py
index 922b7256e3..8330c80b72 100644
--- a/bitbake/lib/bb/utils.py
+++ b/bitbake/lib/bb/utils.py
@@ -793,13 +793,12 @@ def init_logger(logger, verbose, debug, debug_domains):
793 Set verbosity and debug levels in the logger 793 Set verbosity and debug levels in the logger
794 """ 794 """
795 795
796 if verbose:
797 logger.set_verbose(True)
798
799 if debug: 796 if debug:
800 logger.set_debug_level(debug) 797 bb.msg.set_debug_level(debug)
798 elif verbose:
799 bb.msg.set_verbose(True)
801 else: 800 else:
802 logger.set_debug_level(0) 801 bb.msg.set_debug_level(0)
803 802
804 if debug_domains: 803 if debug_domains:
805 logger.set_debug_domains(debug_domains) 804 bb.msg.set_debug_domains(debug_domains)