1 # Copyright (c) 2015 Stephen Warren
2 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
4 # SPDX-License-Identifier: GPL-2.0
6 # Generate an HTML-formatted log file containing multiple streams of data,
7 # each represented in a well-delineated/-structured fashion.
15 mod_dir = os.path.dirname(os.path.abspath(__file__))
17 class LogfileStream(object):
18 """A file-like object used to write a single logical stream of data into
19 a multiplexed log file. Objects of this type should be created by factory
20 functions in the Logfile class rather than directly."""
22 def __init__(self, logfile, name, chained_file):
23 """Initialize a new object.
26 logfile: The Logfile object to log to.
27 name: The name of this log stream.
28 chained_file: The file-like object to which all stream data should be
29 logged to in addition to logfile. Can be None.
35 self.logfile = logfile
37 self.chained_file = chained_file
40 """Dummy function so that this class is "file-like".
51 def write(self, data, implicit=False):
52 """Write data to the log stream.
55 data: The data to write tot he file.
56 implicit: Boolean indicating whether data actually appeared in the
57 stream, or was implicitly generated. A valid use-case is to
58 repeat a shell prompt at the start of each separate log
59 section, which makes the log sections more readable in
66 self.logfile.write(self, data, implicit)
68 self.chained_file.write(data)
71 """Flush the log stream, to ensure correct log interleaving.
82 self.chained_file.flush()
84 class RunAndLog(object):
85 """A utility object used to execute sub-processes and log their output to
86 a multiplexed log file. Objects of this type should be created by factory
87 functions in the Logfile class rather than directly."""
89 def __init__(self, logfile, name, chained_file):
90 """Initialize a new object.
93 logfile: The Logfile object to log to.
94 name: The name of this log stream or sub-process.
95 chained_file: The file-like object to which all stream data should
96 be logged to in addition to logfile. Can be None.
102 self.logfile = logfile
104 self.chained_file = chained_file
106 self.exit_status = None
109 """Clean up any resources managed by this object."""
112 def run(self, cmd, cwd=None, ignore_errors=False):
113 """Run a command as a sub-process, and log the results.
115 The output is available at self.output which can be useful if there is
119 cmd: The command to execute.
120 cwd: The directory to run the command in. Can be None to use the
122 ignore_errors: Indicate whether to ignore errors. If True, the
123 function will simply return if the command cannot be executed
124 or exits with an error code, otherwise an exception will be
125 raised if such problems occur.
128 The output as a string.
131 msg = '+' + ' '.join(cmd) + '\n'
132 if self.chained_file:
133 self.chained_file.write(msg)
134 self.logfile.write(self, msg)
137 p = subprocess.Popen(cmd, cwd=cwd,
138 stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
139 (stdout, stderr) = p.communicate()
143 output += 'stdout:\n'
147 output += 'stderr:\n'
149 exit_status = p.returncode
151 except subprocess.CalledProcessError as cpe:
153 exit_status = cpe.returncode
155 except Exception as e:
159 if output and not output.endswith('\n'):
161 if exit_status and not exception and not ignore_errors:
162 exception = Exception('Exit code: ' + str(exit_status))
164 output += str(exception) + '\n'
165 self.logfile.write(self, output)
166 if self.chained_file:
167 self.chained_file.write(output)
168 self.logfile.timestamp()
170 # Store the output so it can be accessed if we raise an exception.
172 self.exit_status = exit_status
177 class SectionCtxMgr(object):
178 """A context manager for Python's "with" statement, which allows a certain
179 portion of test code to be logged to a separate section of the log file.
180 Objects of this type should be created by factory functions in the Logfile
181 class rather than directly."""
183 def __init__(self, log, marker, anchor):
184 """Initialize a new object.
187 log: The Logfile object to log to.
188 marker: The name of the nested log section.
189 anchor: The anchor value to pass to start_section().
200 self.anchor = self.log.start_section(self.marker, self.anchor)
202 def __exit__(self, extype, value, traceback):
203 self.log.end_section(self.marker)
205 class Logfile(object):
206 """Generates an HTML-formatted log file containing multiple streams of
207 data, each represented in a well-delineated/-structured fashion."""
209 def __init__(self, fn):
210 """Initialize a new object.
213 fn: The filename to write to.
219 self.f = open(fn, 'wt')
220 self.last_stream = None
224 self.timestamp_start = self._get_time()
225 self.timestamp_prev = self.timestamp_start
226 self.timestamp_blocks = []
228 shutil.copy(mod_dir + '/multiplexed_log.css', os.path.dirname(fn))
232 <link rel="stylesheet" type="text/css" href="multiplexed_log.css">
233 <script src="http://code.jquery.com/jquery.min.js"></script>
235 $(document).ready(function () {
236 // Copy status report HTML to start of log for easy access
237 sts = $(".block#status_report")[0].outerHTML;
238 $("tt").prepend(sts);
240 // Add expand/contract buttons to all block headers
241 btns = "<span class=\\\"block-expand hidden\\\">[+] </span>" +
242 "<span class=\\\"block-contract\\\">[-] </span>";
243 $(".block-header").prepend(btns);
245 // Pre-contract all blocks which passed, leaving only problem cases
246 // expanded, to highlight issues the user should look at.
247 // Only top-level blocks (sections) should have any status
248 passed_bcs = $(".block-content:has(.status-pass)");
249 // Some blocks might have multiple status entries (e.g. the status
250 // report), so take care not to hide blocks with partial success.
251 passed_bcs = passed_bcs.not(":has(.status-fail)");
252 passed_bcs = passed_bcs.not(":has(.status-xfail)");
253 passed_bcs = passed_bcs.not(":has(.status-xpass)");
254 passed_bcs = passed_bcs.not(":has(.status-skipped)");
255 // Hide the passed blocks
256 passed_bcs.addClass("hidden");
257 // Flip the expand/contract button hiding for those blocks.
258 bhs = passed_bcs.parent().children(".block-header")
259 bhs.children(".block-expand").removeClass("hidden");
260 bhs.children(".block-contract").addClass("hidden");
262 // Add click handler to block headers.
263 // The handler expands/contracts the block.
264 $(".block-header").on("click", function (e) {
265 var header = $(this);
266 var content = header.next(".block-content");
267 var expanded = !content.hasClass("hidden");
269 content.addClass("hidden");
270 header.children(".block-expand").first().removeClass("hidden");
271 header.children(".block-contract").first().addClass("hidden");
273 header.children(".block-contract").first().removeClass("hidden");
274 header.children(".block-expand").first().addClass("hidden");
275 content.removeClass("hidden");
279 // When clicking on a link, expand the target block
280 $("a").on("click", function (e) {
281 var block = $($(this).attr("href"));
282 var header = block.children(".block-header");
283 var content = block.children(".block-content").first();
284 header.children(".block-contract").first().removeClass("hidden");
285 header.children(".block-expand").first().addClass("hidden");
286 content.removeClass("hidden");
296 """Close the log file.
298 After calling this function, no more data may be written to the log.
314 # The set of characters that should be represented as hexadecimal codes in
316 _nonprint = ('%' + ''.join(chr(c) for c in range(0, 32) if c not in (9, 10)) +
317 ''.join(chr(c) for c in range(127, 256)))
319 def _escape(self, data):
320 """Render data format suitable for inclusion in an HTML document.
322 This includes HTML-escaping certain characters, and translating
323 control characters to a hexadecimal representation.
326 data: The raw string data to be escaped.
329 An escaped version of the data.
332 data = data.replace(chr(13), '')
333 data = ''.join((c in self._nonprint) and ('%%%02x' % ord(c)) or
335 data = cgi.escape(data)
338 def _terminate_stream(self):
339 """Write HTML to the log file to terminate the current stream's data.
349 if not self.last_stream:
351 self.f.write('</pre>\n')
352 self.f.write('<div class="stream-trailer block-trailer">End stream: ' +
353 self.last_stream.name + '</div>\n')
354 self.f.write('</div>\n')
355 self.f.write('</div>\n')
356 self.last_stream = None
358 def _note(self, note_type, msg, anchor=None):
359 """Write a note or one-off message to the log file.
362 note_type: The type of note. This must be a value supported by the
363 accompanying multiplexed_log.css.
364 msg: The note/message to log.
365 anchor: Optional internal link target.
371 self._terminate_stream()
372 self.f.write('<div class="' + note_type + '">\n')
373 self.f.write('<pre>')
375 self.f.write('<a href="#%s">' % anchor)
376 self.f.write(self._escape(msg))
379 self.f.write('\n</pre>\n')
380 self.f.write('</div>\n')
382 def start_section(self, marker, anchor=None):
383 """Begin a new nested section in the log file.
386 marker: The name of the section that is starting.
387 anchor: The value to use for the anchor. If None, a unique value
388 will be calculated and used
391 Name of the HTML anchor emitted before section.
394 self._terminate_stream()
395 self.blocks.append(marker)
396 self.timestamp_blocks.append(self._get_time())
399 anchor = str(self.anchor)
400 blk_path = '/'.join(self.blocks)
401 self.f.write('<div class="section block" id="' + anchor + '">\n')
402 self.f.write('<div class="section-header block-header">Section: ' +
403 blk_path + '</div>\n')
404 self.f.write('<div class="section-content block-content">\n')
409 def end_section(self, marker):
410 """Terminate the current nested section in the log file.
412 This function validates proper nesting of start_section() and
413 end_section() calls. If a mismatch is found, an exception is raised.
416 marker: The name of the section that is ending.
422 if (not self.blocks) or (marker != self.blocks[-1]):
423 raise Exception('Block nesting mismatch: "%s" "%s"' %
424 (marker, '/'.join(self.blocks)))
425 self._terminate_stream()
426 timestamp_now = self._get_time()
427 timestamp_section_start = self.timestamp_blocks.pop()
428 delta_section = timestamp_now - timestamp_section_start
429 self._note("timestamp",
430 "TIME: SINCE-SECTION: " + str(delta_section))
431 blk_path = '/'.join(self.blocks)
432 self.f.write('<div class="section-trailer block-trailer">' +
433 'End section: ' + blk_path + '</div>\n')
434 self.f.write('</div>\n')
435 self.f.write('</div>\n')
438 def section(self, marker, anchor=None):
439 """Create a temporary section in the log file.
441 This function creates a context manager for Python's "with" statement,
442 which allows a certain portion of test code to be logged to a separate
443 section of the log file.
446 with log.section("somename"):
450 marker: The name of the nested section.
451 anchor: The anchor value to pass to start_section().
454 A context manager object.
457 return SectionCtxMgr(self, marker, anchor)
459 def error(self, msg):
460 """Write an error note to the log file.
463 msg: A message describing the error.
469 self._note("error", msg)
471 def warning(self, msg):
472 """Write an warning note to the log file.
475 msg: A message describing the warning.
481 self._note("warning", msg)
484 """Write an informational note to the log file.
487 msg: An informational message.
493 self._note("info", msg)
495 def action(self, msg):
496 """Write an action note to the log file.
499 msg: A message describing the action that is being logged.
505 self._note("action", msg)
508 return datetime.datetime.now()
511 """Write a timestamp to the log file.
520 timestamp_now = self._get_time()
521 delta_prev = timestamp_now - self.timestamp_prev
522 delta_start = timestamp_now - self.timestamp_start
523 self.timestamp_prev = timestamp_now
525 self._note("timestamp",
526 "TIME: NOW: " + timestamp_now.strftime("%Y/%m/%d %H:%M:%S.%f"))
527 self._note("timestamp",
528 "TIME: SINCE-PREV: " + str(delta_prev))
529 self._note("timestamp",
530 "TIME: SINCE-START: " + str(delta_start))
532 def status_pass(self, msg, anchor=None):
533 """Write a note to the log file describing test(s) which passed.
536 msg: A message describing the passed test(s).
537 anchor: Optional internal link target.
543 self._note("status-pass", msg, anchor)
545 def status_skipped(self, msg, anchor=None):
546 """Write a note to the log file describing skipped test(s).
549 msg: A message describing the skipped test(s).
550 anchor: Optional internal link target.
556 self._note("status-skipped", msg, anchor)
558 def status_xfail(self, msg, anchor=None):
559 """Write a note to the log file describing xfailed test(s).
562 msg: A message describing the xfailed test(s).
563 anchor: Optional internal link target.
569 self._note("status-xfail", msg, anchor)
571 def status_xpass(self, msg, anchor=None):
572 """Write a note to the log file describing xpassed test(s).
575 msg: A message describing the xpassed test(s).
576 anchor: Optional internal link target.
582 self._note("status-xpass", msg, anchor)
584 def status_fail(self, msg, anchor=None):
585 """Write a note to the log file describing failed test(s).
588 msg: A message describing the failed test(s).
589 anchor: Optional internal link target.
595 self._note("status-fail", msg, anchor)
597 def get_stream(self, name, chained_file=None):
598 """Create an object to log a single stream's data into the log file.
600 This creates a "file-like" object that can be written to in order to
601 write a single stream's data to the log file. The implementation will
602 handle any required interleaving of data (from multiple streams) in
603 the log, in a way that makes it obvious which stream each bit of data
607 name: The name of the stream.
608 chained_file: The file-like object to which all stream data should
609 be logged to in addition to this log. Can be None.
615 return LogfileStream(self, name, chained_file)
617 def get_runner(self, name, chained_file=None):
618 """Create an object that executes processes and logs their output.
621 name: The name of this sub-process.
622 chained_file: The file-like object to which all stream data should
623 be logged to in addition to logfile. Can be None.
629 return RunAndLog(self, name, chained_file)
631 def write(self, stream, data, implicit=False):
632 """Write stream data into the log file.
634 This function should only be used by instances of LogfileStream or
638 stream: The stream whose data is being logged.
639 data: The data to log.
640 implicit: Boolean indicating whether data actually appeared in the
641 stream, or was implicitly generated. A valid use-case is to
642 repeat a shell prompt at the start of each separate log
643 section, which makes the log sections more readable in
650 if stream != self.last_stream:
651 self._terminate_stream()
652 self.f.write('<div class="stream block">\n')
653 self.f.write('<div class="stream-header block-header">Stream: ' +
654 stream.name + '</div>\n')
655 self.f.write('<div class="stream-content block-content">\n')
656 self.f.write('<pre>')
658 self.f.write('<span class="implicit">')
659 self.f.write(self._escape(data))
661 self.f.write('</span>')
662 self.last_stream = stream
665 """Flush the log stream, to ensure correct log interleaving.