2016-01-15 18:15:24 +00:00
|
|
|
# Copyright (c) 2015 Stephen Warren
|
|
|
|
# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
# Generate an HTML-formatted log file containing multiple streams of data,
|
|
|
|
# each represented in a well-delineated/-structured fashion.
|
|
|
|
|
|
|
|
import cgi
|
|
|
|
import os.path
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
mod_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
class LogfileStream(object):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""A file-like object used to write a single logical stream of data into
|
2016-01-15 18:15:24 +00:00
|
|
|
a multiplexed log file. Objects of this type should be created by factory
|
2016-01-26 20:41:30 +00:00
|
|
|
functions in the Logfile class rather than directly."""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def __init__(self, logfile, name, chained_file):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Initialize a new object.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
logfile: The Logfile object to log to.
|
|
|
|
name: The name of this log stream.
|
|
|
|
chained_file: The file-like object to which all stream data should be
|
|
|
|
logged to in addition to logfile. Can be None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self.logfile = logfile
|
|
|
|
self.name = name
|
|
|
|
self.chained_file = chained_file
|
|
|
|
|
|
|
|
def close(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Dummy function so that this class is "file-like".
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
def write(self, data, implicit=False):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write data to the log stream.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
data: The data to write tot he file.
|
|
|
|
implicit: Boolean indicating whether data actually appeared in the
|
|
|
|
stream, or was implicitly generated. A valid use-case is to
|
|
|
|
repeat a shell prompt at the start of each separate log
|
|
|
|
section, which makes the log sections more readable in
|
|
|
|
isolation.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self.logfile.write(self, data, implicit)
|
|
|
|
if self.chained_file:
|
|
|
|
self.chained_file.write(data)
|
|
|
|
|
|
|
|
def flush(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Flush the log stream, to ensure correct log interleaving.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self.logfile.flush()
|
|
|
|
if self.chained_file:
|
|
|
|
self.chained_file.flush()
|
|
|
|
|
|
|
|
class RunAndLog(object):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""A utility object used to execute sub-processes and log their output to
|
2016-01-15 18:15:24 +00:00
|
|
|
a multiplexed log file. Objects of this type should be created by factory
|
2016-01-26 20:41:30 +00:00
|
|
|
functions in the Logfile class rather than directly."""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def __init__(self, logfile, name, chained_file):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Initialize a new object.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
logfile: The Logfile object to log to.
|
|
|
|
name: The name of this log stream or sub-process.
|
|
|
|
chained_file: The file-like object to which all stream data should
|
|
|
|
be logged to in addition to logfile. Can be None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self.logfile = logfile
|
|
|
|
self.name = name
|
|
|
|
self.chained_file = chained_file
|
|
|
|
|
|
|
|
def close(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Clean up any resources managed by this object."""
|
2016-01-15 18:15:24 +00:00
|
|
|
pass
|
|
|
|
|
2016-01-22 19:30:11 +00:00
|
|
|
def run(self, cmd, cwd=None, ignore_errors=False):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Run a command as a sub-process, and log the results.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
cmd: The command to execute.
|
|
|
|
cwd: The directory to run the command in. Can be None to use the
|
|
|
|
current directory.
|
2016-01-22 19:30:11 +00:00
|
|
|
ignore_errors: Indicate whether to ignore errors. If True, the
|
|
|
|
function will simply return if the command cannot be executed
|
|
|
|
or exits with an error code, otherwise an exception will be
|
|
|
|
raised if such problems occur.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
2016-01-26 20:41:31 +00:00
|
|
|
msg = '+' + ' '.join(cmd) + '\n'
|
2016-01-15 18:15:24 +00:00
|
|
|
if self.chained_file:
|
|
|
|
self.chained_file.write(msg)
|
|
|
|
self.logfile.write(self, msg)
|
|
|
|
|
|
|
|
try:
|
|
|
|
p = subprocess.Popen(cmd, cwd=cwd,
|
|
|
|
stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
|
|
(stdout, stderr) = p.communicate()
|
|
|
|
output = ''
|
|
|
|
if stdout:
|
|
|
|
if stderr:
|
|
|
|
output += 'stdout:\n'
|
|
|
|
output += stdout
|
|
|
|
if stderr:
|
|
|
|
if stdout:
|
|
|
|
output += 'stderr:\n'
|
|
|
|
output += stderr
|
|
|
|
exit_status = p.returncode
|
|
|
|
exception = None
|
|
|
|
except subprocess.CalledProcessError as cpe:
|
|
|
|
output = cpe.output
|
|
|
|
exit_status = cpe.returncode
|
|
|
|
exception = cpe
|
|
|
|
except Exception as e:
|
|
|
|
output = ''
|
|
|
|
exit_status = 0
|
|
|
|
exception = e
|
|
|
|
if output and not output.endswith('\n'):
|
|
|
|
output += '\n'
|
2016-01-22 19:30:11 +00:00
|
|
|
if exit_status and not exception and not ignore_errors:
|
2016-01-15 18:15:24 +00:00
|
|
|
exception = Exception('Exit code: ' + str(exit_status))
|
|
|
|
if exception:
|
|
|
|
output += str(exception) + '\n'
|
|
|
|
self.logfile.write(self, output)
|
|
|
|
if self.chained_file:
|
|
|
|
self.chained_file.write(output)
|
|
|
|
if exception:
|
|
|
|
raise exception
|
|
|
|
|
|
|
|
class SectionCtxMgr(object):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""A context manager for Python's "with" statement, which allows a certain
|
2016-01-15 18:15:24 +00:00
|
|
|
portion of test code to be logged to a separate section of the log file.
|
|
|
|
Objects of this type should be created by factory functions in the Logfile
|
2016-01-26 20:41:30 +00:00
|
|
|
class rather than directly."""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def __init__(self, log, marker):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Initialize a new object.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
log: The Logfile object to log to.
|
|
|
|
marker: The name of the nested log section.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self.log = log
|
|
|
|
self.marker = marker
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.log.start_section(self.marker)
|
|
|
|
|
|
|
|
def __exit__(self, extype, value, traceback):
|
|
|
|
self.log.end_section(self.marker)
|
|
|
|
|
|
|
|
class Logfile(object):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Generates an HTML-formatted log file containing multiple streams of
|
|
|
|
data, each represented in a well-delineated/-structured fashion."""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def __init__(self, fn):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Initialize a new object.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
fn: The filename to write to.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
2016-01-26 20:41:31 +00:00
|
|
|
self.f = open(fn, 'wt')
|
2016-01-15 18:15:24 +00:00
|
|
|
self.last_stream = None
|
|
|
|
self.blocks = []
|
|
|
|
self.cur_evt = 1
|
2016-01-26 20:41:31 +00:00
|
|
|
shutil.copy(mod_dir + '/multiplexed_log.css', os.path.dirname(fn))
|
|
|
|
self.f.write('''\
|
2016-01-15 18:15:24 +00:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<link rel="stylesheet" type="text/css" href="multiplexed_log.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<tt>
|
2016-01-26 20:41:31 +00:00
|
|
|
''')
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def close(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Close the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
After calling this function, no more data may be written to the log.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
2016-01-26 20:41:31 +00:00
|
|
|
self.f.write('''\
|
2016-01-15 18:15:24 +00:00
|
|
|
</tt>
|
|
|
|
</body>
|
|
|
|
</html>
|
2016-01-26 20:41:31 +00:00
|
|
|
''')
|
2016-01-15 18:15:24 +00:00
|
|
|
self.f.close()
|
|
|
|
|
|
|
|
# The set of characters that should be represented as hexadecimal codes in
|
|
|
|
# the log file.
|
2016-01-26 20:41:31 +00:00
|
|
|
_nonprint = ('%' + ''.join(chr(c) for c in range(0, 32) if c not in (9, 10)) +
|
|
|
|
''.join(chr(c) for c in range(127, 256)))
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def _escape(self, data):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Render data format suitable for inclusion in an HTML document.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
This includes HTML-escaping certain characters, and translating
|
|
|
|
control characters to a hexadecimal representation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: The raw string data to be escaped.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
An escaped version of the data.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
2016-01-26 20:41:31 +00:00
|
|
|
data = data.replace(chr(13), '')
|
|
|
|
data = ''.join((c in self._nonprint) and ('%%%02x' % ord(c)) or
|
2016-01-15 18:15:24 +00:00
|
|
|
c for c in data)
|
|
|
|
data = cgi.escape(data)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def _terminate_stream(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write HTML to the log file to terminate the current stream's data.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self.cur_evt += 1
|
|
|
|
if not self.last_stream:
|
|
|
|
return
|
2016-01-26 20:41:31 +00:00
|
|
|
self.f.write('</pre>\n')
|
|
|
|
self.f.write('<div class="stream-trailer" id="' +
|
|
|
|
self.last_stream.name + '">End stream: ' +
|
|
|
|
self.last_stream.name + '</div>\n')
|
|
|
|
self.f.write('</div>\n')
|
2016-01-15 18:15:24 +00:00
|
|
|
self.last_stream = None
|
|
|
|
|
|
|
|
def _note(self, note_type, msg):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write a note or one-off message to the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
note_type: The type of note. This must be a value supported by the
|
|
|
|
accompanying multiplexed_log.css.
|
|
|
|
msg: The note/message to log.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._terminate_stream()
|
2016-01-26 20:41:31 +00:00
|
|
|
self.f.write('<div class="' + note_type + '">\n<pre>')
|
2016-01-15 18:15:24 +00:00
|
|
|
self.f.write(self._escape(msg))
|
2016-01-26 20:41:31 +00:00
|
|
|
self.f.write('\n</pre></div>\n')
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def start_section(self, marker):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Begin a new nested section in the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
marker: The name of the section that is starting.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._terminate_stream()
|
|
|
|
self.blocks.append(marker)
|
2016-01-26 20:41:31 +00:00
|
|
|
blk_path = '/'.join(self.blocks)
|
|
|
|
self.f.write('<div class="section" id="' + blk_path + '">\n')
|
|
|
|
self.f.write('<div class="section-header" id="' + blk_path +
|
|
|
|
'">Section: ' + blk_path + '</div>\n')
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
def end_section(self, marker):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Terminate the current nested section in the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
This function validates proper nesting of start_section() and
|
|
|
|
end_section() calls. If a mismatch is found, an exception is raised.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
marker: The name of the section that is ending.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
if (not self.blocks) or (marker != self.blocks[-1]):
|
2016-01-26 20:41:31 +00:00
|
|
|
raise Exception('Block nesting mismatch: "%s" "%s"' %
|
|
|
|
(marker, '/'.join(self.blocks)))
|
2016-01-15 18:15:24 +00:00
|
|
|
self._terminate_stream()
|
2016-01-26 20:41:31 +00:00
|
|
|
blk_path = '/'.join(self.blocks)
|
|
|
|
self.f.write('<div class="section-trailer" id="section-trailer-' +
|
|
|
|
blk_path + '">End section: ' + blk_path + '</div>\n')
|
|
|
|
self.f.write('</div>\n')
|
2016-01-15 18:15:24 +00:00
|
|
|
self.blocks.pop()
|
|
|
|
|
|
|
|
def section(self, marker):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Create a temporary section in the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
This function creates a context manager for Python's "with" statement,
|
|
|
|
which allows a certain portion of test code to be logged to a separate
|
|
|
|
section of the log file.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
with log.section("somename"):
|
|
|
|
some test code
|
|
|
|
|
|
|
|
Args:
|
|
|
|
marker: The name of the nested section.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A context manager object.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
return SectionCtxMgr(self, marker)
|
|
|
|
|
|
|
|
def error(self, msg):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write an error note to the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
msg: A message describing the error.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._note("error", msg)
|
|
|
|
|
|
|
|
def warning(self, msg):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write an warning note to the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
msg: A message describing the warning.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._note("warning", msg)
|
|
|
|
|
|
|
|
def info(self, msg):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write an informational note to the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
msg: An informational message.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._note("info", msg)
|
|
|
|
|
|
|
|
def action(self, msg):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write an action note to the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
msg: A message describing the action that is being logged.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._note("action", msg)
|
|
|
|
|
|
|
|
def status_pass(self, msg):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write a note to the log file describing test(s) which passed.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
msg: A message describing passed test(s).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._note("status-pass", msg)
|
|
|
|
|
|
|
|
def status_skipped(self, msg):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write a note to the log file describing skipped test(s).
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
msg: A message describing passed test(s).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._note("status-skipped", msg)
|
|
|
|
|
|
|
|
def status_fail(self, msg):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write a note to the log file describing failed test(s).
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
msg: A message describing passed test(s).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self._note("status-fail", msg)
|
|
|
|
|
|
|
|
def get_stream(self, name, chained_file=None):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Create an object to log a single stream's data into the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
This creates a "file-like" object that can be written to in order to
|
|
|
|
write a single stream's data to the log file. The implementation will
|
|
|
|
handle any required interleaving of data (from multiple streams) in
|
|
|
|
the log, in a way that makes it obvious which stream each bit of data
|
|
|
|
came from.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: The name of the stream.
|
|
|
|
chained_file: The file-like object to which all stream data should
|
|
|
|
be logged to in addition to this log. Can be None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A file-like object.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
return LogfileStream(self, name, chained_file)
|
|
|
|
|
|
|
|
def get_runner(self, name, chained_file=None):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Create an object that executes processes and logs their output.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
name: The name of this sub-process.
|
|
|
|
chained_file: The file-like object to which all stream data should
|
|
|
|
be logged to in addition to logfile. Can be None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A RunAndLog object.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
return RunAndLog(self, name, chained_file)
|
|
|
|
|
|
|
|
def write(self, stream, data, implicit=False):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Write stream data into the log file.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
This function should only be used by instances of LogfileStream or
|
|
|
|
RunAndLog.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
stream: The stream whose data is being logged.
|
|
|
|
data: The data to log.
|
|
|
|
implicit: Boolean indicating whether data actually appeared in the
|
|
|
|
stream, or was implicitly generated. A valid use-case is to
|
|
|
|
repeat a shell prompt at the start of each separate log
|
|
|
|
section, which makes the log sections more readable in
|
|
|
|
isolation.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
if stream != self.last_stream:
|
|
|
|
self._terminate_stream()
|
2016-01-26 20:41:31 +00:00
|
|
|
self.f.write('<div class="stream" id="%s">\n' % stream.name)
|
|
|
|
self.f.write('<div class="stream-header" id="' + stream.name +
|
|
|
|
'">Stream: ' + stream.name + '</div>\n')
|
|
|
|
self.f.write('<pre>')
|
2016-01-15 18:15:24 +00:00
|
|
|
if implicit:
|
2016-01-26 20:41:31 +00:00
|
|
|
self.f.write('<span class="implicit">')
|
2016-01-15 18:15:24 +00:00
|
|
|
self.f.write(self._escape(data))
|
|
|
|
if implicit:
|
2016-01-26 20:41:31 +00:00
|
|
|
self.f.write('</span>')
|
2016-01-15 18:15:24 +00:00
|
|
|
self.last_stream = stream
|
|
|
|
|
|
|
|
def flush(self):
|
2016-01-26 20:41:30 +00:00
|
|
|
"""Flush the log stream, to ensure correct log interleaving.
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Nothing.
|
2016-01-26 20:41:30 +00:00
|
|
|
"""
|
2016-01-15 18:15:24 +00:00
|
|
|
|
|
|
|
self.f.flush()
|