import logging import os import platform def setup_logger(name: str = "hf_demos", level: str | None = None) -> logging.Logger: logger = logging.getLogger(name) if logger.handlers: return logger # already configured lvl = (level or os.environ.get("LOG_LEVEL", "INFO")).upper() logger.setLevel(getattr(logging, lvl, logging.INFO)) ch = logging.StreamHandler() fmt = logging.Formatter( "%(asctime)s [%(levelname)s] [%(name)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) ch.setFormatter(fmt) logger.addHandler(ch) logger.propagate = False return logger def log_cpu_info(logger: logging.Logger) -> None: """Log CPU model, core count, architecture, and platform at startup.""" model = "" physical_cores: set[tuple[str, str]] = set() cur_physical_id = "" try: with open("/proc/cpuinfo") as f: for line in f: if line.startswith("model name") and not model: model = line.split(":", 1)[1].strip() elif line.startswith("physical id"): cur_physical_id = line.split(":", 1)[1].strip() elif line.startswith("core id"): physical_cores.add((cur_physical_id, line.split(":", 1)[1].strip())) except OSError: model = platform.processor() or "unknown" logical_cores = os.cpu_count() or 0 n_physical = len(physical_cores) if physical_cores else logical_cores arch = platform.machine() plat = platform.platform() logger.info( f"CPU: {model} | {n_physical} physical, {logical_cores} logical cores | {arch} | {plat}" )