2019-12-31 14:36:56 +08:00
|
|
|
# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
|
2018-10-08 04:52:23 +08:00
|
|
|
# Spack Project Developers. See the top-level COPYRIGHT file for details.
|
2015-05-30 08:20:08 +08:00
|
|
|
#
|
2018-10-08 04:52:23 +08:00
|
|
|
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
|
|
|
|
2015-05-30 08:20:08 +08:00
|
|
|
"""Utility classes for logging the output of blocks of code.
|
|
|
|
"""
|
2019-06-08 00:57:26 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
import atexit
|
|
|
|
import errno
|
2016-08-11 15:08:00 +08:00
|
|
|
import multiprocessing
|
2015-05-30 08:20:08 +08:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import select
|
2016-08-11 15:08:00 +08:00
|
|
|
import sys
|
2017-08-20 15:12:05 +08:00
|
|
|
import traceback
|
2020-03-21 03:22:32 +08:00
|
|
|
import signal
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
from contextlib import contextmanager
|
2017-08-23 04:33:31 +08:00
|
|
|
from six import string_types
|
|
|
|
from six import StringIO
|
2015-06-21 11:20:28 +08:00
|
|
|
|
2015-05-30 08:20:08 +08:00
|
|
|
import llnl.util.tty as tty
|
|
|
|
|
2020-03-21 03:22:32 +08:00
|
|
|
try:
|
|
|
|
import termios
|
|
|
|
except ImportError:
|
|
|
|
termios = None
|
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
|
2015-05-30 08:20:08 +08:00
|
|
|
# Use this to strip escape sequences
|
|
|
|
_escape = re.compile(r'\x1b[^m]*m|\x1b\[?1034h')
|
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
# control characters for enabling/disabling echo
|
|
|
|
#
|
|
|
|
# We use control characters to ensure that echo enable/disable are inline
|
|
|
|
# with the other output. We always follow these with a newline to ensure
|
|
|
|
# one per line the following newline is ignored in output.
|
|
|
|
xon, xoff = '\x11\n', '\x13\n'
|
|
|
|
control = re.compile('(\x11\n|\x13\n)')
|
2016-08-10 04:23:53 +08:00
|
|
|
|
2017-08-20 15:12:05 +08:00
|
|
|
|
2020-03-21 03:22:32 +08:00
|
|
|
@contextmanager
|
2020-04-16 02:05:41 +08:00
|
|
|
def ignore_signal(signum):
|
|
|
|
"""Context manager to temporarily ignore a signal."""
|
|
|
|
old_handler = signal.signal(signum, signal.SIG_IGN)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
signal.signal(signum, old_handler)
|
2020-03-21 03:22:32 +08:00
|
|
|
|
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
def _is_background_tty(stream):
|
|
|
|
"""True if the stream is a tty and calling process is in the background.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
stream.isatty() and
|
|
|
|
os.getpgrp() != os.tcgetpgrp(stream.fileno())
|
|
|
|
)
|
2020-03-21 03:22:32 +08:00
|
|
|
|
|
|
|
|
2015-05-30 08:20:08 +08:00
|
|
|
def _strip(line):
|
|
|
|
"""Strip color and control characters from a line."""
|
|
|
|
return _escape.sub('', line)
|
|
|
|
|
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
class keyboard_input(object):
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
"""Context manager to disable line editing and echoing.
|
2015-06-21 11:20:28 +08:00
|
|
|
|
2017-04-26 13:24:02 +08:00
|
|
|
Use this with ``sys.stdin`` for keyboard input, e.g.::
|
2015-06-21 11:20:28 +08:00
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
with keyboard_input(sys.stdin) as kb:
|
|
|
|
while True:
|
|
|
|
kb.check_fg_bg()
|
|
|
|
r, w, x = select.select([sys.stdin], [], [])
|
|
|
|
# ... do something with keypresses ...
|
|
|
|
|
|
|
|
The ``keyboard_input`` context manager disables canonical
|
|
|
|
(line-based) input and echoing, so that keypresses are available on
|
|
|
|
the stream immediately, and they are not printed to the
|
|
|
|
terminal. Typically, standard input is line-buffered, which means
|
|
|
|
keypresses won't be sent until the user hits return. In this mode, a
|
|
|
|
user can hit, e.g., 'v', and it will be read on the other end of the
|
|
|
|
pipe immediately but not printed.
|
|
|
|
|
|
|
|
The handler takes care to ensure that terminal changes only take
|
|
|
|
effect when the calling process is in the foreground. If the process
|
|
|
|
is backgrounded, canonical mode and echo are re-enabled. They are
|
|
|
|
disabled again when the calling process comes back to the foreground.
|
|
|
|
|
|
|
|
This context manager works through a single signal handler for
|
|
|
|
``SIGTSTP``, along with a poolling routine called ``check_fg_bg()``.
|
|
|
|
Here are the relevant states, transitions, and POSIX signals::
|
|
|
|
|
|
|
|
[Running] -------- Ctrl-Z sends SIGTSTP ------------.
|
|
|
|
[ in FG ] <------- fg sends SIGCONT --------------. |
|
|
|
|
^ | |
|
|
|
|
| fg (no signal) | |
|
|
|
|
| | v
|
|
|
|
[Running] <------- bg sends SIGCONT ---------- [Stopped]
|
|
|
|
[ in BG ] [ in BG ]
|
|
|
|
|
|
|
|
We handle all transitions exept for ``SIGTSTP`` generated by Ctrl-Z
|
|
|
|
by periodically calling ``check_fg_bg()``. This routine notices if
|
|
|
|
we are in the background with canonical mode or echo disabled, or if
|
|
|
|
we are in the foreground without canonical disabled and echo enabled,
|
|
|
|
and it fixes the terminal settings in response.
|
|
|
|
|
|
|
|
``check_fg_bg()`` works *except* for when the process is stopped with
|
|
|
|
``SIGTSTP``. We cannot rely on a periodic timer in this case, as it
|
|
|
|
may not rrun before the process stops. We therefore restore terminal
|
|
|
|
settings in the ``SIGTSTP`` handler.
|
|
|
|
|
|
|
|
Additional notes:
|
|
|
|
|
|
|
|
* We mostly use polling here instead of a SIGARLM timer or a
|
|
|
|
thread. This is to avoid the complexities of many interrupts, which
|
|
|
|
seem to make system calls (like I/O) unreliable in older Python
|
|
|
|
versions (2.6 and 2.7). See these issues for details:
|
|
|
|
|
|
|
|
1. https://www.python.org/dev/peps/pep-0475/
|
|
|
|
2. https://bugs.python.org/issue8354
|
|
|
|
|
|
|
|
There are essentially too many ways for asynchronous signals to go
|
|
|
|
wrong if we also have to support older Python versions, so we opt
|
|
|
|
not to use them.
|
|
|
|
|
|
|
|
* ``SIGSTOP`` can stop a process (in the foreground or background),
|
|
|
|
but it can't be caught. Because of this, we can't fix any terminal
|
|
|
|
settings on ``SIGSTOP``, and the terminal will be left with
|
|
|
|
``ICANON`` and ``ECHO`` disabled until it is resumes execution.
|
|
|
|
|
|
|
|
* Technically, a process *could* be sent ``SIGTSTP`` while running in
|
|
|
|
the foreground, without the shell backgrounding that process. This
|
|
|
|
doesn't happen in practice, and we assume that ``SIGTSTP`` always
|
|
|
|
means that defaults should be restored.
|
|
|
|
|
|
|
|
* We rely on ``termios`` support. Without it, or if the stream isn't
|
|
|
|
a TTY, ``keyboard_input`` has no effect.
|
2016-08-10 04:23:53 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
"""
|
2015-06-21 11:20:28 +08:00
|
|
|
def __init__(self, stream):
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
"""Create a context manager that will enable keyboard input on stream.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
stream (file-like): stream on which to accept keyboard input
|
2017-08-20 15:12:05 +08:00
|
|
|
|
|
|
|
Note that stream can be None, in which case ``keyboard_input``
|
|
|
|
will do nothing.
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
"""
|
2015-06-21 11:20:28 +08:00
|
|
|
self.stream = stream
|
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
def _is_background(self):
|
|
|
|
"""True iff calling process is in the background."""
|
|
|
|
return _is_background_tty(self.stream)
|
|
|
|
|
|
|
|
def _get_canon_echo_flags(self):
|
|
|
|
"""Get current termios canonical and echo settings."""
|
|
|
|
cfg = termios.tcgetattr(self.stream)
|
|
|
|
return (
|
|
|
|
bool(cfg[3] & termios.ICANON),
|
|
|
|
bool(cfg[3] & termios.ECHO),
|
|
|
|
)
|
|
|
|
|
|
|
|
def _enable_keyboard_input(self):
|
|
|
|
"""Disable canonical input and echoing on ``self.stream``."""
|
|
|
|
# "enable" input by disabling canonical mode and echo
|
|
|
|
new_cfg = termios.tcgetattr(self.stream)
|
|
|
|
new_cfg[3] &= ~termios.ICANON
|
|
|
|
new_cfg[3] &= ~termios.ECHO
|
|
|
|
|
|
|
|
# Apply new settings for terminal
|
|
|
|
with ignore_signal(signal.SIGTTOU):
|
|
|
|
termios.tcsetattr(self.stream, termios.TCSANOW, new_cfg)
|
|
|
|
|
|
|
|
def _restore_default_terminal_settings(self):
|
|
|
|
"""Restore the original input configuration on ``self.stream``."""
|
|
|
|
# _restore_default_terminal_settings Can be called in foreground
|
|
|
|
# or background. When called in the background, tcsetattr triggers
|
|
|
|
# SIGTTOU, which we must ignore, or the process will be stopped.
|
|
|
|
with ignore_signal(signal.SIGTTOU):
|
|
|
|
termios.tcsetattr(self.stream, termios.TCSANOW, self.old_cfg)
|
|
|
|
|
|
|
|
def _tstp_handler(self, signum, frame):
|
|
|
|
self._restore_default_terminal_settings()
|
|
|
|
os.kill(os.getpid(), signal.SIGSTOP)
|
|
|
|
|
|
|
|
def check_fg_bg(self):
|
|
|
|
# old_cfg is set up in __enter__ and indicates that we have
|
|
|
|
# termios and a valid stream.
|
|
|
|
if not self.old_cfg:
|
|
|
|
return
|
|
|
|
|
|
|
|
# query terminal flags and fg/bg status
|
|
|
|
flags = self._get_canon_echo_flags()
|
|
|
|
bg = self._is_background()
|
|
|
|
|
|
|
|
# restore sanity if flags are amiss -- see diagram in class docs
|
|
|
|
if not bg and any(flags): # fg, but input not enabled
|
|
|
|
self._enable_keyboard_input()
|
|
|
|
elif bg and not all(flags): # bg, but input enabled
|
|
|
|
self._restore_default_terminal_settings()
|
|
|
|
|
2015-06-21 11:20:28 +08:00
|
|
|
def __enter__(self):
|
2020-04-16 02:05:41 +08:00
|
|
|
"""Enable immediate keypress input, while this process is foreground.
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
|
|
|
|
If the stream is not a TTY or the system doesn't support termios,
|
|
|
|
do nothing.
|
|
|
|
"""
|
2015-06-21 11:20:28 +08:00
|
|
|
self.old_cfg = None
|
2020-04-16 02:05:41 +08:00
|
|
|
self.old_handlers = {}
|
2015-06-21 11:20:28 +08:00
|
|
|
|
|
|
|
# Ignore all this if the input stream is not a tty.
|
2017-08-20 15:12:05 +08:00
|
|
|
if not self.stream or not self.stream.isatty():
|
2020-04-16 02:05:41 +08:00
|
|
|
return self
|
2015-06-21 11:20:28 +08:00
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
if termios:
|
|
|
|
# save old termios settings to restore later
|
|
|
|
self.old_cfg = termios.tcgetattr(self.stream)
|
2015-06-21 11:20:28 +08:00
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
# Install a signal handler to disable/enable keyboard input
|
|
|
|
# when the process moves between foreground and background.
|
|
|
|
self.old_handlers[signal.SIGTSTP] = signal.signal(
|
|
|
|
signal.SIGTSTP, self._tstp_handler)
|
2015-06-21 11:20:28 +08:00
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
# add an atexit handler to ensure the terminal is restored
|
|
|
|
atexit.register(self._restore_default_terminal_settings)
|
2015-06-21 11:20:28 +08:00
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
# enable keyboard input initially (if foreground)
|
|
|
|
if not self._is_background():
|
|
|
|
self._enable_keyboard_input()
|
|
|
|
|
|
|
|
return self
|
2015-06-21 11:20:28 +08:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exception, traceback):
|
2020-04-16 02:05:41 +08:00
|
|
|
"""If termios was available, restore old settings."""
|
2015-06-21 11:20:28 +08:00
|
|
|
if self.old_cfg:
|
2020-04-16 02:05:41 +08:00
|
|
|
self._restore_default_terminal_settings()
|
|
|
|
|
|
|
|
# restore SIGSTP and SIGCONT handlers
|
|
|
|
if self.old_handlers:
|
|
|
|
for signum, old_handler in self.old_handlers.items():
|
|
|
|
signal.signal(signum, old_handler)
|
2015-06-21 11:20:28 +08:00
|
|
|
|
|
|
|
|
2017-08-20 18:29:54 +08:00
|
|
|
class Unbuffered(object):
|
|
|
|
"""Wrapper for Python streams that forces them to be unbuffered.
|
|
|
|
|
|
|
|
This is implemented by forcing a flush after each write.
|
|
|
|
"""
|
|
|
|
def __init__(self, stream):
|
|
|
|
self.stream = stream
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
self.stream.write(data)
|
|
|
|
self.stream.flush()
|
|
|
|
|
|
|
|
def writelines(self, datas):
|
|
|
|
self.stream.writelines(datas)
|
|
|
|
self.stream.flush()
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.stream, attr)
|
|
|
|
|
|
|
|
|
2017-08-23 04:33:31 +08:00
|
|
|
def _file_descriptors_work(*streams):
|
|
|
|
"""Whether we can get file descriptors for the streams specified.
|
2017-08-20 15:12:05 +08:00
|
|
|
|
2017-08-23 04:33:31 +08:00
|
|
|
This tries to call ``fileno()`` on all streams in the argument list,
|
2017-08-20 15:12:05 +08:00
|
|
|
and returns ``False`` if anything goes wrong.
|
|
|
|
|
|
|
|
This can happen, when, e.g., the test framework replaces stdout with
|
|
|
|
a ``StringIO`` object.
|
|
|
|
|
|
|
|
We have to actually try this to see whether it works, rather than
|
|
|
|
checking for the fileno attribute, beacuse frameworks like pytest add
|
|
|
|
dummy fileno methods on their dummy file objects that return
|
|
|
|
``UnsupportedOperationErrors``.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# test whether we can get fds for out and error
|
|
|
|
try:
|
2017-08-23 04:33:31 +08:00
|
|
|
for stream in streams:
|
|
|
|
stream.fileno()
|
2017-08-20 15:12:05 +08:00
|
|
|
return True
|
2017-10-23 22:51:11 +08:00
|
|
|
except BaseException:
|
2017-08-20 15:12:05 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-05-30 08:20:08 +08:00
|
|
|
class log_output(object):
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
"""Context manager that logs its output to a file.
|
|
|
|
|
|
|
|
In the simplest case, the usage looks like this::
|
|
|
|
|
|
|
|
with log_output('logfile.txt'):
|
|
|
|
# do things ... output will be logged
|
|
|
|
|
|
|
|
Any output from the with block will be redirected to ``logfile.txt``.
|
|
|
|
If you also want the output to be echoed to ``stdout``, use the
|
|
|
|
``echo`` parameter::
|
|
|
|
|
|
|
|
with log_output('logfile.txt', echo=True):
|
|
|
|
# do things ... output will be logged and printed out
|
2015-05-30 08:20:08 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
And, if you just want to echo *some* stuff from the parent, use
|
2017-08-20 15:12:05 +08:00
|
|
|
``force_echo``::
|
2017-04-26 13:24:02 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
with log_output('logfile.txt', echo=False) as logger:
|
|
|
|
# do things ... output will be logged
|
2015-05-30 08:20:08 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
with logger.force_echo():
|
|
|
|
# things here will be echoed *and* logged
|
2017-04-26 13:24:02 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
Under the hood, we spawn a daemon and set up a pipe between this
|
|
|
|
process and the daemon. The daemon writes our output to both the
|
|
|
|
file and to stdout (if echoing). The parent process can communicate
|
|
|
|
with the daemon to tell it when and when not to echo; this is what
|
|
|
|
force_echo does. You can also enable/disable echoing by typing 'v'.
|
2015-05-30 08:20:08 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
We try to use OS-level file descriptors to do the redirection, but if
|
|
|
|
stdout or stderr has been set to some Python-level file object, we
|
|
|
|
use Python-level redirection instead. This allows the redirection to
|
|
|
|
work within test frameworks like nose and pytest.
|
2015-05-30 08:20:08 +08:00
|
|
|
"""
|
2016-08-11 15:08:00 +08:00
|
|
|
|
2020-07-23 15:49:57 +08:00
|
|
|
def __init__(self, file_like=None, echo=False, debug=0, buffer=False):
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
"""Create a new output log context manager.
|
|
|
|
|
2017-08-20 18:29:54 +08:00
|
|
|
Args:
|
2017-08-23 04:33:31 +08:00
|
|
|
file_like (str or stream): open file object or name of file where
|
|
|
|
output should be logged
|
2017-08-20 18:29:54 +08:00
|
|
|
echo (bool): whether to echo output in addition to logging it
|
2020-07-23 15:49:57 +08:00
|
|
|
debug (int): positive to enable tty debug mode during logging
|
2017-08-20 18:29:54 +08:00
|
|
|
buffer (bool): pass buffer=True to skip unbuffering output; note
|
|
|
|
this doesn't set up any *new* buffering
|
|
|
|
|
2017-08-23 04:33:31 +08:00
|
|
|
log_output can take either a file object or a filename. If a
|
|
|
|
filename is passed, the file will be opened and closed entirely
|
|
|
|
within ``__enter__`` and ``__exit__``. If a file object is passed,
|
|
|
|
this assumes the caller owns it and will close it.
|
|
|
|
|
2017-08-20 18:29:54 +08:00
|
|
|
By default, we unbuffer sys.stdout and sys.stderr because the
|
|
|
|
logger will include output from executed programs and from python
|
|
|
|
calls. If stdout and stderr are buffered, their output won't be
|
|
|
|
printed in the right place w.r.t. output from commands.
|
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
Logger daemon is not started until ``__enter__()``.
|
2017-08-20 18:29:54 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
"""
|
2017-08-23 04:33:31 +08:00
|
|
|
self.file_like = file_like
|
2015-05-30 08:20:08 +08:00
|
|
|
self.echo = echo
|
|
|
|
self.debug = debug
|
2017-08-20 18:29:54 +08:00
|
|
|
self.buffer = buffer
|
2015-05-30 08:20:08 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
self._active = False # used to prevent re-entry
|
2016-07-14 02:49:16 +08:00
|
|
|
|
2017-08-23 04:33:31 +08:00
|
|
|
def __call__(self, file_like=None, echo=None, debug=None, buffer=None):
|
2020-07-23 15:49:57 +08:00
|
|
|
"""This behaves the same as init. It allows a logger to be reused.
|
2017-08-20 15:12:05 +08:00
|
|
|
|
2017-08-23 04:33:31 +08:00
|
|
|
Arguments are the same as for ``__init__()``. Args here take
|
|
|
|
precedence over those passed to ``__init__()``.
|
|
|
|
|
2017-08-20 15:12:05 +08:00
|
|
|
With the ``__call__`` function, you can save state between uses
|
|
|
|
of a single logger. This is useful if you want to remember,
|
|
|
|
e.g., the echo settings for a prior ``with log_output()``::
|
|
|
|
|
|
|
|
logger = log_output()
|
|
|
|
|
|
|
|
with logger('foo.txt'):
|
|
|
|
# log things; user can change echo settings with 'v'
|
|
|
|
|
|
|
|
with logger('bar.txt'):
|
|
|
|
# log things; logger remembers prior echo settings.
|
|
|
|
|
|
|
|
"""
|
2017-08-23 04:33:31 +08:00
|
|
|
if file_like is not None:
|
|
|
|
self.file_like = file_like
|
2017-08-20 15:12:05 +08:00
|
|
|
if echo is not None:
|
|
|
|
self.echo = echo
|
|
|
|
if debug is not None:
|
|
|
|
self.debug = debug
|
2017-08-20 18:29:54 +08:00
|
|
|
if buffer is not None:
|
|
|
|
self.buffer = buffer
|
2017-08-20 15:12:05 +08:00
|
|
|
return self
|
|
|
|
|
2016-07-14 15:34:01 +08:00
|
|
|
def __enter__(self):
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
if self._active:
|
|
|
|
raise RuntimeError("Can't re-enter the same log_output!")
|
|
|
|
|
2017-08-23 04:33:31 +08:00
|
|
|
if self.file_like is None:
|
2017-08-20 15:12:05 +08:00
|
|
|
raise RuntimeError(
|
2017-08-23 04:33:31 +08:00
|
|
|
"file argument must be set by either __init__ or __call__")
|
|
|
|
|
|
|
|
# set up a stream for the daemon to write to
|
|
|
|
self.close_log_in_parent = True
|
|
|
|
self.write_log_in_parent = False
|
|
|
|
if isinstance(self.file_like, string_types):
|
|
|
|
self.log_file = open(self.file_like, 'w')
|
|
|
|
|
|
|
|
elif _file_descriptors_work(self.file_like):
|
|
|
|
self.log_file = self.file_like
|
|
|
|
self.close_log_in_parent = False
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.log_file = StringIO()
|
|
|
|
self.write_log_in_parent = True
|
2017-08-20 15:12:05 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
# record parent color settings before redirecting. We do this
|
|
|
|
# because color output depends on whether the *original* stdout
|
|
|
|
# is a TTY. New stdout won't be a TTY so we force colorization.
|
|
|
|
self._saved_color = tty.color._force_color
|
|
|
|
forced_color = tty.color.get_color_when()
|
|
|
|
|
|
|
|
# also record parent debug settings -- in case the logger is
|
|
|
|
# forcing debug output.
|
|
|
|
self._saved_debug = tty._debug
|
|
|
|
|
|
|
|
# OS-level pipe for redirecting output to logger
|
2020-04-16 02:05:41 +08:00
|
|
|
read_fd, write_fd = os.pipe()
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
|
2017-08-20 15:12:05 +08:00
|
|
|
# Multiprocessing pipe for communication back from the daemon
|
|
|
|
# Currently only used to save echo value between uses
|
2020-04-16 02:05:41 +08:00
|
|
|
self.parent_pipe, child_pipe = multiprocessing.Pipe()
|
2017-08-20 15:12:05 +08:00
|
|
|
|
2016-11-03 23:03:10 +08:00
|
|
|
# Sets a daemon that writes to file what it reads from a pipe
|
|
|
|
try:
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
# need to pass this b/c multiprocessing closes stdin in child.
|
2017-08-20 15:12:05 +08:00
|
|
|
try:
|
|
|
|
input_stream = os.fdopen(os.dup(sys.stdin.fileno()))
|
2017-10-23 22:51:11 +08:00
|
|
|
except BaseException:
|
2017-08-20 15:12:05 +08:00
|
|
|
input_stream = None # just don't forward input if this fails
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
|
|
|
|
self.process = multiprocessing.Process(
|
2020-04-16 02:05:41 +08:00
|
|
|
target=_writer_daemon,
|
|
|
|
args=(
|
|
|
|
input_stream, read_fd, write_fd, self.echo, self.log_file,
|
|
|
|
child_pipe
|
|
|
|
)
|
|
|
|
)
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
self.process.daemon = True # must set before start()
|
|
|
|
self.process.start()
|
2020-04-16 02:05:41 +08:00
|
|
|
os.close(read_fd) # close in the parent process
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
|
2016-11-03 23:03:10 +08:00
|
|
|
finally:
|
2017-08-20 15:12:05 +08:00
|
|
|
if input_stream:
|
|
|
|
input_stream.close()
|
2015-05-30 08:20:08 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
# Flush immediately before redirecting so that anything buffered
|
|
|
|
# goes to the original stream
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
2016-07-14 02:49:16 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
# Now do the actual output rediction.
|
2017-08-23 04:33:31 +08:00
|
|
|
self.use_fds = _file_descriptors_work(sys.stdout, sys.stderr)
|
2017-08-20 15:12:05 +08:00
|
|
|
if self.use_fds:
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
# We try first to use OS-level file descriptors, as this
|
|
|
|
# redirects output for subprocesses and system calls.
|
|
|
|
|
|
|
|
# Save old stdout and stderr file descriptors
|
|
|
|
self._saved_stdout = os.dup(sys.stdout.fileno())
|
|
|
|
self._saved_stderr = os.dup(sys.stderr.fileno())
|
|
|
|
|
|
|
|
# redirect to the pipe we created above
|
2020-04-16 02:05:41 +08:00
|
|
|
os.dup2(write_fd, sys.stdout.fileno())
|
|
|
|
os.dup2(write_fd, sys.stderr.fileno())
|
|
|
|
os.close(write_fd)
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
|
2017-08-20 15:12:05 +08:00
|
|
|
else:
|
|
|
|
# Handle I/O the Python way. This won't redirect lower-level
|
|
|
|
# output, but it's the best we can do, and the caller
|
|
|
|
# shouldn't expect any better, since *they* have apparently
|
|
|
|
# redirected I/O the Python way.
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
|
|
|
|
# Save old stdout and stderr file objects
|
|
|
|
self._saved_stdout = sys.stdout
|
|
|
|
self._saved_stderr = sys.stderr
|
|
|
|
|
|
|
|
# create a file object for the pipe; redirect to it.
|
2020-04-16 02:05:41 +08:00
|
|
|
pipe_fd_out = os.fdopen(write_fd, 'w')
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
sys.stdout = pipe_fd_out
|
|
|
|
sys.stderr = pipe_fd_out
|
|
|
|
|
2017-08-20 18:29:54 +08:00
|
|
|
# Unbuffer stdout and stderr at the Python level
|
|
|
|
if not self.buffer:
|
|
|
|
sys.stdout = Unbuffered(sys.stdout)
|
|
|
|
sys.stderr = Unbuffered(sys.stderr)
|
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
# Force color and debug settings now that we have redirected.
|
|
|
|
tty.color.set_color_when(forced_color)
|
|
|
|
tty._debug = self.debug
|
|
|
|
|
|
|
|
# track whether we're currently inside this log_output
|
|
|
|
self._active = True
|
|
|
|
|
|
|
|
# return this log_output object so that the user can do things
|
|
|
|
# like temporarily echo some ouptut.
|
|
|
|
return self
|
2017-03-08 06:25:48 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
# Flush any buffered output to the logger daemon.
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
|
|
|
# restore previous output settings, either the low-level way or
|
|
|
|
# the python way
|
|
|
|
if self.use_fds:
|
|
|
|
os.dup2(self._saved_stdout, sys.stdout.fileno())
|
|
|
|
os.close(self._saved_stdout)
|
|
|
|
|
|
|
|
os.dup2(self._saved_stderr, sys.stderr.fileno())
|
|
|
|
os.close(self._saved_stderr)
|
|
|
|
else:
|
|
|
|
sys.stdout = self._saved_stdout
|
|
|
|
sys.stderr = self._saved_stderr
|
|
|
|
|
2017-08-23 04:33:31 +08:00
|
|
|
# print log contents in parent if needed.
|
|
|
|
if self.write_log_in_parent:
|
2020-04-16 02:05:41 +08:00
|
|
|
string = self.parent_pipe.recv()
|
2017-08-23 04:33:31 +08:00
|
|
|
self.file_like.write(string)
|
|
|
|
|
|
|
|
if self.close_log_in_parent:
|
|
|
|
self.log_file.close()
|
|
|
|
|
2017-08-20 15:12:05 +08:00
|
|
|
# recover and store echo settings from the child before it dies
|
2020-04-16 02:05:41 +08:00
|
|
|
self.echo = self.parent_pipe.recv()
|
2017-08-20 15:12:05 +08:00
|
|
|
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
# join the daemon process. The daemon will quit automatically
|
|
|
|
# when the write pipe is closed; we just wait for it here.
|
|
|
|
self.process.join()
|
|
|
|
|
|
|
|
# restore old color and debug settings
|
|
|
|
tty.color._force_color = self._saved_color
|
|
|
|
tty._debug = self._saved_debug
|
|
|
|
|
|
|
|
self._active = False # safe to enter again
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def force_echo(self):
|
|
|
|
"""Context manager to force local echo, even if echo is off."""
|
|
|
|
if not self._active:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Can't call force_echo() outside log_output region!")
|
|
|
|
|
|
|
|
# This uses the xon/xoff to highlight regions to be echoed in the
|
|
|
|
# output. We us these control characters rather than, say, a
|
|
|
|
# separate pipe, because they're in-band and assured to appear
|
|
|
|
# exactly before and after the text we want to echo.
|
|
|
|
sys.stdout.write(xon)
|
|
|
|
sys.stdout.flush()
|
2020-04-16 02:05:41 +08:00
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
sys.stdout.write(xoff)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
|
def _writer_daemon(stdin, read_fd, write_fd, echo, log_file, control_pipe):
|
|
|
|
"""Daemon used by ``log_output`` to write to a log file and to ``stdout``.
|
|
|
|
|
|
|
|
The daemon receives output from the parent process and writes it both
|
|
|
|
to a log and, optionally, to ``stdout``. The relationship looks like
|
|
|
|
this::
|
|
|
|
|
|
|
|
Terminal
|
|
|
|
|
|
|
|
|
| +-------------------------+
|
|
|
|
| | Parent Process |
|
|
|
|
+--------> | with log_output(): |
|
|
|
|
| stdin | ... |
|
|
|
|
| +-------------------------+
|
|
|
|
| ^ | write_fd (parent's redirected stdout)
|
|
|
|
| | control |
|
|
|
|
| | pipe |
|
|
|
|
| | v read_fd
|
|
|
|
| +-------------------------+ stdout
|
|
|
|
| | Writer daemon |------------>
|
|
|
|
+--------> | read from read_fd | log_file
|
|
|
|
stdin | write to out and log |------------>
|
|
|
|
+-------------------------+
|
|
|
|
|
|
|
|
Within the ``log_output`` handler, the parent's output is redirected
|
|
|
|
to a pipe from which the daemon reads. The daemon writes each line
|
|
|
|
from the pipe to a log file and (optionally) to ``stdout``. The user
|
|
|
|
can hit ``v`` to toggle output on ``stdout``.
|
|
|
|
|
|
|
|
In addition to the input and output file descriptors, the daemon
|
|
|
|
interacts with the parent via ``control_pipe``. It reports whether
|
|
|
|
``stdout`` was enabled or disabled when it finished and, if the
|
|
|
|
``log_file`` is a ``StringIO`` object, then the daemon also sends the
|
|
|
|
logged output back to the parent as a string, to be written to the
|
|
|
|
``StringIO`` in the parent. This is mainly for testing.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
stdin (stream): input from the terminal
|
|
|
|
read_fd (int): pipe for reading from parent's redirected stdout
|
|
|
|
write_fd (int): parent's end of the pipe will write to (will be
|
|
|
|
immediately closed by the writer daemon)
|
|
|
|
echo (bool): initial echo setting -- controlled by user and
|
|
|
|
preserved across multiple writer daemons
|
|
|
|
log_file (file-like): file to log all output
|
|
|
|
control_pipe (Pipe): multiprocessing pipe on which to send control
|
|
|
|
information to the parent
|
Rework output redirection in Spack.
- Simplify interface to log_output. New interface requires only one
context handler instead of two. Before:
with log_output('logfile.txt') as log_redirection:
with log_redirection:
# do things ... output will be logged
After:
with log_output('logfile.txt'):
# do things ... output will be logged
If you also want the output to be echoed to ``stdout``, use the
`echo` parameter::
with log_output('logfile.txt', echo=True):
# do things ... output will be logged and printed out
And, if you just want to echo *some* stuff from the parent, use
``force_echo``:
with log_output('logfile.txt', echo=False) as logger:
# do things ... output will be logged
with logger.force_echo():
# things here will be echoed *and* logged
A key difference between this and the previous implementation is that
*everything* in the context handler is logged. Previously, things like
`Executing phase 'configure'` would not be logged, only output to the
screen, so understanding phases in the build log was difficult.
- The implementation of `log_output()` is different in two major ways:
1. This implementation avoids race cases by using only one pipe (before
we had a multiprocessing pipe and a unix pipe). The logger daemon
stops naturally when the input stream is closed, which avoids a race
in the previous implementation where we'd miss some lines of output
because the parent would shut the daemon down before it was done
with all output.
2. Instead of turning output redirection on and off, which prevented
some things from being logged, this version uses control characters
in the output stream to enable/disable forced echoing. We're using
the time-honored xon and xoff codes, which tell the daemon to echo
anything between them AND write it to the log. This is how
`logger.force_echo()` works.
- Fix places where output could get stuck in buffers by flushing more
aggressively. This makes the output printed to the terminal the same
as that which would be printed through a pipe to `cat` or to a file.
Previously these could be weirdly different, and some output would be
missing when redirecting Spack to a file or pipe.
- Simplify input and color handling in both `build_environment.fork()`
and `llnl.util.tty.log.log_output()`. Neither requires an input_stream
parameter anymore; we assume stdin will be forwarded if possible.
- remove `llnl.util.lang.duplicate_stream()` and remove associated
monkey-patching in tests, as these aren't needed if you just check
whether stdin is a tty and has a fileno attribute.
2017-08-14 19:33:01 +08:00
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
"""
|
|
|
|
# Use line buffering (3rd param = 1) since Python 3 has a bug
|
|
|
|
# that prevents unbuffered text I/O.
|
|
|
|
in_pipe = os.fdopen(read_fd, 'r', 1)
|
|
|
|
os.close(write_fd)
|
2020-03-21 03:22:32 +08:00
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
# list of streams to select from
|
|
|
|
istreams = [in_pipe, stdin] if stdin else [in_pipe]
|
|
|
|
force_echo = False # parent can force echo for certain output
|
|
|
|
|
|
|
|
try:
|
|
|
|
with keyboard_input(stdin) as kb:
|
|
|
|
while True:
|
|
|
|
# fix the terminal settings if we recently came to
|
|
|
|
# the foreground
|
|
|
|
kb.check_fg_bg()
|
|
|
|
|
|
|
|
# wait for input from any stream. use a coarse timeout to
|
|
|
|
# allow other checks while we wait for input
|
|
|
|
rlist, _, _ = _retry(select.select)(istreams, [], [], 1e-1)
|
|
|
|
|
|
|
|
# Allow user to toggle echo with 'v' key.
|
|
|
|
# Currently ignores other chars.
|
|
|
|
# only read stdin if we're in the foreground
|
|
|
|
if stdin in rlist and not _is_background_tty(stdin):
|
|
|
|
# it's possible to be backgrounded between the above
|
|
|
|
# check and the read, so we ignore SIGTTIN here.
|
|
|
|
with ignore_signal(signal.SIGTTIN):
|
|
|
|
try:
|
|
|
|
if stdin.read(1) == 'v':
|
|
|
|
echo = not echo
|
|
|
|
except IOError as e:
|
|
|
|
# If SIGTTIN is ignored, the system gives EIO
|
|
|
|
# to let the caller know the read failed b/c it
|
|
|
|
# was in the bg. Ignore that too.
|
|
|
|
if e.errno != errno.EIO:
|
|
|
|
raise
|
|
|
|
|
|
|
|
if in_pipe in rlist:
|
|
|
|
# Handle output from the calling process.
|
|
|
|
line = _retry(in_pipe.readline)()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
|
|
|
|
# find control characters and strip them.
|
|
|
|
controls = control.findall(line)
|
|
|
|
line = control.sub('', line)
|
|
|
|
|
|
|
|
# Echo to stdout if requested or forced.
|
|
|
|
if echo or force_echo:
|
|
|
|
sys.stdout.write(line)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
# Stripped output to log file.
|
|
|
|
log_file.write(_strip(line))
|
|
|
|
log_file.flush()
|
|
|
|
|
|
|
|
if xon in controls:
|
|
|
|
force_echo = True
|
|
|
|
if xoff in controls:
|
|
|
|
force_echo = False
|
|
|
|
|
|
|
|
except BaseException:
|
|
|
|
tty.error("Exception occurred in writer daemon!")
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# send written data back to parent if we used a StringIO
|
|
|
|
if isinstance(log_file, StringIO):
|
|
|
|
control_pipe.send(log_file.getvalue())
|
|
|
|
log_file.close()
|
|
|
|
|
|
|
|
# send echo value back to the parent so it can be preserved.
|
|
|
|
control_pipe.send(echo)
|
2017-08-20 15:12:05 +08:00
|
|
|
|
2017-08-23 04:33:31 +08:00
|
|
|
|
2020-04-16 02:05:41 +08:00
|
|
|
def _retry(function):
|
|
|
|
"""Retry a call if errors indicating an interrupted system call occur.
|
|
|
|
|
|
|
|
Interrupted system calls return -1 and set ``errno`` to ``EINTR`` if
|
|
|
|
certain flags are not set. Newer Pythons automatically retry them,
|
|
|
|
but older Pythons do not, so we need to retry the calls.
|
|
|
|
|
|
|
|
This function converts a call like this:
|
|
|
|
|
|
|
|
syscall(args)
|
|
|
|
|
|
|
|
and makes it retry by wrapping the function like this:
|
|
|
|
|
|
|
|
_retry(syscall)(args)
|
|
|
|
|
|
|
|
This is a private function because EINTR is unfortunately raised in
|
|
|
|
different ways from different functions, and we only handle the ones
|
|
|
|
relevant for this file.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
return function(*args, **kwargs)
|
|
|
|
except IOError as e:
|
|
|
|
if e.errno == errno.EINTR:
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
except select.error as e:
|
|
|
|
if e.args[0] == errno.EINTR:
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
return wrapped
|