Skip to content

Commit 6f44803

Browse files
committed
feat(utils): add function to find messages in logs
Added a new function `find_msgs_in_logs` to search for messages in log files based on a given regex pattern. This function supports seeking from a specific offset and timestamp, and can return either all matching lines or just the first match.
1 parent fb7d096 commit 6f44803

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

cardano_node_tests/utils/logfiles.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,38 @@ def add_ignore_rule(
274274
infile.write(f"{files_glob};;{skip_after};;{regex}\n")
275275

276276

277+
def find_msgs_in_logs(
278+
regex: str,
279+
logfile: pl.Path,
280+
seek_offset: int,
281+
timestamp: float,
282+
only_first: bool = False,
283+
) -> tp.List[str]:
284+
"""Find messages in log."""
285+
regex_comp = re.compile(regex)
286+
lines_found = []
287+
for logfile_rec in _get_rotated_logs(
288+
logfile=pl.Path(logfile), seek=seek_offset, timestamp=timestamp
289+
):
290+
with open(logfile_rec.logfile, encoding="utf-8") as infile:
291+
infile.seek(logfile_rec.seek)
292+
for line in infile:
293+
if regex_comp.search(line):
294+
lines_found.append(line)
295+
if only_first:
296+
break
297+
if lines_found and only_first:
298+
break
299+
return lines_found
300+
301+
277302
def check_msgs_presence_in_logs(
278303
regex_pairs: tp.List[tp.Tuple[str, str]],
279304
seek_offsets: tp.Dict[str, int],
280305
state_dir: pl.Path,
281306
timestamp: float,
282307
) -> tp.List[str]:
283-
"""Make sure the expected messages are present in logs."""
308+
"""Check if the expected messages are present in logs."""
284309
errors = []
285310
for files_glob, regex in regex_pairs:
286311
regex_comp = re.compile(regex)

0 commit comments

Comments
 (0)