Exemple #1
0
    def _pantsd_logging(self) -> Iterator[IO[str]]:
        """A context manager that runs with pantsd logging.

        Asserts that stdio (represented by file handles 0, 1, 2) is closed to ensure that we can
        safely reuse those fd numbers.
        """

        # Ensure that stdio is closed so that we can safely reuse those file descriptors.
        for fd in (0, 1, 2):
            try:
                os.fdopen(fd)
                raise AssertionError(
                    f"pantsd logging cannot initialize while stdio is open: {fd}"
                )
            except OSError:
                pass

        # Redirect stdio to /dev/null for the rest of the run, to reserve those file descriptors
        # for further forks.
        with stdio_as(stdin_fd=-1, stdout_fd=-1, stderr_fd=-1):
            # Reinitialize logging for the daemon context.
            init_rust_logger(self._log_level, self._log_show_rust_3rdparty)
            # We can't statically prove it, but we won't execute `launch()` (which
            # calls `run_sync` which calls `_pantsd_logging`) unless PantsDaemon
            # is launched with full_init=True. If PantsdDaemon is launched with
            # full_init=True, we can guarantee self._native and self._bootstrap_options
            # are non-None.
            native = cast(Native, self._native)
            bootstrap_options = cast(OptionValueContainer,
                                     self._bootstrap_options)

            level = self._log_level
            ignores = bootstrap_options.for_global_scope(
            ).ignore_pants_warnings
            clear_logging_handlers()
            log_dir = os.path.join(self._work_dir, self.name)
            log_handler = setup_logging_to_file(
                level,
                log_dir=log_dir,
                log_filename=self.LOG_NAME,
                warnings_filter_regexes=ignores)

            native.override_thread_logging_destination_to_just_pantsd()

            # Do a python-level redirect of stdout/stderr, which will not disturb `0,1,2`.
            # TODO: Consider giving these pipes/actual fds, in order to make them "deep" replacements
            # for `1,2`, and allow them to be used via `stdio_as`.
            sys.stdout = _LoggerStream(logging.getLogger(), logging.INFO,
                                       log_handler)  # type: ignore[assignment]
            sys.stderr = _LoggerStream(logging.getLogger(), logging.WARN,
                                       log_handler)  # type: ignore[assignment]

            self._logger.debug("logging initialized")
            yield log_handler.stream
Exemple #2
0
    def _stderr_logging(self, global_bootstrap_options):
        """Temporarily replaces existing handlers (ie, the pantsd handler) with a stderr handler.

        In the context of pantsd, there will be an existing handler for the pantsd log, which we
        temporarily replace. Making them additive would cause per-run logs to go to pantsd, which
        we don't want.

        TODO: It would be good to handle logging destinations entirely via the threadlocal state
        rather than via handler mutations.
        """
        handlers = get_logging_handlers()
        try:
            clear_logging_handlers()
            Native().override_thread_logging_destination_to_just_stderr()
            setup_logging(global_bootstrap_options, stderr_logging=True)
            yield
        finally:
            Native().override_thread_logging_destination_to_just_pantsd()
            set_logging_handlers(handlers)