third_party:waf: update to upstream 2.0.4 release
[bbaumbach/samba-autobuild/.git] / third_party / waf / waflib / Logs.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
4
5 #!/usr/bin/env python
6 # encoding: utf-8
7 # Thomas Nagy, 2005-2018 (ita)
8
9 """
10 logging, colors, terminal width and pretty-print
11 """
12
13 import os, re, traceback, sys
14 from waflib import Utils, ansiterm
15
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)
22
23 # import the logging module after since it holds a reference on sys.stderr
24 # in case someone uses the root logger
25 import logging
26
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')
29
30 zones = []
31 """
32 See :py:class:`waflib.Logs.log_filter`
33 """
34
35 verbose = 0
36 """
37 Global verbosity level, see :py:func:`waflib.Logs.debug` and :py:func:`waflib.Logs.error`
38 """
39
40 colors_lst = {
41 'USE' : True,
42 'BOLD'  :'\x1b[01;1m',
43 'RED'   :'\x1b[01;31m',
44 'GREEN' :'\x1b[32m',
45 'YELLOW':'\x1b[33m',
46 'PINK'  :'\x1b[35m',
47 'BLUE'  :'\x1b[01;34m',
48 'CYAN'  :'\x1b[36m',
49 'GREY'  :'\x1b[37m',
50 'NORMAL':'\x1b[0m',
51 'cursor_on'  :'\x1b[?25h',
52 'cursor_off' :'\x1b[?25l',
53 }
54
55 indicator = '\r\x1b[K%s%s%s'
56
57 try:
58         unicode
59 except NameError:
60         unicode = None
61
62 def enable_colors(use):
63         """
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.
68
69         :param use: whether to enable colors or not
70         :type use: integer
71         """
72         if use == 1:
73                 if not (sys.stderr.isatty() or sys.stdout.isatty()):
74                         use = 0
75                 if Utils.is_win32 and os.name != 'java':
76                         term = os.environ.get('TERM', '') # has ansiterm
77                 else:
78                         term = os.environ.get('TERM', 'dumb')
79
80                 if term in ('dumb', 'emacs'):
81                         use = 0
82
83         if use >= 1:
84                 os.environ['TERM'] = 'vt100'
85
86         colors_lst['USE'] = use
87
88 # If console packages are available, replace the dummy function with a real
89 # implementation
90 try:
91         get_term_cols = ansiterm.get_term_cols
92 except AttributeError:
93         def get_term_cols():
94                 return 80
95
96 get_term_cols.__doc__ = """
97         Returns the console width in characters.
98
99         :return: the number of characters per line
100         :rtype: int
101         """
102
103 def get_color(cl):
104         """
105         Returns the ansi sequence corresponding to the given color name.
106         An empty string is returned when coloring is globally disabled.
107
108         :param cl: color name in capital letters
109         :type cl: string
110         """
111         if colors_lst['USE']:
112                 return colors_lst.get(cl, '')
113         return ''
114
115 class color_dict(object):
116         """attribute-based color access, eg: colors.PINK"""
117         def __getattr__(self, a):
118                 return get_color(a)
119         def __call__(self, a):
120                 return get_color(a)
121
122 colors = color_dict()
123
124 re_log = re.compile(r'(\w+): (.*)', re.M)
125 class log_filter(logging.Filter):
126         """
127         Waf logs are of the form 'name: message', and can be filtered by 'waf --zones=name'.
128         For example, the following::
129
130                 from waflib import Logs
131                 Logs.debug('test: here is a message')
132
133         Will be displayed only when executing::
134
135                 $ waf --zones=test
136         """
137         def __init__(self, name=''):
138                 logging.Filter.__init__(self, name)
139
140         def filter(self, rec):
141                 """
142                 Filters log records by zone and by logging level
143
144                 :param rec: log entry
145                 """
146                 rec.zone = rec.module
147                 if rec.levelno >= logging.INFO:
148                         return True
149
150                 m = re_log.match(rec.msg)
151                 if m:
152                         rec.zone = m.group(1)
153                         rec.msg = m.group(2)
154
155                 if zones:
156                         return getattr(rec, 'zone', '') in zones or '*' in zones
157                 elif not verbose > 2:
158                         return False
159                 return True
160
161 class log_handler(logging.StreamHandler):
162         """Dispatches messages to stderr/stdout depending on the severity level"""
163         def emit(self, record):
164                 """
165                 Delegates the functionality to :py:meth:`waflib.Log.log_handler.emit_override`
166                 """
167                 # default implementation
168                 try:
169                         try:
170                                 self.stream = record.stream
171                         except AttributeError:
172                                 if record.levelno >= logging.WARNING:
173                                         record.stream = self.stream = sys.stderr
174                                 else:
175                                         record.stream = self.stream = sys.stdout
176                         self.emit_override(record)
177                         self.flush()
178                 except (KeyboardInterrupt, SystemExit):
179                         raise
180                 except: # from the python library -_-
181                         self.handleError(record)
182
183         def emit_override(self, record, **kw):
184                 """
185                 Writes the log record to the desired stream (stderr/stdout)
186                 """
187                 self.terminator = getattr(record, 'terminator', '\n')
188                 stream = self.stream
189                 if unicode:
190                         # python2
191                         msg = self.formatter.format(record)
192                         fs = '%s' + self.terminator
193                         try:
194                                 if (isinstance(msg, unicode) and getattr(stream, 'encoding', None)):
195                                         fs = fs.decode(stream.encoding)
196                                         try:
197                                                 stream.write(fs % msg)
198                                         except UnicodeEncodeError:
199                                                 stream.write((fs % msg).encode(stream.encoding))
200                                 else:
201                                         stream.write(fs % msg)
202                         except UnicodeError:
203                                 stream.write((fs % msg).encode('utf-8'))
204                 else:
205                         logging.StreamHandler.emit(self, record)
206
207 class formatter(logging.Formatter):
208         """Simple log formatter which handles colors"""
209         def __init__(self):
210                 logging.Formatter.__init__(self, LOG_FORMAT, HOUR_FORMAT)
211
212         def format(self, rec):
213                 """
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*.
216                 """
217                 try:
218                         msg = rec.msg.decode('utf-8')
219                 except Exception:
220                         msg = rec.msg
221
222                 use = colors_lst['USE']
223                 if (use == 1 and rec.stream.isatty()) or use == 2:
224
225                         c1 = getattr(rec, 'c1', None)
226                         if c1 is None:
227                                 c1 = ''
228                                 if rec.levelno >= logging.ERROR:
229                                         c1 = colors.RED
230                                 elif rec.levelno >= logging.WARNING:
231                                         c1 = colors.YELLOW
232                                 elif rec.levelno >= logging.INFO:
233                                         c1 = colors.GREEN
234                         c2 = getattr(rec, 'c2', colors.NORMAL)
235                         msg = '%s%s%s' % (c1, msg, c2)
236                 else:
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)
240
241                 if rec.levelno >= logging.INFO:
242                         # the goal of this is to format without the leading "Logs, hour" prefix
243                         if rec.args:
244                                 return msg % rec.args
245                         return msg
246
247                 rec.msg = msg
248                 rec.c1 = colors.PINK
249                 rec.c2 = colors.NORMAL
250                 return logging.Formatter.format(self, rec)
251
252 log = None
253 """global logger for Logs.debug, Logs.error, etc"""
254
255 def debug(*k, **kw):
256         """
257         Wraps logging.debug and discards messages if the verbosity level :py:attr:`waflib.Logs.verbose` ≤ 0
258         """
259         if verbose:
260                 k = list(k)
261                 k[0] = k[0].replace('\n', ' ')
262                 log.debug(*k, **kw)
263
264 def error(*k, **kw):
265         """
266         Wrap logging.errors, adds the stack trace when the verbosity level :py:attr:`waflib.Logs.verbose` ≥ 2
267         """
268         log.error(*k, **kw)
269         if verbose > 2:
270                 st = traceback.extract_stack()
271                 if st:
272                         st = st[:-1]
273                         buf = []
274                         for filename, lineno, name, line in st:
275                                 buf.append('  File %r, line %d, in %s' % (filename, lineno, name))
276                                 if line:
277                                         buf.append('    %s' % line.strip())
278                         if buf:
279                                 log.error('\n'.join(buf))
280
281 def warn(*k, **kw):
282         """
283         Wraps logging.warn
284         """
285         log.warn(*k, **kw)
286
287 def info(*k, **kw):
288         """
289         Wraps logging.info
290         """
291         log.info(*k, **kw)
292
293 def init_log():
294         """
295         Initializes the logger :py:attr:`waflib.Logs.log`
296         """
297         global log
298         log = logging.getLogger('waflib')
299         log.handlers = []
300         log.filters = []
301         hdlr = log_handler()
302         hdlr.setFormatter(formatter())
303         log.addHandler(hdlr)
304         log.addFilter(log_filter())
305         log.setLevel(logging.DEBUG)
306
307 def make_logger(path, name):
308         """
309         Creates a simple logger, which is often used to redirect the context command output::
310
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)
314
315                 # have the file closed immediately
316                 Logs.free_logger(bld.logger)
317
318                 # stop logging
319                 bld.logger = None
320
321         The method finalize() of the command will try to free the logger, if any
322
323         :param path: file name to write the log output to
324         :type path: string
325         :param name: logger name (loggers are reused)
326         :type name: string
327         """
328         logger = logging.getLogger(name)
329         if sys.hexversion > 0x3000000:
330                 encoding = sys.stdout.encoding
331         else:
332                 encoding = None
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)
338         return logger
339
340 def make_mem_logger(name, to_log, size=8192):
341         """
342         Creates a memory logger to avoid writing concurrently to the main logger
343         """
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)
352         return logger
353
354 def free_logger(logger):
355         """
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).
358         """
359         try:
360                 for x in logger.handlers:
361                         x.close()
362                         logger.removeHandler(x)
363         except Exception:
364                 pass
365
366 def pprint(col, msg, label='', sep='\n'):
367         """
368         Prints messages in color immediately on stderr::
369
370                 from waflib import Logs
371                 Logs.pprint('RED', 'Something bad just happened')
372
373         :param col: color name to use in :py:const:`Logs.colors_lst`
374         :type col: string
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
378         :type label: string
379         :param sep: a string to append at the end (line separator)
380         :type sep: string
381         """
382         info('%s%s%s %s', colors(col), msg, colors.NORMAL, label, extra={'terminator':sep})
383