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