Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 2 | # Copyright (c) 2015 Stephen Warren |
| 3 | # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 4 | |
| 5 | # Generate an HTML-formatted log file containing multiple streams of data, |
| 6 | # each represented in a well-delineated/-structured fashion. |
| 7 | |
| 8 | import cgi |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 9 | import datetime |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 10 | import os.path |
| 11 | import shutil |
| 12 | import subprocess |
| 13 | |
| 14 | mod_dir = os.path.dirname(os.path.abspath(__file__)) |
| 15 | |
| 16 | class LogfileStream(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 17 | """A file-like object used to write a single logical stream of data into |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 18 | a multiplexed log file. Objects of this type should be created by factory |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 19 | functions in the Logfile class rather than directly.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 20 | |
| 21 | def __init__(self, logfile, name, chained_file): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 22 | """Initialize a new object. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 23 | |
| 24 | Args: |
| 25 | logfile: The Logfile object to log to. |
| 26 | name: The name of this log stream. |
| 27 | chained_file: The file-like object to which all stream data should be |
| 28 | logged to in addition to logfile. Can be None. |
| 29 | |
| 30 | Returns: |
| 31 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 32 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 33 | |
| 34 | self.logfile = logfile |
| 35 | self.name = name |
| 36 | self.chained_file = chained_file |
| 37 | |
| 38 | def close(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 39 | """Dummy function so that this class is "file-like". |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 40 | |
| 41 | Args: |
| 42 | None. |
| 43 | |
| 44 | Returns: |
| 45 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 46 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 47 | |
| 48 | pass |
| 49 | |
| 50 | def write(self, data, implicit=False): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 51 | """Write data to the log stream. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 52 | |
| 53 | Args: |
| 54 | data: The data to write tot he file. |
| 55 | implicit: Boolean indicating whether data actually appeared in the |
| 56 | stream, or was implicitly generated. A valid use-case is to |
| 57 | repeat a shell prompt at the start of each separate log |
| 58 | section, which makes the log sections more readable in |
| 59 | isolation. |
| 60 | |
| 61 | Returns: |
| 62 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 63 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 64 | |
| 65 | self.logfile.write(self, data, implicit) |
| 66 | if self.chained_file: |
| 67 | self.chained_file.write(data) |
| 68 | |
| 69 | def flush(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 70 | """Flush the log stream, to ensure correct log interleaving. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 71 | |
| 72 | Args: |
| 73 | None. |
| 74 | |
| 75 | Returns: |
| 76 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 77 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 78 | |
| 79 | self.logfile.flush() |
| 80 | if self.chained_file: |
| 81 | self.chained_file.flush() |
| 82 | |
| 83 | class RunAndLog(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 84 | """A utility object used to execute sub-processes and log their output to |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 85 | a multiplexed log file. Objects of this type should be created by factory |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 86 | functions in the Logfile class rather than directly.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 87 | |
| 88 | def __init__(self, logfile, name, chained_file): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 89 | """Initialize a new object. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 90 | |
| 91 | Args: |
| 92 | logfile: The Logfile object to log to. |
| 93 | name: The name of this log stream or sub-process. |
| 94 | chained_file: The file-like object to which all stream data should |
| 95 | be logged to in addition to logfile. Can be None. |
| 96 | |
| 97 | Returns: |
| 98 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 99 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 100 | |
| 101 | self.logfile = logfile |
| 102 | self.name = name |
| 103 | self.chained_file = chained_file |
Simon Glass | 4134caf | 2016-07-03 09:40:38 -0600 | [diff] [blame] | 104 | self.output = None |
Simon Glass | 95adb80 | 2016-07-31 17:35:03 -0600 | [diff] [blame] | 105 | self.exit_status = None |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 106 | |
| 107 | def close(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 108 | """Clean up any resources managed by this object.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 109 | pass |
| 110 | |
Stephen Warren | 118e37e | 2016-01-22 12:30:11 -0700 | [diff] [blame] | 111 | def run(self, cmd, cwd=None, ignore_errors=False): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 112 | """Run a command as a sub-process, and log the results. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 113 | |
Simon Glass | 4134caf | 2016-07-03 09:40:38 -0600 | [diff] [blame] | 114 | The output is available at self.output which can be useful if there is |
| 115 | an exception. |
| 116 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 117 | Args: |
| 118 | cmd: The command to execute. |
| 119 | cwd: The directory to run the command in. Can be None to use the |
| 120 | current directory. |
Stephen Warren | 118e37e | 2016-01-22 12:30:11 -0700 | [diff] [blame] | 121 | ignore_errors: Indicate whether to ignore errors. If True, the |
| 122 | function will simply return if the command cannot be executed |
| 123 | or exits with an error code, otherwise an exception will be |
| 124 | raised if such problems occur. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 125 | |
| 126 | Returns: |
Simon Glass | 9f3c9e9 | 2016-07-03 09:40:37 -0600 | [diff] [blame] | 127 | The output as a string. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 128 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 129 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 130 | msg = '+' + ' '.join(cmd) + '\n' |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 131 | if self.chained_file: |
| 132 | self.chained_file.write(msg) |
| 133 | self.logfile.write(self, msg) |
| 134 | |
| 135 | try: |
| 136 | p = subprocess.Popen(cmd, cwd=cwd, |
| 137 | stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 138 | (stdout, stderr) = p.communicate() |
| 139 | output = '' |
| 140 | if stdout: |
| 141 | if stderr: |
| 142 | output += 'stdout:\n' |
| 143 | output += stdout |
| 144 | if stderr: |
| 145 | if stdout: |
| 146 | output += 'stderr:\n' |
| 147 | output += stderr |
| 148 | exit_status = p.returncode |
| 149 | exception = None |
| 150 | except subprocess.CalledProcessError as cpe: |
| 151 | output = cpe.output |
| 152 | exit_status = cpe.returncode |
| 153 | exception = cpe |
| 154 | except Exception as e: |
| 155 | output = '' |
| 156 | exit_status = 0 |
| 157 | exception = e |
| 158 | if output and not output.endswith('\n'): |
| 159 | output += '\n' |
Stephen Warren | 118e37e | 2016-01-22 12:30:11 -0700 | [diff] [blame] | 160 | if exit_status and not exception and not ignore_errors: |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 161 | exception = Exception('Exit code: ' + str(exit_status)) |
| 162 | if exception: |
| 163 | output += str(exception) + '\n' |
| 164 | self.logfile.write(self, output) |
| 165 | if self.chained_file: |
| 166 | self.chained_file.write(output) |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 167 | self.logfile.timestamp() |
Simon Glass | 4134caf | 2016-07-03 09:40:38 -0600 | [diff] [blame] | 168 | |
| 169 | # Store the output so it can be accessed if we raise an exception. |
| 170 | self.output = output |
Simon Glass | 95adb80 | 2016-07-31 17:35:03 -0600 | [diff] [blame] | 171 | self.exit_status = exit_status |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 172 | if exception: |
| 173 | raise exception |
Simon Glass | 9f3c9e9 | 2016-07-03 09:40:37 -0600 | [diff] [blame] | 174 | return output |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 175 | |
| 176 | class SectionCtxMgr(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 177 | """A context manager for Python's "with" statement, which allows a certain |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 178 | portion of test code to be logged to a separate section of the log file. |
| 179 | Objects of this type should be created by factory functions in the Logfile |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 180 | class rather than directly.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 181 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 182 | def __init__(self, log, marker, anchor): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 183 | """Initialize a new object. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 184 | |
| 185 | Args: |
| 186 | log: The Logfile object to log to. |
| 187 | marker: The name of the nested log section. |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 188 | anchor: The anchor value to pass to start_section(). |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 189 | |
| 190 | Returns: |
| 191 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 192 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 193 | |
| 194 | self.log = log |
| 195 | self.marker = marker |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 196 | self.anchor = anchor |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 197 | |
| 198 | def __enter__(self): |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 199 | self.anchor = self.log.start_section(self.marker, self.anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 200 | |
| 201 | def __exit__(self, extype, value, traceback): |
| 202 | self.log.end_section(self.marker) |
| 203 | |
| 204 | class Logfile(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 205 | """Generates an HTML-formatted log file containing multiple streams of |
| 206 | data, each represented in a well-delineated/-structured fashion.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 207 | |
| 208 | def __init__(self, fn): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 209 | """Initialize a new object. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 210 | |
| 211 | Args: |
| 212 | fn: The filename to write to. |
| 213 | |
| 214 | Returns: |
| 215 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 216 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 217 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 218 | self.f = open(fn, 'wt') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 219 | self.last_stream = None |
| 220 | self.blocks = [] |
| 221 | self.cur_evt = 1 |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 222 | self.anchor = 0 |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 223 | self.timestamp_start = self._get_time() |
| 224 | self.timestamp_prev = self.timestamp_start |
| 225 | self.timestamp_blocks = [] |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 226 | self.seen_warning = False |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 227 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 228 | shutil.copy(mod_dir + '/multiplexed_log.css', os.path.dirname(fn)) |
| 229 | self.f.write('''\ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 230 | <html> |
| 231 | <head> |
| 232 | <link rel="stylesheet" type="text/css" href="multiplexed_log.css"> |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 233 | <script src="http://code.jquery.com/jquery.min.js"></script> |
| 234 | <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); |
| 239 | |
| 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); |
| 244 | |
| 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)"); |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 255 | passed_bcs = passed_bcs.not(":has(.status-warning)"); |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 256 | // Hide the passed blocks |
| 257 | passed_bcs.addClass("hidden"); |
| 258 | // Flip the expand/contract button hiding for those blocks. |
| 259 | bhs = passed_bcs.parent().children(".block-header") |
| 260 | bhs.children(".block-expand").removeClass("hidden"); |
| 261 | bhs.children(".block-contract").addClass("hidden"); |
| 262 | |
| 263 | // Add click handler to block headers. |
| 264 | // The handler expands/contracts the block. |
| 265 | $(".block-header").on("click", function (e) { |
| 266 | var header = $(this); |
| 267 | var content = header.next(".block-content"); |
| 268 | var expanded = !content.hasClass("hidden"); |
| 269 | if (expanded) { |
| 270 | content.addClass("hidden"); |
| 271 | header.children(".block-expand").first().removeClass("hidden"); |
| 272 | header.children(".block-contract").first().addClass("hidden"); |
| 273 | } else { |
| 274 | header.children(".block-contract").first().removeClass("hidden"); |
| 275 | header.children(".block-expand").first().addClass("hidden"); |
| 276 | content.removeClass("hidden"); |
| 277 | } |
| 278 | }); |
| 279 | |
| 280 | // When clicking on a link, expand the target block |
| 281 | $("a").on("click", function (e) { |
| 282 | var block = $($(this).attr("href")); |
| 283 | var header = block.children(".block-header"); |
| 284 | var content = block.children(".block-content").first(); |
| 285 | header.children(".block-contract").first().removeClass("hidden"); |
| 286 | header.children(".block-expand").first().addClass("hidden"); |
| 287 | content.removeClass("hidden"); |
| 288 | }); |
| 289 | }); |
| 290 | </script> |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 291 | </head> |
| 292 | <body> |
| 293 | <tt> |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 294 | ''') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 295 | |
| 296 | def close(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 297 | """Close the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 298 | |
| 299 | After calling this function, no more data may be written to the log. |
| 300 | |
| 301 | Args: |
| 302 | None. |
| 303 | |
| 304 | Returns: |
| 305 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 306 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 307 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 308 | self.f.write('''\ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 309 | </tt> |
| 310 | </body> |
| 311 | </html> |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 312 | ''') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 313 | self.f.close() |
| 314 | |
| 315 | # The set of characters that should be represented as hexadecimal codes in |
| 316 | # the log file. |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 317 | _nonprint = ('%' + ''.join(chr(c) for c in range(0, 32) if c not in (9, 10)) + |
| 318 | ''.join(chr(c) for c in range(127, 256))) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 319 | |
| 320 | def _escape(self, data): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 321 | """Render data format suitable for inclusion in an HTML document. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 322 | |
| 323 | This includes HTML-escaping certain characters, and translating |
| 324 | control characters to a hexadecimal representation. |
| 325 | |
| 326 | Args: |
| 327 | data: The raw string data to be escaped. |
| 328 | |
| 329 | Returns: |
| 330 | An escaped version of the data. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 331 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 332 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 333 | data = data.replace(chr(13), '') |
| 334 | data = ''.join((c in self._nonprint) and ('%%%02x' % ord(c)) or |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 335 | c for c in data) |
| 336 | data = cgi.escape(data) |
| 337 | return data |
| 338 | |
| 339 | def _terminate_stream(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 340 | """Write HTML to the log file to terminate the current stream's data. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 341 | |
| 342 | Args: |
| 343 | None. |
| 344 | |
| 345 | Returns: |
| 346 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 347 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 348 | |
| 349 | self.cur_evt += 1 |
| 350 | if not self.last_stream: |
| 351 | return |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 352 | self.f.write('</pre>\n') |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 353 | self.f.write('<div class="stream-trailer block-trailer">End stream: ' + |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 354 | self.last_stream.name + '</div>\n') |
| 355 | self.f.write('</div>\n') |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 356 | self.f.write('</div>\n') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 357 | self.last_stream = None |
| 358 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 359 | def _note(self, note_type, msg, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 360 | """Write a note or one-off message to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 361 | |
| 362 | Args: |
| 363 | note_type: The type of note. This must be a value supported by the |
| 364 | accompanying multiplexed_log.css. |
| 365 | msg: The note/message to log. |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 366 | anchor: Optional internal link target. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 367 | |
| 368 | Returns: |
| 369 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 370 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 371 | |
| 372 | self._terminate_stream() |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 373 | self.f.write('<div class="' + note_type + '">\n') |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 374 | self.f.write('<pre>') |
Stephen Warren | e0d2aee | 2017-09-18 11:50:47 -0600 | [diff] [blame] | 375 | if anchor: |
| 376 | self.f.write('<a href="#%s">' % anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 377 | self.f.write(self._escape(msg)) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 378 | if anchor: |
Stephen Warren | e0d2aee | 2017-09-18 11:50:47 -0600 | [diff] [blame] | 379 | self.f.write('</a>') |
| 380 | self.f.write('\n</pre>\n') |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 381 | self.f.write('</div>\n') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 382 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 383 | def start_section(self, marker, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 384 | """Begin a new nested section in the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 385 | |
| 386 | Args: |
| 387 | marker: The name of the section that is starting. |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 388 | anchor: The value to use for the anchor. If None, a unique value |
| 389 | will be calculated and used |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 390 | |
| 391 | Returns: |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 392 | Name of the HTML anchor emitted before section. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 393 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 394 | |
| 395 | self._terminate_stream() |
| 396 | self.blocks.append(marker) |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 397 | self.timestamp_blocks.append(self._get_time()) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 398 | if not anchor: |
| 399 | self.anchor += 1 |
| 400 | anchor = str(self.anchor) |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 401 | blk_path = '/'.join(self.blocks) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 402 | self.f.write('<div class="section block" id="' + anchor + '">\n') |
| 403 | self.f.write('<div class="section-header block-header">Section: ' + |
| 404 | blk_path + '</div>\n') |
| 405 | self.f.write('<div class="section-content block-content">\n') |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 406 | self.timestamp() |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 407 | |
| 408 | return anchor |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 409 | |
| 410 | def end_section(self, marker): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 411 | """Terminate the current nested section in the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 412 | |
| 413 | This function validates proper nesting of start_section() and |
| 414 | end_section() calls. If a mismatch is found, an exception is raised. |
| 415 | |
| 416 | Args: |
| 417 | marker: The name of the section that is ending. |
| 418 | |
| 419 | Returns: |
| 420 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 421 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 422 | |
| 423 | if (not self.blocks) or (marker != self.blocks[-1]): |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 424 | raise Exception('Block nesting mismatch: "%s" "%s"' % |
| 425 | (marker, '/'.join(self.blocks))) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 426 | self._terminate_stream() |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 427 | timestamp_now = self._get_time() |
| 428 | timestamp_section_start = self.timestamp_blocks.pop() |
| 429 | delta_section = timestamp_now - timestamp_section_start |
| 430 | self._note("timestamp", |
| 431 | "TIME: SINCE-SECTION: " + str(delta_section)) |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 432 | blk_path = '/'.join(self.blocks) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 433 | self.f.write('<div class="section-trailer block-trailer">' + |
| 434 | 'End section: ' + blk_path + '</div>\n') |
| 435 | self.f.write('</div>\n') |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 436 | self.f.write('</div>\n') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 437 | self.blocks.pop() |
| 438 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 439 | def section(self, marker, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 440 | """Create a temporary section in the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 441 | |
| 442 | This function creates a context manager for Python's "with" statement, |
| 443 | which allows a certain portion of test code to be logged to a separate |
| 444 | section of the log file. |
| 445 | |
| 446 | Usage: |
| 447 | with log.section("somename"): |
| 448 | some test code |
| 449 | |
| 450 | Args: |
| 451 | marker: The name of the nested section. |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 452 | anchor: The anchor value to pass to start_section(). |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 453 | |
| 454 | Returns: |
| 455 | A context manager object. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 456 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 457 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 458 | return SectionCtxMgr(self, marker, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 459 | |
| 460 | def error(self, msg): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 461 | """Write an error note to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 462 | |
| 463 | Args: |
| 464 | msg: A message describing the error. |
| 465 | |
| 466 | Returns: |
| 467 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 468 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 469 | |
| 470 | self._note("error", msg) |
| 471 | |
| 472 | def warning(self, msg): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 473 | """Write an warning note to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 474 | |
| 475 | Args: |
| 476 | msg: A message describing the warning. |
| 477 | |
| 478 | Returns: |
| 479 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 480 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 481 | |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 482 | self.seen_warning = True |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 483 | self._note("warning", msg) |
| 484 | |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 485 | def get_and_reset_warning(self): |
| 486 | """Get and reset the log warning flag. |
| 487 | |
| 488 | Args: |
| 489 | None |
| 490 | |
| 491 | Returns: |
| 492 | Whether a warning was seen since the last call. |
| 493 | """ |
| 494 | |
| 495 | ret = self.seen_warning |
| 496 | self.seen_warning = False |
| 497 | return ret |
| 498 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 499 | def info(self, msg): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 500 | """Write an informational note to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 501 | |
| 502 | Args: |
| 503 | msg: An informational message. |
| 504 | |
| 505 | Returns: |
| 506 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 507 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 508 | |
| 509 | self._note("info", msg) |
| 510 | |
| 511 | def action(self, msg): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 512 | """Write an action note to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 513 | |
| 514 | Args: |
| 515 | msg: A message describing the action that is being logged. |
| 516 | |
| 517 | Returns: |
| 518 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 519 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 520 | |
| 521 | self._note("action", msg) |
| 522 | |
Stephen Warren | b1c556a | 2017-10-27 11:04:08 -0600 | [diff] [blame] | 523 | def _get_time(self): |
| 524 | return datetime.datetime.now() |
| 525 | |
| 526 | def timestamp(self): |
| 527 | """Write a timestamp to the log file. |
| 528 | |
| 529 | Args: |
| 530 | None |
| 531 | |
| 532 | Returns: |
| 533 | Nothing. |
| 534 | """ |
| 535 | |
| 536 | timestamp_now = self._get_time() |
| 537 | delta_prev = timestamp_now - self.timestamp_prev |
| 538 | delta_start = timestamp_now - self.timestamp_start |
| 539 | self.timestamp_prev = timestamp_now |
| 540 | |
| 541 | self._note("timestamp", |
| 542 | "TIME: NOW: " + timestamp_now.strftime("%Y/%m/%d %H:%M:%S.%f")) |
| 543 | self._note("timestamp", |
| 544 | "TIME: SINCE-PREV: " + str(delta_prev)) |
| 545 | self._note("timestamp", |
| 546 | "TIME: SINCE-START: " + str(delta_start)) |
| 547 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 548 | def status_pass(self, msg, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 549 | """Write a note to the log file describing test(s) which passed. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 550 | |
| 551 | Args: |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 552 | msg: A message describing the passed test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 553 | anchor: Optional internal link target. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 554 | |
| 555 | Returns: |
| 556 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 557 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 558 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 559 | self._note("status-pass", msg, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 560 | |
Stephen Warren | e27a6ae | 2018-02-20 12:51:55 -0700 | [diff] [blame] | 561 | def status_warning(self, msg, anchor=None): |
| 562 | """Write a note to the log file describing test(s) which passed. |
| 563 | |
| 564 | Args: |
| 565 | msg: A message describing the passed test(s). |
| 566 | anchor: Optional internal link target. |
| 567 | |
| 568 | Returns: |
| 569 | Nothing. |
| 570 | """ |
| 571 | |
| 572 | self._note("status-warning", msg, anchor) |
| 573 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 574 | def status_skipped(self, msg, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 575 | """Write a note to the log file describing skipped test(s). |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 576 | |
| 577 | Args: |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 578 | msg: A message describing the skipped test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 579 | anchor: Optional internal link target. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 580 | |
| 581 | Returns: |
| 582 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 583 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 584 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 585 | self._note("status-skipped", msg, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 586 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 587 | def status_xfail(self, msg, anchor=None): |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 588 | """Write a note to the log file describing xfailed test(s). |
| 589 | |
| 590 | Args: |
| 591 | msg: A message describing the xfailed test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 592 | anchor: Optional internal link target. |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 593 | |
| 594 | Returns: |
| 595 | Nothing. |
| 596 | """ |
| 597 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 598 | self._note("status-xfail", msg, anchor) |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 599 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 600 | def status_xpass(self, msg, anchor=None): |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 601 | """Write a note to the log file describing xpassed test(s). |
| 602 | |
| 603 | Args: |
| 604 | msg: A message describing the xpassed test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 605 | anchor: Optional internal link target. |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 606 | |
| 607 | Returns: |
| 608 | Nothing. |
| 609 | """ |
| 610 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 611 | self._note("status-xpass", msg, anchor) |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 612 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 613 | def status_fail(self, msg, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 614 | """Write a note to the log file describing failed test(s). |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 615 | |
| 616 | Args: |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 617 | msg: A message describing the failed test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 618 | anchor: Optional internal link target. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 619 | |
| 620 | Returns: |
| 621 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 622 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 623 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 624 | self._note("status-fail", msg, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 625 | |
| 626 | def get_stream(self, name, chained_file=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 627 | """Create an object to log a single stream's data into the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 628 | |
| 629 | This creates a "file-like" object that can be written to in order to |
| 630 | write a single stream's data to the log file. The implementation will |
| 631 | handle any required interleaving of data (from multiple streams) in |
| 632 | the log, in a way that makes it obvious which stream each bit of data |
| 633 | came from. |
| 634 | |
| 635 | Args: |
| 636 | name: The name of the stream. |
| 637 | chained_file: The file-like object to which all stream data should |
| 638 | be logged to in addition to this log. Can be None. |
| 639 | |
| 640 | Returns: |
| 641 | A file-like object. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 642 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 643 | |
| 644 | return LogfileStream(self, name, chained_file) |
| 645 | |
| 646 | def get_runner(self, name, chained_file=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 647 | """Create an object that executes processes and logs their output. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 648 | |
| 649 | Args: |
| 650 | name: The name of this sub-process. |
| 651 | chained_file: The file-like object to which all stream data should |
| 652 | be logged to in addition to logfile. Can be None. |
| 653 | |
| 654 | Returns: |
| 655 | A RunAndLog object. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 656 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 657 | |
| 658 | return RunAndLog(self, name, chained_file) |
| 659 | |
| 660 | def write(self, stream, data, implicit=False): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 661 | """Write stream data into the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 662 | |
| 663 | This function should only be used by instances of LogfileStream or |
| 664 | RunAndLog. |
| 665 | |
| 666 | Args: |
| 667 | stream: The stream whose data is being logged. |
| 668 | data: The data to log. |
| 669 | implicit: Boolean indicating whether data actually appeared in the |
| 670 | stream, or was implicitly generated. A valid use-case is to |
| 671 | repeat a shell prompt at the start of each separate log |
| 672 | section, which makes the log sections more readable in |
| 673 | isolation. |
| 674 | |
| 675 | Returns: |
| 676 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 677 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 678 | |
| 679 | if stream != self.last_stream: |
| 680 | self._terminate_stream() |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 681 | self.f.write('<div class="stream block">\n') |
| 682 | self.f.write('<div class="stream-header block-header">Stream: ' + |
| 683 | stream.name + '</div>\n') |
| 684 | self.f.write('<div class="stream-content block-content">\n') |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 685 | self.f.write('<pre>') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 686 | if implicit: |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 687 | self.f.write('<span class="implicit">') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 688 | self.f.write(self._escape(data)) |
| 689 | if implicit: |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 690 | self.f.write('</span>') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 691 | self.last_stream = stream |
| 692 | |
| 693 | def flush(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 694 | """Flush the log stream, to ensure correct log interleaving. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 695 | |
| 696 | Args: |
| 697 | None. |
| 698 | |
| 699 | Returns: |
| 700 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 701 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 702 | |
| 703 | self.f.flush() |