3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
7 # Thomas Nagy, 2005-2018 (ita)
10 logging, colors, terminal width and pretty-print
13 import os, re, traceback, sys
14 from waflib import Utils, ansiterm
16 if not os.environ.get('NOSYNC', False):
17 # synchronized output is nearly mandatory to prevent garbled output
18 if sys.stdout.isatty() and id(sys.stdout) == id(sys.__stdout__):
19 sys.stdout = ansiterm.AnsiTerm(sys.stdout)
20 if sys.stderr.isatty() and id(sys.stderr) == id(sys.__stderr__):
21 sys.stderr = ansiterm.AnsiTerm(sys.stderr)
23 # import the logging module after since it holds a reference on sys.stderr
24 # in case someone uses the root logger
27 LOG_FORMAT = os.environ.get('WAF_LOG_FORMAT', '%(asctime)s %(c1)s%(zone)s%(c2)s %(message)s')
28 HOUR_FORMAT = os.environ.get('WAF_HOUR_FORMAT', '%H:%M:%S')
32 See :py:class:`waflib.Logs.log_filter`
37 Global verbosity level, see :py:func:`waflib.Logs.debug` and :py:func:`waflib.Logs.error`
47 'BLUE' :'\x1b[01;34m',
51 'cursor_on' :'\x1b[?25h',
52 'cursor_off' :'\x1b[?25l',
55 indicator = '\r\x1b[K%s%s%s'
62 def enable_colors(use):
64 If *1* is given, then the system will perform a few verifications
65 before enabling colors, such as checking whether the interpreter
66 is running in a terminal. A value of zero will disable colors,
67 and a value above *1* will force colors.
69 :param use: whether to enable colors or not
73 if not (sys.stderr.isatty() or sys.stdout.isatty()):
75 if Utils.is_win32 and os.name != 'java':
76 term = os.environ.get('TERM', '') # has ansiterm
78 term = os.environ.get('TERM', 'dumb')
80 if term in ('dumb', 'emacs'):
84 os.environ['TERM'] = 'vt100'
86 colors_lst['USE'] = use
88 # If console packages are available, replace the dummy function with a real
91 get_term_cols = ansiterm.get_term_cols
92 except AttributeError:
96 get_term_cols.__doc__ = """
97 Returns the console width in characters.
99 :return: the number of characters per line
105 Returns the ansi sequence corresponding to the given color name.
106 An empty string is returned when coloring is globally disabled.
108 :param cl: color name in capital letters
111 if colors_lst['USE']:
112 return colors_lst.get(cl, '')
115 class color_dict(object):
116 """attribute-based color access, eg: colors.PINK"""
117 def __getattr__(self, a):
119 def __call__(self, a):
122 colors = color_dict()
124 re_log = re.compile(r'(\w+): (.*)', re.M)
125 class log_filter(logging.Filter):
127 Waf logs are of the form 'name: message', and can be filtered by 'waf --zones=name'.
128 For example, the following::
130 from waflib import Logs
131 Logs.debug('test: here is a message')
133 Will be displayed only when executing::
137 def __init__(self, name=''):
138 logging.Filter.__init__(self, name)
140 def filter(self, rec):
142 Filters log records by zone and by logging level
144 :param rec: log entry
146 rec.zone = rec.module
147 if rec.levelno >= logging.INFO:
150 m = re_log.match(rec.msg)
152 rec.zone = m.group(1)
156 return getattr(rec, 'zone', '') in zones or '*' in zones
157 elif not verbose > 2:
161 class log_handler(logging.StreamHandler):
162 """Dispatches messages to stderr/stdout depending on the severity level"""
163 def emit(self, record):
165 Delegates the functionality to :py:meth:`waflib.Log.log_handler.emit_override`
167 # default implementation
170 self.stream = record.stream
171 except AttributeError:
172 if record.levelno >= logging.WARNING:
173 record.stream = self.stream = sys.stderr
175 record.stream = self.stream = sys.stdout
176 self.emit_override(record)
178 except (KeyboardInterrupt, SystemExit):
180 except: # from the python library -_-
181 self.handleError(record)
183 def emit_override(self, record, **kw):
185 Writes the log record to the desired stream (stderr/stdout)
187 self.terminator = getattr(record, 'terminator', '\n')
191 msg = self.formatter.format(record)
192 fs = '%s' + self.terminator
194 if (isinstance(msg, unicode) and getattr(stream, 'encoding', None)):
195 fs = fs.decode(stream.encoding)
197 stream.write(fs % msg)
198 except UnicodeEncodeError:
199 stream.write((fs % msg).encode(stream.encoding))
201 stream.write(fs % msg)
203 stream.write((fs % msg).encode('utf-8'))
205 logging.StreamHandler.emit(self, record)
207 class formatter(logging.Formatter):
208 """Simple log formatter which handles colors"""
210 logging.Formatter.__init__(self, LOG_FORMAT, HOUR_FORMAT)
212 def format(self, rec):
214 Formats records and adds colors as needed. The records do not get
215 a leading hour format if the logging level is above *INFO*.
218 msg = rec.msg.decode('utf-8')
222 use = colors_lst['USE']
223 if (use == 1 and rec.stream.isatty()) or use == 2:
225 c1 = getattr(rec, 'c1', None)
228 if rec.levelno >= logging.ERROR:
230 elif rec.levelno >= logging.WARNING:
232 elif rec.levelno >= logging.INFO:
234 c2 = getattr(rec, 'c2', colors.NORMAL)
235 msg = '%s%s%s' % (c1, msg, c2)
237 # remove single \r that make long lines in text files
238 # and other terminal commands
239 msg = re.sub(r'\r(?!\n)|\x1B\[(K|.*?(m|h|l))', '', msg)
241 if rec.levelno >= logging.INFO:
242 # the goal of this is to format without the leading "Logs, hour" prefix
244 return msg % rec.args
249 rec.c2 = colors.NORMAL
250 return logging.Formatter.format(self, rec)
253 """global logger for Logs.debug, Logs.error, etc"""
257 Wraps logging.debug and discards messages if the verbosity level :py:attr:`waflib.Logs.verbose` ≤ 0
261 k[0] = k[0].replace('\n', ' ')
266 Wrap logging.errors, adds the stack trace when the verbosity level :py:attr:`waflib.Logs.verbose` ≥ 2
270 st = traceback.extract_stack()
274 for filename, lineno, name, line in st:
275 buf.append(' File %r, line %d, in %s' % (filename, lineno, name))
277 buf.append(' %s' % line.strip())
279 log.error('\n'.join(buf))
295 Initializes the logger :py:attr:`waflib.Logs.log`
298 log = logging.getLogger('waflib')
302 hdlr.setFormatter(formatter())
304 log.addFilter(log_filter())
305 log.setLevel(logging.DEBUG)
307 def make_logger(path, name):
309 Creates a simple logger, which is often used to redirect the context command output::
311 from waflib import Logs
312 bld.logger = Logs.make_logger('test.log', 'build')
313 bld.check(header_name='sadlib.h', features='cxx cprogram', mandatory=False)
315 # have the file closed immediately
316 Logs.free_logger(bld.logger)
321 The method finalize() of the command will try to free the logger, if any
323 :param path: file name to write the log output to
325 :param name: logger name (loggers are reused)
328 logger = logging.getLogger(name)
329 if sys.hexversion > 0x3000000:
330 encoding = sys.stdout.encoding
333 hdlr = logging.FileHandler(path, 'w', encoding=encoding)
334 formatter = logging.Formatter('%(message)s')
335 hdlr.setFormatter(formatter)
336 logger.addHandler(hdlr)
337 logger.setLevel(logging.DEBUG)
340 def make_mem_logger(name, to_log, size=8192):
342 Creates a memory logger to avoid writing concurrently to the main logger
344 from logging.handlers import MemoryHandler
345 logger = logging.getLogger(name)
346 hdlr = MemoryHandler(size, target=to_log)
347 formatter = logging.Formatter('%(message)s')
348 hdlr.setFormatter(formatter)
349 logger.addHandler(hdlr)
350 logger.memhandler = hdlr
351 logger.setLevel(logging.DEBUG)
354 def free_logger(logger):
356 Frees the resources held by the loggers created through make_logger or make_mem_logger.
357 This is used for file cleanup and for handler removal (logger objects are re-used).
360 for x in logger.handlers:
362 logger.removeHandler(x)
366 def pprint(col, msg, label='', sep='\n'):
368 Prints messages in color immediately on stderr::
370 from waflib import Logs
371 Logs.pprint('RED', 'Something bad just happened')
373 :param col: color name to use in :py:const:`Logs.colors_lst`
375 :param msg: message to display
376 :type msg: string or a value that can be printed by %s
377 :param label: a message to add after the colored output
379 :param sep: a string to append at the end (line separator)
382 info('%s%s%s %s', colors(col), msg, colors.NORMAL, label, extra={'terminator':sep})