3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
7 # Thomas Nagy, 2005-2016 (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
147 rec.zone = rec.module
148 if rec.levelno >= logging.INFO:
151 m = re_log.match(rec.msg)
153 rec.zone = m.group(1)
157 return getattr(rec, 'zone', '') in zones or '*' in zones
158 elif not verbose > 2:
162 class log_handler(logging.StreamHandler):
163 """Dispatches messages to stderr/stdout depending on the severity level"""
164 def emit(self, record):
166 Delegates the functionality to :py:meth:`waflib.Log.log_handler.emit_override`
168 # default implementation
171 self.stream = record.stream
172 except AttributeError:
173 if record.levelno >= logging.WARNING:
174 record.stream = self.stream = sys.stderr
176 record.stream = self.stream = sys.stdout
177 self.emit_override(record)
179 except (KeyboardInterrupt, SystemExit):
181 except: # from the python library -_-
182 self.handleError(record)
184 def emit_override(self, record, **kw):
186 Writes the log record to the desired stream (stderr/stdout)
188 self.terminator = getattr(record, 'terminator', '\n')
192 msg = self.formatter.format(record)
193 fs = '%s' + self.terminator
195 if (isinstance(msg, unicode) and getattr(stream, 'encoding', None)):
196 fs = fs.decode(stream.encoding)
198 stream.write(fs % msg)
199 except UnicodeEncodeError:
200 stream.write((fs % msg).encode(stream.encoding))
202 stream.write(fs % msg)
204 stream.write((fs % msg).encode('utf-8'))
206 logging.StreamHandler.emit(self, record)
208 class formatter(logging.Formatter):
209 """Simple log formatter which handles colors"""
211 logging.Formatter.__init__(self, LOG_FORMAT, HOUR_FORMAT)
213 def format(self, rec):
215 Formats records and adds colors as needed. The records do not get
216 a leading hour format if the logging level is above *INFO*.
219 msg = rec.msg.decode('utf-8')
223 use = colors_lst['USE']
224 if (use == 1 and rec.stream.isatty()) or use == 2:
226 c1 = getattr(rec, 'c1', None)
229 if rec.levelno >= logging.ERROR:
231 elif rec.levelno >= logging.WARNING:
233 elif rec.levelno >= logging.INFO:
235 c2 = getattr(rec, 'c2', colors.NORMAL)
236 msg = '%s%s%s' % (c1, msg, c2)
238 # remove single \r that make long lines in text files
239 # and other terminal commands
240 msg = re.sub(r'\r(?!\n)|\x1B\[(K|.*?(m|h|l))', '', msg)
242 if rec.levelno >= logging.INFO:
243 # the goal of this is to format without the leading "Logs, hour" prefix
245 return msg % rec.args
250 rec.c2 = colors.NORMAL
251 return logging.Formatter.format(self, rec)
254 """global logger for Logs.debug, Logs.error, etc"""
258 Wraps logging.debug and discards messages if the verbosity level :py:attr:`waflib.Logs.verbose` ≤ 0
263 k[0] = k[0].replace('\n', ' ')
269 Wrap logging.errors, adds the stack trace when the verbosity level :py:attr:`waflib.Logs.verbose` ≥ 2
274 st = traceback.extract_stack()
278 for filename, lineno, name, line in st:
279 buf.append(' File %r, line %d, in %s' % (filename, lineno, name))
281 buf.append(' %s' % line.strip())
282 if buf: log.error('\n'.join(buf))
300 Initializes the logger :py:attr:`waflib.Logs.log`
303 log = logging.getLogger('waflib')
307 hdlr.setFormatter(formatter())
309 log.addFilter(log_filter())
310 log.setLevel(logging.DEBUG)
312 def make_logger(path, name):
314 Creates a simple logger, which is often used to redirect the context command output::
316 from waflib import Logs
317 bld.logger = Logs.make_logger('test.log', 'build')
318 bld.check(header_name='sadlib.h', features='cxx cprogram', mandatory=False)
320 # have the file closed immediately
321 Logs.free_logger(bld.logger)
326 The method finalize() of the command will try to free the logger, if any
328 :param path: file name to write the log output to
330 :param name: logger name (loggers are reused)
333 logger = logging.getLogger(name)
334 hdlr = logging.FileHandler(path, 'w')
335 formatter = logging.Formatter('%(message)s')
336 hdlr.setFormatter(formatter)
337 logger.addHandler(hdlr)
338 logger.setLevel(logging.DEBUG)
341 def make_mem_logger(name, to_log, size=8192):
343 Creates a memory logger to avoid writing concurrently to the main logger
345 from logging.handlers import MemoryHandler
346 logger = logging.getLogger(name)
347 hdlr = MemoryHandler(size, target=to_log)
348 formatter = logging.Formatter('%(message)s')
349 hdlr.setFormatter(formatter)
350 logger.addHandler(hdlr)
351 logger.memhandler = hdlr
352 logger.setLevel(logging.DEBUG)
355 def free_logger(logger):
357 Frees the resources held by the loggers created through make_logger or make_mem_logger.
358 This is used for file cleanup and for handler removal (logger objects are re-used).
361 for x in logger.handlers:
363 logger.removeHandler(x)
367 def pprint(col, msg, label='', sep='\n'):
369 Prints messages in color immediately on stderr::
371 from waflib import Logs
372 Logs.pprint('RED', 'Something bad just happened')
374 :param col: color name to use in :py:const:`Logs.colors_lst`
376 :param msg: message to display
377 :type msg: string or a value that can be printed by %s
378 :param label: a message to add after the colored output
380 :param sep: a string to append at the end (line separator)
384 info('%s%s%s %s', colors(col), msg, colors.NORMAL, label, extra={'terminator':sep})