Manage quiet-mode output for AI agents. In quiet mode, opens a log file for verbose fbuild output redirection. Use emit() for compact summary lines (always printed to stdout). Use log_file property to redirect subprocess output to the log. Does NOT redirect sys.stdout — CrashPatter
| 57 | |
| 58 | |
| 59 | class QuietContext: |
| 60 | """Manage quiet-mode output for AI agents. |
| 61 | |
| 62 | In quiet mode, opens a log file for verbose fbuild output redirection. |
| 63 | Use emit() for compact summary lines (always printed to stdout). |
| 64 | Use log_file property to redirect subprocess output to the log. |
| 65 | |
| 66 | Does NOT redirect sys.stdout — CrashPatternInterceptor in main() |
| 67 | needs to see serial output on the real stdout. |
| 68 | |
| 69 | Usage:: |
| 70 | |
| 71 | with QuietContext(args.quiet) as qctx: |
| 72 | run_fbuild_deploy(..., log_file=qctx.log_file) |
| 73 | qctx.emit("BUILD+FLASH ok 12.3s") |
| 74 | """ |
| 75 | |
| 76 | def __init__(self, quiet: bool, log_path: Path | None = None) -> None: |
| 77 | self.quiet = quiet |
| 78 | self.log_path = log_path or Path(".autoresearch_last.log") |
| 79 | self._log_file: Any = None |
| 80 | |
| 81 | def __enter__(self) -> "QuietContext": |
| 82 | if self.quiet: |
| 83 | self._log_file = open(self.log_path, "w", encoding="utf-8") # noqa: SIM115 |
| 84 | return self |
| 85 | |
| 86 | def __exit__(self, *_args: Any) -> None: |
| 87 | if self._log_file is not None: |
| 88 | self._log_file.close() |
| 89 | self._log_file = None |
| 90 | |
| 91 | @property |
| 92 | def log_file(self) -> Any: |
| 93 | """Log file handle (open in quiet mode, None otherwise).""" |
| 94 | return self._log_file |
| 95 | |
| 96 | def emit(self, msg: str) -> None: |
| 97 | """Print a compact summary line to stdout (always visible).""" |
| 98 | print(msg) |
| 99 | |
| 100 | def emit_log_path(self) -> None: |
| 101 | """Print log file path so agents know where to look on failure.""" |
| 102 | if self.quiet: |
| 103 | print(f"LOG {self.log_path}") |
| 104 | |
| 105 | |
| 106 | # ============================================================ |
no outgoing calls