Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 1 | # Copyright (c) 2015 Stephen Warren |
| 2 | # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: GPL-2.0 |
| 5 | |
| 6 | # Generate an HTML-formatted log file containing multiple streams of data, |
| 7 | # each represented in a well-delineated/-structured fashion. |
| 8 | |
| 9 | import cgi |
| 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 |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 105 | |
| 106 | def close(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 107 | """Clean up any resources managed by this object.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 108 | pass |
| 109 | |
Stephen Warren | 118e37e | 2016-01-22 12:30:11 -0700 | [diff] [blame] | 110 | def run(self, cmd, cwd=None, ignore_errors=False): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 111 | """Run a command as a sub-process, and log the results. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 112 | |
Simon Glass | 4134caf | 2016-07-03 09:40:38 -0600 | [diff] [blame^] | 113 | The output is available at self.output which can be useful if there is |
| 114 | an exception. |
| 115 | |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 116 | Args: |
| 117 | cmd: The command to execute. |
| 118 | cwd: The directory to run the command in. Can be None to use the |
| 119 | current directory. |
Stephen Warren | 118e37e | 2016-01-22 12:30:11 -0700 | [diff] [blame] | 120 | ignore_errors: Indicate whether to ignore errors. If True, the |
| 121 | function will simply return if the command cannot be executed |
| 122 | or exits with an error code, otherwise an exception will be |
| 123 | raised if such problems occur. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 124 | |
| 125 | Returns: |
Simon Glass | 9f3c9e9 | 2016-07-03 09:40:37 -0600 | [diff] [blame] | 126 | The output as a string. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 127 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 128 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 129 | msg = '+' + ' '.join(cmd) + '\n' |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 130 | if self.chained_file: |
| 131 | self.chained_file.write(msg) |
| 132 | self.logfile.write(self, msg) |
| 133 | |
| 134 | try: |
| 135 | p = subprocess.Popen(cmd, cwd=cwd, |
| 136 | stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 137 | (stdout, stderr) = p.communicate() |
| 138 | output = '' |
| 139 | if stdout: |
| 140 | if stderr: |
| 141 | output += 'stdout:\n' |
| 142 | output += stdout |
| 143 | if stderr: |
| 144 | if stdout: |
| 145 | output += 'stderr:\n' |
| 146 | output += stderr |
| 147 | exit_status = p.returncode |
| 148 | exception = None |
| 149 | except subprocess.CalledProcessError as cpe: |
| 150 | output = cpe.output |
| 151 | exit_status = cpe.returncode |
| 152 | exception = cpe |
| 153 | except Exception as e: |
| 154 | output = '' |
| 155 | exit_status = 0 |
| 156 | exception = e |
| 157 | if output and not output.endswith('\n'): |
| 158 | output += '\n' |
Stephen Warren | 118e37e | 2016-01-22 12:30:11 -0700 | [diff] [blame] | 159 | if exit_status and not exception and not ignore_errors: |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 160 | exception = Exception('Exit code: ' + str(exit_status)) |
| 161 | if exception: |
| 162 | output += str(exception) + '\n' |
| 163 | self.logfile.write(self, output) |
| 164 | if self.chained_file: |
| 165 | self.chained_file.write(output) |
Simon Glass | 4134caf | 2016-07-03 09:40:38 -0600 | [diff] [blame^] | 166 | |
| 167 | # Store the output so it can be accessed if we raise an exception. |
| 168 | self.output = output |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 169 | if exception: |
| 170 | raise exception |
Simon Glass | 9f3c9e9 | 2016-07-03 09:40:37 -0600 | [diff] [blame] | 171 | return output |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 172 | |
| 173 | class SectionCtxMgr(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 174 | """A context manager for Python's "with" statement, which allows a certain |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 175 | portion of test code to be logged to a separate section of the log file. |
| 176 | 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] | 177 | class rather than directly.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 178 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 179 | def __init__(self, log, marker, anchor): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 180 | """Initialize a new object. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 181 | |
| 182 | Args: |
| 183 | log: The Logfile object to log to. |
| 184 | marker: The name of the nested log section. |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 185 | anchor: The anchor value to pass to start_section(). |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 186 | |
| 187 | Returns: |
| 188 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 189 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 190 | |
| 191 | self.log = log |
| 192 | self.marker = marker |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 193 | self.anchor = anchor |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 194 | |
| 195 | def __enter__(self): |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 196 | self.anchor = self.log.start_section(self.marker, self.anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 197 | |
| 198 | def __exit__(self, extype, value, traceback): |
| 199 | self.log.end_section(self.marker) |
| 200 | |
| 201 | class Logfile(object): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 202 | """Generates an HTML-formatted log file containing multiple streams of |
| 203 | data, each represented in a well-delineated/-structured fashion.""" |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 204 | |
| 205 | def __init__(self, fn): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 206 | """Initialize a new object. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 207 | |
| 208 | Args: |
| 209 | fn: The filename to write to. |
| 210 | |
| 211 | Returns: |
| 212 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 213 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 214 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 215 | self.f = open(fn, 'wt') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 216 | self.last_stream = None |
| 217 | self.blocks = [] |
| 218 | self.cur_evt = 1 |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 219 | self.anchor = 0 |
| 220 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 221 | shutil.copy(mod_dir + '/multiplexed_log.css', os.path.dirname(fn)) |
| 222 | self.f.write('''\ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 223 | <html> |
| 224 | <head> |
| 225 | <link rel="stylesheet" type="text/css" href="multiplexed_log.css"> |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 226 | <script src="http://code.jquery.com/jquery.min.js"></script> |
| 227 | <script> |
| 228 | $(document).ready(function () { |
| 229 | // Copy status report HTML to start of log for easy access |
| 230 | sts = $(".block#status_report")[0].outerHTML; |
| 231 | $("tt").prepend(sts); |
| 232 | |
| 233 | // Add expand/contract buttons to all block headers |
| 234 | btns = "<span class=\\\"block-expand hidden\\\">[+] </span>" + |
| 235 | "<span class=\\\"block-contract\\\">[-] </span>"; |
| 236 | $(".block-header").prepend(btns); |
| 237 | |
| 238 | // Pre-contract all blocks which passed, leaving only problem cases |
| 239 | // expanded, to highlight issues the user should look at. |
| 240 | // Only top-level blocks (sections) should have any status |
| 241 | passed_bcs = $(".block-content:has(.status-pass)"); |
| 242 | // Some blocks might have multiple status entries (e.g. the status |
| 243 | // report), so take care not to hide blocks with partial success. |
| 244 | passed_bcs = passed_bcs.not(":has(.status-fail)"); |
| 245 | passed_bcs = passed_bcs.not(":has(.status-xfail)"); |
| 246 | passed_bcs = passed_bcs.not(":has(.status-xpass)"); |
| 247 | passed_bcs = passed_bcs.not(":has(.status-skipped)"); |
| 248 | // Hide the passed blocks |
| 249 | passed_bcs.addClass("hidden"); |
| 250 | // Flip the expand/contract button hiding for those blocks. |
| 251 | bhs = passed_bcs.parent().children(".block-header") |
| 252 | bhs.children(".block-expand").removeClass("hidden"); |
| 253 | bhs.children(".block-contract").addClass("hidden"); |
| 254 | |
| 255 | // Add click handler to block headers. |
| 256 | // The handler expands/contracts the block. |
| 257 | $(".block-header").on("click", function (e) { |
| 258 | var header = $(this); |
| 259 | var content = header.next(".block-content"); |
| 260 | var expanded = !content.hasClass("hidden"); |
| 261 | if (expanded) { |
| 262 | content.addClass("hidden"); |
| 263 | header.children(".block-expand").first().removeClass("hidden"); |
| 264 | header.children(".block-contract").first().addClass("hidden"); |
| 265 | } else { |
| 266 | header.children(".block-contract").first().removeClass("hidden"); |
| 267 | header.children(".block-expand").first().addClass("hidden"); |
| 268 | content.removeClass("hidden"); |
| 269 | } |
| 270 | }); |
| 271 | |
| 272 | // When clicking on a link, expand the target block |
| 273 | $("a").on("click", function (e) { |
| 274 | var block = $($(this).attr("href")); |
| 275 | var header = block.children(".block-header"); |
| 276 | var content = block.children(".block-content").first(); |
| 277 | header.children(".block-contract").first().removeClass("hidden"); |
| 278 | header.children(".block-expand").first().addClass("hidden"); |
| 279 | content.removeClass("hidden"); |
| 280 | }); |
| 281 | }); |
| 282 | </script> |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 283 | </head> |
| 284 | <body> |
| 285 | <tt> |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 286 | ''') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 287 | |
| 288 | def close(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 289 | """Close the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 290 | |
| 291 | After calling this function, no more data may be written to the log. |
| 292 | |
| 293 | Args: |
| 294 | None. |
| 295 | |
| 296 | Returns: |
| 297 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 298 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 299 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 300 | self.f.write('''\ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 301 | </tt> |
| 302 | </body> |
| 303 | </html> |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 304 | ''') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 305 | self.f.close() |
| 306 | |
| 307 | # The set of characters that should be represented as hexadecimal codes in |
| 308 | # the log file. |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 309 | _nonprint = ('%' + ''.join(chr(c) for c in range(0, 32) if c not in (9, 10)) + |
| 310 | ''.join(chr(c) for c in range(127, 256))) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 311 | |
| 312 | def _escape(self, data): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 313 | """Render data format suitable for inclusion in an HTML document. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 314 | |
| 315 | This includes HTML-escaping certain characters, and translating |
| 316 | control characters to a hexadecimal representation. |
| 317 | |
| 318 | Args: |
| 319 | data: The raw string data to be escaped. |
| 320 | |
| 321 | Returns: |
| 322 | An escaped version of the data. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 323 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 324 | |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 325 | data = data.replace(chr(13), '') |
| 326 | data = ''.join((c in self._nonprint) and ('%%%02x' % ord(c)) or |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 327 | c for c in data) |
| 328 | data = cgi.escape(data) |
| 329 | return data |
| 330 | |
| 331 | def _terminate_stream(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 332 | """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] | 333 | |
| 334 | Args: |
| 335 | None. |
| 336 | |
| 337 | Returns: |
| 338 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 339 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 340 | |
| 341 | self.cur_evt += 1 |
| 342 | if not self.last_stream: |
| 343 | return |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 344 | self.f.write('</pre>\n') |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 345 | self.f.write('<div class="stream-trailer block-trailer">End stream: ' + |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 346 | self.last_stream.name + '</div>\n') |
| 347 | self.f.write('</div>\n') |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 348 | self.f.write('</div>\n') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 349 | self.last_stream = None |
| 350 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 351 | def _note(self, note_type, msg, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 352 | """Write a note or one-off message to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 353 | |
| 354 | Args: |
| 355 | note_type: The type of note. This must be a value supported by the |
| 356 | accompanying multiplexed_log.css. |
| 357 | msg: The note/message to log. |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 358 | anchor: Optional internal link target. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 359 | |
| 360 | Returns: |
| 361 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 362 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 363 | |
| 364 | self._terminate_stream() |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 365 | self.f.write('<div class="' + note_type + '">\n') |
| 366 | if anchor: |
| 367 | self.f.write('<a href="#%s">\n' % anchor) |
| 368 | self.f.write('<pre>') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 369 | self.f.write(self._escape(msg)) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 370 | self.f.write('\n</pre>\n') |
| 371 | if anchor: |
| 372 | self.f.write('</a>\n') |
| 373 | self.f.write('</div>\n') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 374 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 375 | def start_section(self, marker, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 376 | """Begin a new nested section in the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 377 | |
| 378 | Args: |
| 379 | marker: The name of the section that is starting. |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 380 | anchor: The value to use for the anchor. If None, a unique value |
| 381 | will be calculated and used |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 382 | |
| 383 | Returns: |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 384 | Name of the HTML anchor emitted before section. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 385 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 386 | |
| 387 | self._terminate_stream() |
| 388 | self.blocks.append(marker) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 389 | if not anchor: |
| 390 | self.anchor += 1 |
| 391 | anchor = str(self.anchor) |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 392 | blk_path = '/'.join(self.blocks) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 393 | self.f.write('<div class="section block" id="' + anchor + '">\n') |
| 394 | self.f.write('<div class="section-header block-header">Section: ' + |
| 395 | blk_path + '</div>\n') |
| 396 | self.f.write('<div class="section-content block-content">\n') |
| 397 | |
| 398 | return anchor |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 399 | |
| 400 | def end_section(self, marker): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 401 | """Terminate the current nested section in the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 402 | |
| 403 | This function validates proper nesting of start_section() and |
| 404 | end_section() calls. If a mismatch is found, an exception is raised. |
| 405 | |
| 406 | Args: |
| 407 | marker: The name of the section that is ending. |
| 408 | |
| 409 | Returns: |
| 410 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 411 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 412 | |
| 413 | if (not self.blocks) or (marker != self.blocks[-1]): |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 414 | raise Exception('Block nesting mismatch: "%s" "%s"' % |
| 415 | (marker, '/'.join(self.blocks))) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 416 | self._terminate_stream() |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 417 | blk_path = '/'.join(self.blocks) |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 418 | self.f.write('<div class="section-trailer block-trailer">' + |
| 419 | 'End section: ' + blk_path + '</div>\n') |
| 420 | self.f.write('</div>\n') |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 421 | self.f.write('</div>\n') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 422 | self.blocks.pop() |
| 423 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 424 | def section(self, marker, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 425 | """Create a temporary section in the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 426 | |
| 427 | This function creates a context manager for Python's "with" statement, |
| 428 | which allows a certain portion of test code to be logged to a separate |
| 429 | section of the log file. |
| 430 | |
| 431 | Usage: |
| 432 | with log.section("somename"): |
| 433 | some test code |
| 434 | |
| 435 | Args: |
| 436 | marker: The name of the nested section. |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 437 | anchor: The anchor value to pass to start_section(). |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 438 | |
| 439 | Returns: |
| 440 | A context manager object. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 441 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 442 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 443 | return SectionCtxMgr(self, marker, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 444 | |
| 445 | def error(self, msg): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 446 | """Write an error note to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 447 | |
| 448 | Args: |
| 449 | msg: A message describing the error. |
| 450 | |
| 451 | Returns: |
| 452 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 453 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 454 | |
| 455 | self._note("error", msg) |
| 456 | |
| 457 | def warning(self, msg): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 458 | """Write an warning note to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 459 | |
| 460 | Args: |
| 461 | msg: A message describing the warning. |
| 462 | |
| 463 | Returns: |
| 464 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 465 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 466 | |
| 467 | self._note("warning", msg) |
| 468 | |
| 469 | def info(self, msg): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 470 | """Write an informational note to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 471 | |
| 472 | Args: |
| 473 | msg: An informational message. |
| 474 | |
| 475 | Returns: |
| 476 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 477 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 478 | |
| 479 | self._note("info", msg) |
| 480 | |
| 481 | def action(self, msg): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 482 | """Write an action note to the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 483 | |
| 484 | Args: |
| 485 | msg: A message describing the action that is being logged. |
| 486 | |
| 487 | Returns: |
| 488 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 489 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 490 | |
| 491 | self._note("action", msg) |
| 492 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 493 | def status_pass(self, msg, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 494 | """Write a note to the log file describing test(s) which passed. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 495 | |
| 496 | Args: |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 497 | msg: A message describing the passed test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 498 | anchor: Optional internal link target. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 499 | |
| 500 | Returns: |
| 501 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 502 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 503 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 504 | self._note("status-pass", msg, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 505 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 506 | def status_skipped(self, msg, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 507 | """Write a note to the log file describing skipped test(s). |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 508 | |
| 509 | Args: |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 510 | msg: A message describing the skipped test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 511 | anchor: Optional internal link target. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 512 | |
| 513 | Returns: |
| 514 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 515 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 516 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 517 | self._note("status-skipped", msg, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 518 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 519 | def status_xfail(self, msg, anchor=None): |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 520 | """Write a note to the log file describing xfailed test(s). |
| 521 | |
| 522 | Args: |
| 523 | msg: A message describing the xfailed test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 524 | anchor: Optional internal link target. |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 525 | |
| 526 | Returns: |
| 527 | Nothing. |
| 528 | """ |
| 529 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 530 | self._note("status-xfail", msg, anchor) |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 531 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 532 | def status_xpass(self, msg, anchor=None): |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 533 | """Write a note to the log file describing xpassed test(s). |
| 534 | |
| 535 | Args: |
| 536 | msg: A message describing the xpassed test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 537 | anchor: Optional internal link target. |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 538 | |
| 539 | Returns: |
| 540 | Nothing. |
| 541 | """ |
| 542 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 543 | self._note("status-xpass", msg, anchor) |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 544 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 545 | def status_fail(self, msg, anchor=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 546 | """Write a note to the log file describing failed test(s). |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 547 | |
| 548 | Args: |
Stephen Warren | 25b0524 | 2016-01-27 23:57:51 -0700 | [diff] [blame] | 549 | msg: A message describing the failed test(s). |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 550 | anchor: Optional internal link target. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 551 | |
| 552 | Returns: |
| 553 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 554 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 555 | |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 556 | self._note("status-fail", msg, anchor) |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 557 | |
| 558 | def get_stream(self, name, chained_file=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 559 | """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] | 560 | |
| 561 | This creates a "file-like" object that can be written to in order to |
| 562 | write a single stream's data to the log file. The implementation will |
| 563 | handle any required interleaving of data (from multiple streams) in |
| 564 | the log, in a way that makes it obvious which stream each bit of data |
| 565 | came from. |
| 566 | |
| 567 | Args: |
| 568 | name: The name of the stream. |
| 569 | chained_file: The file-like object to which all stream data should |
| 570 | be logged to in addition to this log. Can be None. |
| 571 | |
| 572 | Returns: |
| 573 | A file-like object. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 574 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 575 | |
| 576 | return LogfileStream(self, name, chained_file) |
| 577 | |
| 578 | def get_runner(self, name, chained_file=None): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 579 | """Create an object that executes processes and logs their output. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 580 | |
| 581 | Args: |
| 582 | name: The name of this sub-process. |
| 583 | chained_file: The file-like object to which all stream data should |
| 584 | be logged to in addition to logfile. Can be None. |
| 585 | |
| 586 | Returns: |
| 587 | A RunAndLog object. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 588 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 589 | |
| 590 | return RunAndLog(self, name, chained_file) |
| 591 | |
| 592 | def write(self, stream, data, implicit=False): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 593 | """Write stream data into the log file. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 594 | |
| 595 | This function should only be used by instances of LogfileStream or |
| 596 | RunAndLog. |
| 597 | |
| 598 | Args: |
| 599 | stream: The stream whose data is being logged. |
| 600 | data: The data to log. |
| 601 | implicit: Boolean indicating whether data actually appeared in the |
| 602 | stream, or was implicitly generated. A valid use-case is to |
| 603 | repeat a shell prompt at the start of each separate log |
| 604 | section, which makes the log sections more readable in |
| 605 | isolation. |
| 606 | |
| 607 | Returns: |
| 608 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 609 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 610 | |
| 611 | if stream != self.last_stream: |
| 612 | self._terminate_stream() |
Stephen Warren | e3f2a50 | 2016-02-03 16:46:34 -0700 | [diff] [blame] | 613 | self.f.write('<div class="stream block">\n') |
| 614 | self.f.write('<div class="stream-header block-header">Stream: ' + |
| 615 | stream.name + '</div>\n') |
| 616 | self.f.write('<div class="stream-content block-content">\n') |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 617 | self.f.write('<pre>') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 618 | if implicit: |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 619 | self.f.write('<span class="implicit">') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 620 | self.f.write(self._escape(data)) |
| 621 | if implicit: |
Stephen Warren | 3deb896 | 2016-01-26 13:41:31 -0700 | [diff] [blame] | 622 | self.f.write('</span>') |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 623 | self.last_stream = stream |
| 624 | |
| 625 | def flush(self): |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 626 | """Flush the log stream, to ensure correct log interleaving. |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 627 | |
| 628 | Args: |
| 629 | None. |
| 630 | |
| 631 | Returns: |
| 632 | Nothing. |
Stephen Warren | 75e731e | 2016-01-26 13:41:30 -0700 | [diff] [blame] | 633 | """ |
Stephen Warren | 10e5063 | 2016-01-15 11:15:24 -0700 | [diff] [blame] | 634 | |
| 635 | self.f.flush() |