Debian Codegraph @

subprojects/meson-docs/mesonbuild/mlog.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2013-2014 The Meson development team
3
4 """This is (mostly) a standalone module used to write logging
5 information about Meson runs. Some output goes to screen,
6 some to logging dir and some goes to both."""
7
8 from __future__ import annotations
9
10 import enum
11 import os
12 import io
13 import sys
14 import time
15 import platform
16 import shlex
17 import subprocess
18 import shutil
19 import typing as T
20 from contextlib import contextmanager
21 from dataclasses import dataclass, field
22 from pathlib import Path
23
24 if T.TYPE_CHECKING:
25 from ._typing import StringProtocol, SizedStringProtocol
26
27 from .mparser import BaseNode
28
29 TV_Loggable = T.Union[str, 'AnsiDecorator', StringProtocol]
30 TV_LoggableList = T.List[TV_Loggable]
31
32 def is_windows() -> bool:
33 platname = platform.system().lower()
34 return platname == 'windows'
35
36 def _windows_ansi() -> bool:
37 # windll only exists on windows, so mypy will get mad
38 from ctypes import windll, byref # type: ignore
39 from ctypes.wintypes import DWORD
40
41 kernel = windll.kernel32
42 stdout = kernel.GetStdHandle(-11)
43 mode = DWORD()
44 if not kernel.GetConsoleMode(stdout, byref(mode)):
45 return False
46 # ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0x4
47 # If the call to enable VT processing fails (returns 0), we fallback to
48 # original behavior
49 return bool(kernel.SetConsoleMode(stdout, mode.value | 0x4) or os.environ.get('ANSICON'))
50
51 def colorize_console() -> bool:
52 _colorize_console: bool = getattr(sys.stdout, 'colorize_console', None)
53 if _colorize_console is not None:
54 return _colorize_console
55
56 try:
57 if is_windows():
58 _colorize_console = os.isatty(sys.stdout.fileno()) and _windows_ansi()
59 else:
60 _colorize_console = os.isatty(sys.stdout.fileno()) and os.environ.get('TERM', 'dumb') != 'dumb'
61 except Exception:
62 _colorize_console = False
63
64 sys.stdout.colorize_console = _colorize_console # type: ignore[attr-defined]
65 return _colorize_console
66
67 def setup_console() -> None:
68 # on Windows, a subprocess might call SetConsoleMode() on the console
69 # connected to stdout and turn off ANSI escape processing. Call this after
70 # running a subprocess to ensure we turn it on again.
71 if is_windows():
72 try:
73 delattr(sys.stdout, 'colorize_console')
74 except AttributeError:
75 pass
76
77 _in_ci = 'CI' in os.environ
78
79
80 class _Severity(enum.Enum):
81
82 NOTICE = enum.auto()
83 WARNING = enum.auto()
84 ERROR = enum.auto()
85 DEPRECATION = enum.auto()
86
87 @dataclass
88 class _Logger:
89
90 log_dir: T.Optional[str] = None
91 log_depth: T.List[str] = field(default_factory=list)
92 log_file: T.Optional[T.TextIO] = None
93 log_timestamp_start: T.Optional[float] = None
94 log_fatal_warnings = False
95 log_disable_stdout = False
96 log_errors_only = False
97 logged_once: T.Set[T.Tuple[str, ...]] = field(default_factory=set)
98 log_warnings_counter = 0
99 log_pager: T.Optional['subprocess.Popen'] = None
100
101 _LOG_FNAME: T.ClassVar[str] = 'meson-log.txt'
102
103 @contextmanager
104 def no_logging(self) -> T.Iterator[None]:
105 self.log_disable_stdout = True
106 try:
107 yield
108 finally:
109 self.log_disable_stdout = False
110
111 @contextmanager
112 def force_logging(self) -> T.Iterator[None]:
113 restore = self.log_disable_stdout
114 self.log_disable_stdout = False
115 try:
116 yield
117 finally:
118 self.log_disable_stdout = restore
119
120 def set_quiet(self) -> None:
121 self.log_errors_only = True
122
123 def set_verbose(self) -> None:
124 self.log_errors_only = False
125
126 def set_timestamp_start(self, start: float) -> None:
127 self.log_timestamp_start = start
128
129 def shutdown(self) -> T.Optional[str]:
130 if self.log_file is not None:
131 path = self.log_file.name
132 exception_around_goer = self.log_file
133 self.log_file = None
134 exception_around_goer.close()
135 return path
136 self.stop_pager()
137 return None
138
139 def start_pager(self) -> None:
140 if not colorize_console():
141 return
142 pager_cmd = []
143 if 'PAGER' in os.environ:
144 pager_cmd = shlex.split(os.environ['PAGER'])
145 else:
146 less = shutil.which('less')
147 if not less and is_windows():
148 git = shutil.which('git')
149 if git:
150 path = Path(git).parents[1] / 'usr' / 'bin'
151 less = shutil.which('less', path=str(path))
152 if less:
153 pager_cmd = [less]
154 if not pager_cmd:
155 return
156 try:
157 # Set 'LESS' environment variable, rather than arguments in
158 # pager_cmd, to also support the case where the user has 'PAGER'
159 # set to 'less'. Arguments set are:
160 # "R" : support color
161 # "X" : do not clear the screen when leaving the pager
162 # "F" : skip the pager if content fits into the screen
163 env = os.environ.copy()
164 if 'LESS' not in env:
165 env['LESS'] = 'RXF'
166 # Set "-c" for lv to support color
167 if 'LV' not in env:
168 env['LV'] = '-c'
169 self.log_pager = subprocess.Popen(pager_cmd, stdin=subprocess.PIPE,
170 text=True, encoding='utf-8', env=env)
171 except Exception as e:
172 # Ignore errors, unless it is a user defined pager.
173 if 'PAGER' in os.environ:
174 from .mesonlib import MesonException
175 raise MesonException(f'Failed to start pager: {str(e)}')
176
177 def stop_pager(self) -> None:
178 if self.log_pager:
179 try:
180 self.log_pager.stdin.flush()
181 self.log_pager.stdin.close()
182 except OSError:
183 pass
184 self.log_pager.wait()
185 self.log_pager = None
186
187 def initialize(self, logdir: str, fatal_warnings: bool = False) -> None:
188 self.log_dir = logdir
189 self.log_file = open(os.path.join(logdir, self._LOG_FNAME), 'w', encoding='utf-8')
190 self.log_fatal_warnings = fatal_warnings
191
192 def process_markup(self, args: T.Sequence[TV_Loggable], keep: bool, display_timestamp: bool = True) -> T.List[str]:
193 arr: T.List[str] = []
194 if self.log_timestamp_start is not None and display_timestamp:
195 arr = ['[{:.3f}]'.format(time.monotonic() - self.log_timestamp_start)]
196 for arg in args:
197 if arg is None:
198 continue
199 if isinstance(arg, str):
200 arr.append(arg)
201 elif isinstance(arg, AnsiDecorator):
202 arr.append(arg.get_text(keep))
203 else:
204 arr.append(str(arg))
205 return arr
206
207 def force_print(self, *args: str, nested: bool, sep: T.Optional[str] = None,
208 end: T.Optional[str] = None) -> None:
209 if self.log_disable_stdout:
210 return
211 iostr = io.StringIO()
212 print(*args, sep=sep, end=end, file=iostr)
213
214 raw = iostr.getvalue()
215 if self.log_depth:
216 prepend = self.log_depth[-1] + '| ' if nested else ''
217 lines = []
218 for l in raw.split('\n'):
219 l = l.strip()
220 lines.append(prepend + l if l else '')
221 raw = '\n'.join(lines)
222
223 # _Something_ is going to get printed.
224 try:
225 output = self.log_pager.stdin if self.log_pager else None
226 print(raw, end='', file=output)
227 except UnicodeEncodeError:
228 cleaned = raw.encode('ascii', 'replace').decode('ascii')
229 print(cleaned, end='')
230
231 def debug(self, *args: TV_Loggable, sep: T.Optional[str] = None,
232 end: T.Optional[str] = None, display_timestamp: bool = True) -> None:
233 arr = process_markup(args, False, display_timestamp)
234 if self.log_file is not None:
235 print(*arr, file=self.log_file, sep=sep, end=end)
236 self.log_file.flush()
237
238 def _log(self, *args: TV_Loggable, is_error: bool = False,
239 nested: bool = True, sep: T.Optional[str] = None,
240 end: T.Optional[str] = None, display_timestamp: bool = True) -> None:
241 arr = process_markup(args, False, display_timestamp)
242 if self.log_file is not None:
243 print(*arr, file=self.log_file, sep=sep, end=end)
244 self.log_file.flush()
245 if colorize_console():
246 arr = process_markup(args, True, display_timestamp)
247 if not self.log_errors_only or is_error:
248 force_print(*arr, nested=nested, sep=sep, end=end)
249
250 def _debug_log_cmd(self, cmd: str, args: T.List[str]) -> None:
251 if not _in_ci:
252 return
253 args = [f'"{x}"' for x in args] # Quote all args, just in case
254 self.debug('!meson_ci!/{} {}'.format(cmd, ' '.join(args)))
255
256 def cmd_ci_include(self, file: str) -> None:
257 self._debug_log_cmd('ci_include', [file])
258
259 def log(self, *args: TV_Loggable, is_error: bool = False,
260 once: bool = False, nested: bool = True,
261 sep: T.Optional[str] = None,
262 end: T.Optional[str] = None,
263 display_timestamp: bool = True) -> None:
264 if once:
265 self._log_once(*args, is_error=is_error, nested=nested, sep=sep, end=end, display_timestamp=display_timestamp)
266 else:
267 self._log(*args, is_error=is_error, nested=nested, sep=sep, end=end, display_timestamp=display_timestamp)
268
269 def log_timestamp(self, *args: TV_Loggable) -> None:
270 if self.log_timestamp_start:
271 self.log(*args)
272
273 def _log_once(self, *args: TV_Loggable, is_error: bool = False,
274 nested: bool = True, sep: T.Optional[str] = None,
275 end: T.Optional[str] = None, display_timestamp: bool = True) -> None:
276 """Log variant that only prints a given message one time per meson invocation.
277
278 This considers ansi decorated values by the values they wrap without
279 regard for the AnsiDecorator itself.
280 """
281 def to_str(x: TV_Loggable) -> str:
282 if isinstance(x, str):
283 return x
284 if isinstance(x, AnsiDecorator):
285 return x.text
286 return str(x)
287 t = tuple(to_str(a) for a in args)
288 if t in self.logged_once:
289 return
290 self.logged_once.add(t)
291 self._log(*args, is_error=is_error, nested=nested, sep=sep, end=end, display_timestamp=display_timestamp)
292
293 def _log_error(self, severity: _Severity, *rargs: TV_Loggable,
294 once: bool = False, fatal: bool = True,
295 location: T.Optional[BaseNode] = None,
296 nested: bool = True, sep: T.Optional[str] = None,
297 end: T.Optional[str] = None,
298 is_error: bool = True) -> None:
299 from .mesonlib import MesonException, relpath
300
301 # The typing requirements here are non-obvious. Lists are invariant,
302 # therefore T.List[A] and T.List[T.Union[A, B]] are not able to be joined
303 if severity is _Severity.NOTICE:
304 label: TV_LoggableList = [bold('NOTICE:')]
305 elif severity is _Severity.WARNING:
306 label = [yellow('WARNING:')]
307 elif severity is _Severity.ERROR:
308 label = [red('ERROR:')]
309 elif severity is _Severity.DEPRECATION:
310 label = [red('DEPRECATION:')]
311 # rargs is a tuple, not a list
312 args = label + list(rargs)
313
314 if location is not None:
315 location_file = relpath(location.filename, os.getcwd())
316 location_str = get_error_location_string(location_file, location.lineno)
317 # Unions are frankly awful, and we have to T.cast here to get mypy
318 # to understand that the list concatenation is safe
319 location_list = T.cast('TV_LoggableList', [location_str])
320 args = location_list + args
321
322 log(*args, once=once, nested=nested, sep=sep, end=end, is_error=is_error)
323
324 self.log_warnings_counter += 1
325
326 if self.log_fatal_warnings and fatal:
327 raise MesonException("Fatal warnings enabled, aborting")
328
329 def error(self, *args: TV_Loggable,
330 once: bool = False, fatal: bool = True,
331 location: T.Optional[BaseNode] = None,
332 nested: bool = True, sep: T.Optional[str] = None,
333 end: T.Optional[str] = None) -> None:
334 return self._log_error(_Severity.ERROR, *args, once=once, fatal=fatal, location=location,
335 nested=nested, sep=sep, end=end, is_error=True)
336
337 def warning(self, *args: TV_Loggable,
338 once: bool = False, fatal: bool = True,
339 location: T.Optional[BaseNode] = None,
340 nested: bool = True, sep: T.Optional[str] = None,
341 end: T.Optional[str] = None) -> None:
342 return self._log_error(_Severity.WARNING, *args, once=once, fatal=fatal, location=location,
343 nested=nested, sep=sep, end=end, is_error=True)
344
345 def deprecation(self, *args: TV_Loggable,
346 once: bool = False, fatal: bool = True,
347 location: T.Optional[BaseNode] = None,
348 nested: bool = True, sep: T.Optional[str] = None,
349 end: T.Optional[str] = None) -> None:
350 return self._log_error(_Severity.DEPRECATION, *args, once=once, fatal=fatal, location=location,
351 nested=nested, sep=sep, end=end, is_error=True)
352
353 def notice(self, *args: TV_Loggable,
354 once: bool = False, fatal: bool = True,
355 location: T.Optional[BaseNode] = None,
356 nested: bool = True, sep: T.Optional[str] = None,
357 end: T.Optional[str] = None) -> None:
358 return self._log_error(_Severity.NOTICE, *args, once=once, fatal=fatal, location=location,
359 nested=nested, sep=sep, end=end, is_error=False)
360
361 def exception(self, e: Exception, prefix: T.Optional[AnsiDecorator] = None) -> None:
362 if prefix is None:
363 prefix = red('ERROR:')
364 self.log()
365 args: T.List[T.Union[AnsiDecorator, str]] = []
366 if all(getattr(e, a, None) is not None for a in ['file', 'lineno', 'colno']):
367 # Mypy doesn't follow hasattr, and it's pretty easy to visually inspect
368 # that this is correct, so we'll just ignore it.
369 path = get_relative_path(Path(e.file), Path(os.getcwd())) # type: ignore
370 args.append(f'{path}:{e.lineno}:{e.colno}:') # type: ignore
371 if prefix:
372 args.append(prefix)
373 args.append(str(e))
374
375 with self.force_logging():
376 self.log(*args, is_error=True)
377
378 @contextmanager
379 def nested(self, name: str = '') -> T.Generator[None, None, None]:
380 self.log_depth.append(name)
381 try:
382 yield
383 finally:
384 self.log_depth.pop()
385
386 def get_log_dir(self) -> str:
387 return self.log_dir
388
389 def get_log_depth(self) -> int:
390 return len(self.log_depth)
391
392 @contextmanager
393 def nested_warnings(self) -> T.Iterator[None]:
394 old = self.log_warnings_counter
395 self.log_warnings_counter = 0
396 try:
397 yield
398 finally:
399 self.log_warnings_counter = old
400
401 def get_warning_count(self) -> int:
402 return self.log_warnings_counter
403
404 _logger = _Logger()
405 cmd_ci_include = _logger.cmd_ci_include
406 debug = _logger.debug
407 deprecation = _logger.deprecation
408 error = _logger.error
409 exception = _logger.exception
410 force_print = _logger.force_print
411 get_log_depth = _logger.get_log_depth
412 get_log_dir = _logger.get_log_dir
413 get_warning_count = _logger.get_warning_count
414 initialize = _logger.initialize
415 log = _logger.log
416 log_timestamp = _logger.log_timestamp
417 nested = _logger.nested
418 nested_warnings = _logger.nested_warnings
419 no_logging = _logger.no_logging
420 notice = _logger.notice
421 process_markup = _logger.process_markup
422 set_quiet = _logger.set_quiet
423 set_timestamp_start = _logger.set_timestamp_start
424 set_verbose = _logger.set_verbose
425 shutdown = _logger.shutdown
426 start_pager = _logger.start_pager
427 stop_pager = _logger.stop_pager
428 warning = _logger.warning
429
430 class AnsiDecorator:
431 plain_code = "\033[0m"
432
433 def __init__(self, text: str, code: str, quoted: bool = False):
434 self.text = text
435 self.code = code
436 self.quoted = quoted
437
438 def get_text(self, with_codes: bool) -> str:
439 text = self.text
440 if with_codes and self.code:
441 text = self.code + self.text + AnsiDecorator.plain_code
442 if self.quoted:
443 text = f'"{text}"'
444 return text
445
446 def __len__(self) -> int:
447 return len(self.text)
448
449 def __str__(self) -> str:
450 return self.get_text(colorize_console())
451
452 class AnsiText:
453 def __init__(self, *args: 'SizedStringProtocol'):
454 self.args = args
455
456 def __len__(self) -> int:
457 return sum(len(x) for x in self.args)
458
459 def __str__(self) -> str:
460 return ''.join(str(x) for x in self.args)
461
462
463 def bold(text: str, quoted: bool = False) -> AnsiDecorator:
464 return AnsiDecorator(text, "\033[1m", quoted=quoted)
465
466 def italic(text: str, quoted: bool = False) -> AnsiDecorator:
467 return AnsiDecorator(text, "\033[3m", quoted=quoted)
468
469 def plain(text: str) -> AnsiDecorator:
470 return AnsiDecorator(text, "")
471
472 def red(text: str) -> AnsiDecorator:
473 return AnsiDecorator(text, "\033[1;31m")
474
475 def green(text: str) -> AnsiDecorator:
476 return AnsiDecorator(text, "\033[1;32m")
477
478 def yellow(text: str) -> AnsiDecorator:
479 return AnsiDecorator(text, "\033[1;33m")
480
481 def blue(text: str) -> AnsiDecorator:
482 return AnsiDecorator(text, "\033[1;34m")
483
484 def cyan(text: str) -> AnsiDecorator:
485 return AnsiDecorator(text, "\033[1;36m")
486
487 def normal_red(text: str) -> AnsiDecorator:
488 return AnsiDecorator(text, "\033[31m")
489
490 def normal_green(text: str) -> AnsiDecorator:
491 return AnsiDecorator(text, "\033[32m")
492
493 def normal_yellow(text: str) -> AnsiDecorator:
494 return AnsiDecorator(text, "\033[33m")
495
496 def normal_blue(text: str) -> AnsiDecorator:
497 return AnsiDecorator(text, "\033[34m")
498
499 def normal_cyan(text: str) -> AnsiDecorator:
500 return AnsiDecorator(text, "\033[36m")
501
502 def get_error_location_string(fname: StringProtocol, lineno: int) -> str:
503 return f'{fname}:{lineno}:'
504
505 def get_relative_path(target: Path, current: Path) -> Path:
506 """Get the path to target from current"""
507 # Go up "current" until we find a common ancestor to target
508 acc = ['.']
509 for part in [current, *current.parents]:
510 try:
511 path = target.relative_to(part)
512 return Path(*acc, path)
513 except ValueError:
514 pass
515 acc += ['..']
516
517 # we failed, should not get here
518 return target
519
520 # Format a list for logging purposes as a string. It separates
521 # all but the last item with commas, and the last with 'and'.
522 def format_list(input_list: T.List[str]) -> str:
523 l = len(input_list)
524 if l > 2:
525 return ' and '.join([', '.join(input_list[:-1]), input_list[-1]])
526 elif l == 2:
527 return ' and '.join(input_list)
528 elif l == 1:
529 return input_list[0]
530 else:
531 return ''
532
533
534 def code_line(text: str, line: str, colno: int) -> str:
535 """Print a line with a caret pointing to the colno
536
537 :param text: A message to display before the line
538 :param line: The line of code to be pointed to
539 :param colno: The column number to point at
540 :return: A formatted string of the text, line, and a caret
541 """
542 return f'{text}\n{line}\n{" " * colno}^'