mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
Puts a short plain-text block (25 lines at most) on the clipboard: the Leonida commit, Windows version, the card the newest d3d12 play log names, the play settings in effect and up to 10 scrubbed error lines of that log. The block builder and the scrub (home folder, hex runs, 0x values, hex-like names after shader stage words, program/shader/pipeline/pso/hash values, 200-character cut) are pure functions in tools/bug_report_info.py. The driver version and, mostly, the video memory are not in the runtime's log and read "not in the log". No game data, no paths. Claim: LAUNCHER-COPY-REPORT (#3312) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
237 lines
11 KiB
Python
237 lines
11 KiB
Python
"""The text of the launcher's "Copy info for a bug report" button: pure functions, no game data.
|
|
|
|
import bug_report_info # after sys.path.insert(0, <this folder>), as -I needs
|
|
|
|
The block holds the Leonida version, Windows, the card the runtime's newest play log names, the play settings in
|
|
effect and the last error lines of that log, scrubbed so that nothing of the game and no user name is in it: no
|
|
shader, pipeline or program name or hash, no address, no home folder. It is at most MAX_LINES lines of at most
|
|
MAX_LINE characters. The launcher gathers the inputs (tools/leonida_launcher.py `bug_report_text`); this module
|
|
only parses and formats them, so a test feeds it synthetic text.
|
|
|
|
What the runtime logs of the card (d3d12/crates/leonida-gpu/src), through `tracing` to stderr, which
|
|
`play-nvidia.ps1 -Log` redirects to %LOCALAPPDATA%\\Leonida\\logs\\play-<stamp>.log:
|
|
|
|
* `indirect.rs` (runs_wide), at the run's first indirect execution: a field `card="<adapter description>"`;
|
|
* `host.rs` (chosen_adapter), only with LEONIDA_ADAPTER set: `adapter=<description> index=<n> memory_mb=<MB>`;
|
|
* `host.rs`, only with LEONIDA_D3D12_WARP: "host device on the software adapter (WARP)".
|
|
|
|
The driver version is not logged at all, and the video memory only in the LEONIDA_ADAPTER case.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
|
|
MAX_LINES = 25
|
|
MAX_LINE = 200
|
|
MAX_ERROR_LINES = 10
|
|
HOME = "%USERPROFILE%"
|
|
NOT_IN_LOG = "not in the log"
|
|
PLAY_LOG = re.compile(r"^play-.*\.log$", re.IGNORECASE)
|
|
|
|
# ---------------------------------------------------------------- scrubbing
|
|
|
|
ANSI = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]")
|
|
CONTROL = re.compile(r"[\x00-\x08\x0b-\x1f\x7f]")
|
|
# C:\Users\<name>, \\?\C:\Users\<name>, C:/Users/<name>, /Users/<name>, /home/<name>: the name is what is removed.
|
|
HOME_FOLDER = re.compile(
|
|
r"(?:[A-Za-z]:[\\/]+Users[\\/]+|[\\/]+(?:home|Users)[\\/]+)"
|
|
r"(?:[^\\/\s\"'<>|:*?]+(?:[ ][^\\/\s\"'<>|:*?]+)*(?=[\\/])|[^\\/\s\"'<>|:*?]+)", re.IGNORECASE)
|
|
NAMED_VALUE = re.compile(
|
|
r"(?P<key>\b\w*(?:program|shader|pipeline|pso|hash)\w*)(?P<eq>\s*=\s*)"
|
|
r"(?:\"[^\"]*\"|'[^']*'|[^\s,;)\]}]+)", re.IGNORECASE)
|
|
HEX_VALUE = re.compile(r"0x[0-9A-Fa-f]+", re.IGNORECASE)
|
|
STAGE_NAME = re.compile(r"\b(?:cs|ps|vs|gs|hs|ds|ms|as)(?P<sep>[_:=\-\s]+)(?P<name>\w+)", re.IGNORECASE)
|
|
HEX_RUN = re.compile(r"[0-9A-Fa-f]{8,}")
|
|
LEADING_HEX = re.compile(r"[0-9A-Fa-f]{4,}")
|
|
|
|
|
|
def _stage_name(match: re.Match) -> str:
|
|
"""A name after a stage word is dropped when it starts with a run of 4 or more hex digits that holds a digit, or
|
|
follows a separator other than a space (`ps_beef`, `vs=cafe`); "as added" is left alone."""
|
|
lead = LEADING_HEX.match(match.group("name"))
|
|
if not lead:
|
|
return match.group(0)
|
|
if any(c.isdigit() for c in lead.group()) or match.group("sep").strip():
|
|
return match.group(0)[:match.start("name") - match.start()] + "<name>"
|
|
return match.group(0)
|
|
|
|
|
|
def scrub_line(line: str) -> str:
|
|
"""One log line with nothing of the game or of the user left in it, cut at MAX_LINE characters:
|
|
|
|
1. a home folder (`C:\\Users\\<name>`, `/home/<name>`) becomes %USERPROFILE%;
|
|
2. the value of a `program=`, `shader=`, `pipeline=`, `pso=` or `hash=` (also `shader_hash=`) is removed;
|
|
3. a `0x...` value is removed;
|
|
4. the name after a stage word (cs, ps, vs, gs, hs, ds, ms, as) that is hex-like is removed;
|
|
5. a run of 8 or more hex digits is removed.
|
|
|
|
The cut comes last, so it cannot leave half of a value that was meant to go."""
|
|
line = CONTROL.sub("", ANSI.sub("", line.rstrip("\r\n")))
|
|
line = HOME_FOLDER.sub(HOME, line)
|
|
line = NAMED_VALUE.sub(lambda m: m.group("key") + m.group("eq") + "<removed>", line)
|
|
line = HEX_VALUE.sub("<hex>", line)
|
|
line = STAGE_NAME.sub(_stage_name, line)
|
|
# A run of digits after a dot is a timestamp's fraction (".123456789Z"): kept.
|
|
line = HEX_RUN.sub(lambda m: m.group(0) if m.group(0).isdigit() and line[m.start() - 1:m.start()] == "."
|
|
else "<hex>", line)
|
|
return line[:MAX_LINE]
|
|
|
|
|
|
def scrub_home(text: str) -> str:
|
|
"""`text` with a home folder replaced and control characters dropped, cut at MAX_LINE: for what the launcher itself
|
|
knows (the commit, Windows, the card, its own settings), where a short commit hash must stay."""
|
|
text = CONTROL.sub("", ANSI.sub("", str(text).strip()))
|
|
return HOME_FOLDER.sub(lambda m: HOME, text)[:MAX_LINE]
|
|
|
|
|
|
# ---------------------------------------------------------------- the log
|
|
|
|
ERROR_LINE = re.compile(
|
|
r"\berror\b|\bERROR\b|\bpanick?ed\b|\bpanic\b|device[ _-]?(?:lost|removed|hung)|\bremoved\b|\bexception\b"
|
|
r"|0xC0000[0-9A-Fa-f]{3}", re.IGNORECASE)
|
|
NO_ERROR = re.compile(r"\b(?:no|0|zero) errors?\b|\berrors?=0\b|\bwithout errors?\b", re.IGNORECASE)
|
|
|
|
|
|
def error_lines(text: str, limit: int = MAX_ERROR_LINES) -> list[str]:
|
|
"""The last `limit` distinct scrubbed lines of `text` that name an error, panic, lost or removed device, exception
|
|
or a 0xC0000... status, oldest first. A line repeated (a device-removed flood) is kept once, at its newest place."""
|
|
found: list[str] = []
|
|
seen: set[str] = set()
|
|
for raw in reversed(text.splitlines()):
|
|
plain = ANSI.sub("", raw)
|
|
if not ERROR_LINE.search(plain) or NO_ERROR.search(plain):
|
|
continue
|
|
line = scrub_line(plain).strip()
|
|
if not line or line in seen:
|
|
continue
|
|
seen.add(line)
|
|
found.append(line)
|
|
if len(found) >= limit:
|
|
break
|
|
found.reverse()
|
|
return found
|
|
|
|
|
|
CARD_FIELD = re.compile(r'\bcard="((?:[^"\\]|\\.)*)"')
|
|
ADAPTER_FIELD = re.compile(r"\badapter=(?:\"((?:[^\"\\]|\\.)*)\"|(.+?)(?=\s+index=\d))")
|
|
MEMORY_FIELD = re.compile(r"\bmemory_mb=(\d+)")
|
|
|
|
|
|
def gpu_from_log(text: str) -> dict:
|
|
"""The card the log names: {"name": ..., "memory_mb": int or None, "driver": None}. The driver is never in the
|
|
runtime's log. The first line that names a card wins; the memory is the one on a LEONIDA_ADAPTER line."""
|
|
info: dict = {"name": None, "memory_mb": None, "driver": None}
|
|
for raw in text.splitlines():
|
|
line = ANSI.sub("", raw)
|
|
if "software adapter (WARP)" in line and info["name"] is None:
|
|
info["name"] = "software adapter (WARP)"
|
|
card = ADAPTER_FIELD.search(line) if "LEONIDA_ADAPTER" in line else None
|
|
if card:
|
|
info["name"] = (card.group(1) or card.group(2) or "").strip() or info["name"]
|
|
memory = MEMORY_FIELD.search(line)
|
|
if memory:
|
|
info["memory_mb"] = int(memory.group(1))
|
|
break
|
|
card = CARD_FIELD.search(line)
|
|
if card and info["name"] in (None, "software adapter (WARP)"):
|
|
info["name"] = card.group(1).strip() or info["name"]
|
|
if info["name"]:
|
|
info["name"] = scrub_home(info["name"])
|
|
return info
|
|
|
|
|
|
def newest_play_log(folder: str) -> str | None:
|
|
"""The newest play-*.log of `folder` (by modification time), or None."""
|
|
try:
|
|
names = [n for n in os.listdir(folder) if PLAY_LOG.match(n)]
|
|
except OSError:
|
|
return None
|
|
best, best_time = None, -1.0
|
|
for name in names:
|
|
path = os.path.join(folder, name)
|
|
try:
|
|
if not os.path.isfile(path):
|
|
continue
|
|
stamp = os.path.getmtime(path)
|
|
except OSError:
|
|
continue
|
|
if stamp > best_time:
|
|
best, best_time = path, stamp
|
|
return best
|
|
|
|
|
|
def read_log_ends(path: str, head: int = 256 * 1024, tail: int = 2 * 1024 * 1024) -> tuple[str, str]:
|
|
"""The first `head` and the last `tail` bytes of a log as text (the card is named near the start, the errors
|
|
are near the end); ("", "") when it cannot be read. A short file comes back whole as both."""
|
|
try:
|
|
with open(path, "rb") as f:
|
|
first = f.read(head)
|
|
size = f.seek(0, os.SEEK_END)
|
|
f.seek(max(0, size - tail))
|
|
last = f.read()
|
|
except OSError:
|
|
return "", ""
|
|
return first.decode("utf-8", errors="replace"), last.decode("utf-8", errors="replace")
|
|
|
|
|
|
# ---------------------------------------------------------------- the settings
|
|
|
|
def _shown_value(value: str) -> str:
|
|
"""A value of a setting as it goes in the block: a path is not shown (it can name the user)."""
|
|
value = str(value).strip()
|
|
if re.match(r"^[A-Za-z]:[\\/]", value) or value.startswith(("\\\\", "/")) or "\\" in value:
|
|
return "<path>"
|
|
return scrub_home(value)
|
|
|
|
|
|
def describe_options(args: list[str]) -> dict:
|
|
"""What the switches PLAY passes say, for the block: resolution, upscaler, frame generation."""
|
|
def after(flag):
|
|
return args[args.index(flag) + 1] if flag in args and args.index(flag) + 1 < len(args) else None
|
|
|
|
upscaler = "off" if "-NoDlss" in args else (f"DLSS {after('-Dlss')}" if "-Dlss" in args else "DLSS (auto)")
|
|
if "-NoFrameGen" in args:
|
|
frame = "off"
|
|
elif "-FsrFrameGen" in args:
|
|
frame = "FSR 3"
|
|
elif "-FrameGen" in args:
|
|
frame = f"DLSS {after('-FrameGen')}x"
|
|
else:
|
|
frame = "auto"
|
|
return {"resolution": after("-Resolution") or "default", "upscaler": upscaler, "frame_gen": frame}
|
|
|
|
|
|
# ---------------------------------------------------------------- the block
|
|
|
|
def build_report(version: str, system: str, console: str, args: list[str], variables: dict, log_head: str | None,
|
|
log_tail: str | None, driver: str | None = None) -> str:
|
|
"""The block, at most MAX_LINES lines. `version`: leonida_version(); `system`: Windows' name and build; `console`:
|
|
the profile the next PLAY runs; `args`: the switches PLAY passes; `variables`: the LEONIDA_* values in effect;
|
|
`log_head` and `log_tail`: the ends of the newest play log, None when there is none. `driver` is only for a caller
|
|
that has it from elsewhere (the runtime does not log it)."""
|
|
gpu = gpu_from_log((log_head or "") + "\n" + (log_tail or ""))
|
|
options = describe_options(args)
|
|
shown = [f"{name}={_shown_value(value)}" for name, value in sorted(variables.items())]
|
|
head = [
|
|
"Leonida bug report info",
|
|
f"Version: {scrub_home(version)}",
|
|
f"Windows: {scrub_home(system)}",
|
|
f"GPU: {gpu['name'] or NOT_IN_LOG}",
|
|
f"Driver: {scrub_home(driver) if driver else NOT_IN_LOG}",
|
|
f"Video memory: {str(gpu['memory_mb']) + ' MB' if gpu['memory_mb'] else NOT_IN_LOG}",
|
|
f"Console profile: {scrub_home(console)}",
|
|
f"Resolution: {options['resolution']}",
|
|
f"Upscaler: {options['upscaler']}",
|
|
f"Frame generation: {options['frame_gen']}",
|
|
"Options: " + scrub_home(" ".join(_shown_value(a) for a in args) or "(defaults)"),
|
|
"Variables: " + (scrub_home("; ".join(shown)) if shown else "(none)"),
|
|
]
|
|
if log_head is None and log_tail is None:
|
|
tail = ["Last errors: no play log found (turn on Log in Settings or use Run in diagnostic mode)"]
|
|
else:
|
|
errors = error_lines(log_tail or log_head or "", min(MAX_ERROR_LINES, MAX_LINES - len(head) - 1))
|
|
tail = ["Last errors of the newest play log:" if errors else "Last errors of the newest play log: none"]
|
|
tail += errors
|
|
return "\n".join((head + tail)[:MAX_LINES]) + "\n"
|