clan-cli: Improved --debug output by indenting commands, add TRACE_DEPTH environment variable

This commit is contained in:
Qubasa
2024-11-11 21:22:25 +07:00
parent 162c4148aa
commit d3f0107f4d
5 changed files with 111 additions and 33 deletions

View File

@@ -1,13 +1,24 @@
import logging
import os
import shlex
from clan_app import main
from clan_cli.custom_logger import get_caller
from clan_cli.custom_logger import get_callers
log = logging.getLogger(__name__)
def print_trace(msg: str) -> None:
trace_depth = int(os.environ.get("TRACE_DEPTH", "0"))
callers = get_callers(2, 2 + trace_depth)
if "run_no_stdout" in callers[0]:
callers = get_callers(3, 3 + trace_depth)
callers_str = "\n".join(f"{i+1}: {caller}" for i, caller in enumerate(callers))
log.debug(f"{msg} \nCallers: \n{callers_str}")
def run(args: list[str]) -> None:
cmd = shlex.join(["clan", *args])
log.debug(f"$ {cmd} \nCaller: {get_caller()}")
print_trace(f"$ {cmd}")
main(args)