From fd55049657c17207c3c8109caa32b7a1c2288ede Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 20:15:37 -0700 Subject: [PATCH] =?UTF-8?q?refactor(cli-misc):=20logs.py=20=E2=80=94=20pas?= =?UTF-8?q?s=20filter=20kwargs=20through=20instead=20of=20re-listing=20the?= =?UTF-8?q?m?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hermes_cli/logs.py | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/hermes_cli/logs.py b/hermes_cli/logs.py index 8ee79f5cbe..590af0e4bb 100644 --- a/hermes_cli/logs.py +++ b/hermes_cli/logs.py @@ -164,27 +164,13 @@ def tail_log( print("\n--- stopped ---") -def _read_tail( - path: Path, - num_lines: int, - *, - has_filters: bool = False, - min_level: Optional[str] = None, - session_filter: Optional[str] = None, - since: Optional[datetime] = None, - component_prefixes: Optional[Sequence[str]] = None, -) -> list: - """Read the last *num_lines* matching lines from a log file.""" +def _read_tail(path: Path, num_lines: int, *, has_filters: bool = False, **filters) -> list: + """Read the last *num_lines* matching lines; ``filters`` are ``_matches_filters`` kwargs.""" if not has_filters: return _read_last_n_lines(path, num_lines) # Over-read so enough lines survive filtering. raw_lines = _read_last_n_lines(path, max(num_lines * 20, 2000)) - filtered = [ - l for l in raw_lines - if _matches_filters(l, min_level=min_level, session_filter=session_filter, - since=since, component_prefixes=component_prefixes) - ] - return filtered[-num_lines:] + return [l for l in raw_lines if _matches_filters(l, **filters)][-num_lines:] def _read_all_lines(path: Path) -> list: @@ -223,14 +209,7 @@ def _read_last_n_lines(path: Path, n: int) -> list: return _read_all_lines(path)[-n:] -def _follow_log( - path: Path, - *, - min_level: Optional[str] = None, - session_filter: Optional[str] = None, - since: Optional[datetime] = None, - component_prefixes: Optional[Sequence[str]] = None, -) -> None: +def _follow_log(path: Path, **filters) -> None: """Poll a log file for new content and print matching lines.""" with open(path, "r", encoding="utf-8", errors="replace") as f: f.seek(0, 2) @@ -238,8 +217,7 @@ def _follow_log( line = f.readline() if not line: time.sleep(0.3) - elif _matches_filters(line, min_level=min_level, session_filter=session_filter, - since=since, component_prefixes=component_prefixes): + elif _matches_filters(line, **filters): print(line, end="") sys.stdout.flush()