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