Skip to content

Commit bd4de5e

Browse files
committed
Show first log entries on review screen
1 parent eec8ec6 commit bd4de5e

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

swattool/logsview.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,37 @@ def _get_log_highlights(loglines: list[str], failure: swatbuild.Failure
200200
return highlight_lines
201201

202202

203+
_cached_log_highlights: dict[tuple[swatbuild.Failure, str],
204+
dict[int, _Highlight]] = {}
205+
206+
207+
def _get_cached_log_highlights(failure: swatbuild.Failure, logname: str,
208+
loglines: list[str]
209+
) -> dict[int, _Highlight]:
210+
highlights = _cached_log_highlights.get((failure, logname), None)
211+
if highlights:
212+
return highlights
213+
214+
highlights = _get_log_highlights(loglines, failure)
215+
_cached_log_highlights[(failure, logname)] = highlights
216+
return highlights
217+
218+
219+
def get_log_highlights(failure: swatbuild.Failure, logname: str
220+
) -> list[str]:
221+
"""Get log highlights for a given log file."""
222+
logdata = _load_log(failure, logname)
223+
if not logdata:
224+
return []
225+
226+
loglines = logdata.splitlines()
227+
228+
highlights = _get_cached_log_highlights(failure, logname, loglines)
229+
230+
return [loglines[line - 1] for line in highlights
231+
if highlights[line].in_menu]
232+
233+
203234
def _show_log(loglines: list[str], selected_line: Optional[int],
204235
highlight_lines: dict[int, _Highlight],
205236
preview_height: Optional[int], preview_width: Optional[int]):
@@ -248,7 +279,7 @@ def show_log_menu(failure: swatbuild.Failure, logname: str) -> bool:
248279

249280
utils.clear()
250281
loglines = logdata.splitlines()
251-
highlights = _get_log_highlights(loglines, failure)
282+
highlights = _get_cached_log_highlights(failure, logname, loglines)
252283

253284
entries = ["View entire log file|",
254285
"View entire log file in default editor|",

swattool/review.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"""Swatbot review functions."""
44

55
import logging
6+
import shutil
67
import sys
8+
import textwrap
79
from typing import Any, Optional
810

911
import click
@@ -262,6 +264,27 @@ def review_menu(builds: list[swatbuild.Build],
262264
return (new_entry, need_refresh)
263265

264266

267+
def _show_infos(build: swatbuild.Build, userinfo: userdata.UserInfo):
268+
# Reserve chars for spacing.
269+
reserved = 8
270+
termwidth = shutil.get_terminal_size((80, 20)).columns
271+
width = termwidth - reserved
272+
maxhighlights = 5
273+
274+
print(build.format_description(userinfo, width))
275+
print()
276+
277+
failure = build.get_first_failure()
278+
highlights = logsview.get_log_highlights(failure, "stdio")
279+
wrapped_highlights = [textwrap.indent(line, " " * 4)
280+
for highlight in highlights[:maxhighlights]
281+
for line in textwrap.wrap(highlight, width)
282+
]
283+
print("Key log infos:")
284+
print("\n".join(wrapped_highlights))
285+
print()
286+
287+
265288
def review_failures(builds: list[swatbuild.Build],
266289
userinfos: userdata.UserInfos,
267290
open_autobuilder_url: bool,
@@ -285,16 +308,10 @@ def review_failures(builds: list[swatbuild.Build],
285308
if open_swatbot_url:
286309
click.launch(build.swat_url)
287310
if open_stdio_url:
288-
if build.test in ["a-full", "a-quick"]:
289-
# TODO: can we do anything better here ?
290-
logger.warning("Test is %s, "
291-
"fail log might be the log of a child",
292-
build.test)
293311
build.get_first_failure().open_log_url()
294312

295313
if show_infos:
296-
print(build.format_description(userinfo))
297-
print()
314+
_show_infos(build, userinfo)
298315
show_infos = False
299316

300317
prev_entry = entry

swattool/swatbuild.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import enum
66
import json
77
import logging
8-
import shutil
98
from datetime import datetime
109
from typing import Any, Iterable, Optional
1110

@@ -250,7 +249,8 @@ def get_field(field):
250249

251250
return tuple(get_field(k) for k in keys)
252251

253-
def format_description(self, userinfo: userdata.UserInfo) -> str:
252+
def format_description(self, userinfo: userdata.UserInfo,
253+
maxwidth: int) -> str:
254254
"""Get info on one given failure in a pretty way."""
255255
def format_field(field):
256256
if field == Field.STATUS:
@@ -289,12 +289,7 @@ def format_field(field):
289289
desc = tabulate.tabulate(table, tablefmt="plain")
290290

291291
if userinfo.notes:
292-
# Reserve chars for spacing.
293-
reserved = 8
294-
termwidth = shutil.get_terminal_size((80, 20)).columns
295-
width = termwidth - reserved
296-
297-
wrapped = userinfo.get_wrapped_notes(width, " " * 4)
292+
wrapped = userinfo.get_wrapped_notes(maxwidth, " " * 4)
298293
desc += f"\n\n{Field.USER_NOTES}:\n{wrapped}"
299294

300295
return desc

0 commit comments

Comments
 (0)