refactor(clew): configure logging at startup

This commit is contained in:
2026-09-07 14:45:09 -04:00
parent 5f1b29858c
commit d6cdf482e4

View File

@@ -5,6 +5,7 @@ import logging
import os import os
import shutil import shutil
import sys import sys
from datetime import UTC, datetime
from pathlib import Path from pathlib import Path
from rpycdec.rpa import extract_rpa from rpycdec.rpa import extract_rpa
@@ -58,7 +59,8 @@ def prepare_output(output_root: Path, overwrite: bool) -> Path:
for name in MANAGED_DIRS: for name in MANAGED_DIRS:
path = output_root / name path = output_root / name
if path.exists() and not overwrite: if path.exists() and not overwrite:
raise SystemExit(f"clew: refusing to overwrite existing output path: {path}") LOGGER.critical("refusing to overwrite existing output path: %s", path)
raise SystemExit(2)
if overwrite: if overwrite:
for name in MANAGED_DIRS: for name in MANAGED_DIRS:
@@ -78,7 +80,8 @@ def normalize_suffixes(suffixes: list[str] | None) -> list[str] | None:
normalized = [] normalized = []
for suffix in suffixes: for suffix in suffixes:
if not suffix: if not suffix:
raise SystemExit("clew: suffix must not be empty") LOGGER.critical("suffix must not be empty")
raise SystemExit(2)
if not suffix.startswith("."): if not suffix.startswith("."):
suffix = f".{suffix}" suffix = f".{suffix}"
normalized.append(suffix) normalized.append(suffix)
@@ -109,8 +112,22 @@ def extract_archive(
LOGGER.info("extracted archive: %s", archive_relative) LOGGER.info("extracted archive: %s", archive_relative)
def configure_logging() -> None: def configure_logging(output_root: Path) -> None:
logging.basicConfig(level=logging.INFO, format="%(message)s") log_level = logging.INFO
timestamp = datetime.now(UTC).strftime("%Y%m%dT%H%M%SZ")
logs_dir = output_root / "logs"
logs_dir.mkdir(parents=True, exist_ok=True)
log_file = logs_dir / f"{logging.getLevelName(log_level)}-clew-{timestamp}.log"
formatter = logging.Formatter("%(message)s")
file_handler = logging.FileHandler(log_file, encoding="utf-8")
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logging.basicConfig(level=log_level, handlers=[file_handler, stream_handler])
def run(args: argparse.Namespace) -> None: def run(args: argparse.Namespace) -> None:
@@ -118,10 +135,10 @@ def run(args: argparse.Namespace) -> None:
output_root = resolve_output_root(args.output_root) output_root = resolve_output_root(args.output_root)
if not game_root.is_dir(): if not game_root.is_dir():
raise SystemExit(f"clew: game root is not a directory: {game_root}") LOGGER.critical("game root is not a directory: %s", game_root)
raise SystemExit(2)
suffixes = normalize_suffixes(args.suffix) suffixes = normalize_suffixes(args.suffix)
configure_logging()
extracted_dir = prepare_output(output_root, args.overwrite) extracted_dir = prepare_output(output_root, args.overwrite)
LOGGER.info("copying loose files") LOGGER.info("copying loose files")
@@ -138,4 +155,5 @@ def run(args: argparse.Namespace) -> None:
if __name__ == "__main__": if __name__ == "__main__":
args = parse_args(sys.argv[1:]) args = parse_args(sys.argv[1:])
configure_logging(resolve_output_root(args.output_root))
run(args) run(args)