klj
This commit is contained in:
144
cmdnat/pipe.py
144
cmdnat/pipe.py
@@ -173,6 +173,89 @@ def _apply_log_filter(lines: Sequence[str], filter_text: Optional[str]) -> List[
|
||||
return filtered
|
||||
|
||||
|
||||
def _collapse_repeated_log_lines(lines: Sequence[str]) -> List[str]:
|
||||
collapsed: List[str] = []
|
||||
last_line: Optional[str] = None
|
||||
repeat_count = 0
|
||||
|
||||
def flush() -> None:
|
||||
nonlocal last_line, repeat_count
|
||||
if last_line is None:
|
||||
return
|
||||
if repeat_count > 1:
|
||||
collapsed.append(f"{last_line} [repeated x{repeat_count}]")
|
||||
else:
|
||||
collapsed.append(last_line)
|
||||
last_line = None
|
||||
repeat_count = 0
|
||||
|
||||
for raw in lines:
|
||||
line = str(raw or "")
|
||||
if line == last_line:
|
||||
repeat_count += 1
|
||||
continue
|
||||
flush()
|
||||
last_line = line
|
||||
repeat_count = 1
|
||||
|
||||
flush()
|
||||
return collapsed
|
||||
|
||||
|
||||
def _is_noisy_mpv_log_line(line: str) -> bool:
|
||||
text = str(line or "")
|
||||
lower = text.lower()
|
||||
noisy_tokens = (
|
||||
"client connected",
|
||||
"client disconnected",
|
||||
"destroying client handle",
|
||||
"set property: options/log-file",
|
||||
"set property: options/msg-level",
|
||||
"run command: script-message-to, flags=64, args=[target=\"console\", args=\"log\"",
|
||||
"run command: script-message-to, flags=64, args=[target=\"console\", args=\"print\"",
|
||||
)
|
||||
if any(token in lower for token in noisy_tokens):
|
||||
return True
|
||||
|
||||
startup_prefixes = (
|
||||
"mpv v",
|
||||
" built on ",
|
||||
"libplacebo version:",
|
||||
"ffmpeg version:",
|
||||
"ffmpeg library versions:",
|
||||
" libavcodec",
|
||||
" libavdevice",
|
||||
" libavfilter",
|
||||
" libavformat",
|
||||
" libavutil",
|
||||
" libswresample",
|
||||
" libswscale",
|
||||
"configuration:",
|
||||
"list of enabled features:",
|
||||
"built with ndebug.",
|
||||
)
|
||||
# Log lines look like [timestamp][level][module] content — strip 3 brackets.
|
||||
stripped = lower.split(']', 3)
|
||||
payload = stripped[-1].strip() if len(stripped) > 1 else lower.strip()
|
||||
return any(prefix in payload for prefix in startup_prefixes)
|
||||
|
||||
|
||||
def _focus_mpv_log_lines(lines: Sequence[str]) -> List[str]:
|
||||
focused = [str(line) for line in lines if not _is_noisy_mpv_log_line(str(line))]
|
||||
return _collapse_repeated_log_lines(focused)
|
||||
|
||||
|
||||
def _focus_db_log_rows(
|
||||
rows: Sequence[tuple[Any, Any, Any, Any]],
|
||||
) -> List[str]:
|
||||
rendered: List[str] = []
|
||||
for timestamp, level, _module, message in rows:
|
||||
ts = str(timestamp or "").strip()
|
||||
prefix = f"[{ts}] [{level}] " if ts else f"[{level}] "
|
||||
rendered.append(prefix + str(message or ""))
|
||||
return _collapse_repeated_log_lines(rendered)
|
||||
|
||||
|
||||
def _slice_mpv_log_to_latest_run(lines: Sequence[str]) -> List[str]:
|
||||
startup_pattern = re.compile(r"\bmpv v\d", re.IGNORECASE)
|
||||
collected = list(lines)
|
||||
@@ -296,7 +379,7 @@ def _try_enable_mpv_file_logging(mpv_log_path: str, *, attempts: int = 3) -> boo
|
||||
{
|
||||
"command": ["set_property",
|
||||
"options/msg-level",
|
||||
"all=v"]
|
||||
"cplayer=info,ffmpeg=error,ipc=warn"]
|
||||
}
|
||||
)
|
||||
ok = bool(
|
||||
@@ -1585,10 +1668,9 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
|
||||
try:
|
||||
# Default: keep `.pipe` quiet even if debug is enabled.
|
||||
# With -log: enable debug and route it to stdout (pipeable), plus enable mpv log-file.
|
||||
# With -log: keep transport chatter quiet and print an explicit focused
|
||||
# report later in this function.
|
||||
if log_requested:
|
||||
set_debug(True)
|
||||
set_thread_stream(sys.stdout)
|
||||
try:
|
||||
log_dir = _repo_log_dir()
|
||||
mpv_log_path = str((log_dir / "medeia-mpv.log").resolve())
|
||||
@@ -1606,8 +1688,6 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
debug(f"MPV log file: {mpv_log_path}")
|
||||
|
||||
# Try to enable mpv file logging on the currently running instance.
|
||||
# (If mpv wasn't started with --log-file, this may not work everywhere.)
|
||||
try:
|
||||
@@ -1620,7 +1700,7 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
mpv_live = MPV()
|
||||
if mpv_live.is_running():
|
||||
mpv_live.set_property("options/log-file", mpv_log_path)
|
||||
mpv_live.set_property("options/msg-level", "all=v")
|
||||
mpv_live.set_property("options/msg-level", "cplayer=info,ffmpeg=error,ipc=warn")
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
@@ -2319,7 +2399,9 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
break
|
||||
|
||||
latest_run_tail = _slice_mpv_log_to_latest_run(tail_lines)
|
||||
filtered_tail = _apply_log_filter(latest_run_tail, log_filter_text)
|
||||
filtered_tail = _focus_mpv_log_lines(
|
||||
_apply_log_filter(latest_run_tail, log_filter_text)
|
||||
)
|
||||
|
||||
helper_heartbeat = _get_mpv_property("user-data/medeia-pipeline-ready")
|
||||
helper_status = "not running"
|
||||
@@ -2327,25 +2409,6 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
helper_status = f"running ({helper_heartbeat})"
|
||||
|
||||
print(f"Pipeline helper: {helper_status}")
|
||||
if filtered_tail:
|
||||
title = "MPV log (latest run tail"
|
||||
if log_filter_text:
|
||||
title += f" filtered by '{log_filter_text}'"
|
||||
title += "):"
|
||||
print(title)
|
||||
for ln in filtered_tail:
|
||||
print(ln)
|
||||
else:
|
||||
if log_filter_text:
|
||||
print(f"MPV log (tail): <no entries match filter '{log_filter_text}'>")
|
||||
else:
|
||||
print("MPV log (latest run tail): <empty>")
|
||||
print(
|
||||
"Note: On some Windows builds, mpv cannot start writing to --log-file after launch."
|
||||
)
|
||||
print(
|
||||
"If you need full [main2] logs, restart mpv so it starts with --log-file."
|
||||
)
|
||||
|
||||
# Print database logs for mpv module (helper + lua output)
|
||||
try:
|
||||
@@ -2367,12 +2430,9 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
if marker_msg:
|
||||
print(f"Run marker: {marker_msg}")
|
||||
if mpv_logs:
|
||||
for timestamp, level, _module, message in mpv_logs:
|
||||
ts = str(timestamp or "").strip()
|
||||
if ts:
|
||||
print(f"[{ts}] [{level}] {message}")
|
||||
else:
|
||||
print(f"[{level}] {message}")
|
||||
print("Medios MPV logs (latest run, focused):")
|
||||
for line in _focus_db_log_rows(mpv_logs):
|
||||
print(line)
|
||||
else:
|
||||
if log_filter_text:
|
||||
print(f"(no latest-run mpv logs found matching '{log_filter_text}')")
|
||||
@@ -2382,6 +2442,20 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
debug(f"Could not fetch database logs: {e}")
|
||||
pass
|
||||
|
||||
if filtered_tail:
|
||||
title = "MPV core log (latest run, filtered)"
|
||||
if log_filter_text:
|
||||
title += f" for '{log_filter_text}'"
|
||||
title += ":"
|
||||
print(title)
|
||||
for ln in filtered_tail:
|
||||
print(ln)
|
||||
else:
|
||||
if log_filter_text:
|
||||
print(f"MPV core log: <no focused entries match filter '{log_filter_text}'>")
|
||||
else:
|
||||
print("MPV core log: <no focused entries>")
|
||||
|
||||
fallback_logs = [
|
||||
("Medeia Lua log file tail", str(_lua_log_file())),
|
||||
("Medeia helper log file tail", str(_helper_log_file())),
|
||||
@@ -2391,7 +2465,7 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
lines = _tail_text_file(path, max_lines=120)
|
||||
except Exception:
|
||||
lines = []
|
||||
lines = _apply_log_filter(lines, log_filter_text)
|
||||
lines = _collapse_repeated_log_lines(_apply_log_filter(lines, log_filter_text))
|
||||
if not lines:
|
||||
continue
|
||||
print(f"{title}:")
|
||||
@@ -2481,7 +2555,7 @@ def _start_mpv(
|
||||
mpv_log_path = (start_opts or {}).get("mpv_log_path")
|
||||
if isinstance(mpv_log_path, str) and mpv_log_path.strip():
|
||||
extra_args.append(f"--log-file={mpv_log_path}")
|
||||
extra_args.append("--msg-level=all=v")
|
||||
extra_args.append("--msg-level=cplayer=info,ffmpeg=error,ipc=warn")
|
||||
|
||||
# Always start MPV with the bundled Lua script via MPV class.
|
||||
mpv.start(
|
||||
|
||||
Reference in New Issue
Block a user