(mode: str = "a")
| 72 | |
| 73 | |
| 74 | def _open_log_file(mode: str = "a") -> bool: |
| 75 | global _log_file_handle, _file_writing_disabled, _disable_reason |
| 76 | _close_log_file() |
| 77 | try: |
| 78 | # 使用较大缓冲区(64 KB),由 writer 线程定期 flush,减少系统调用 |
| 79 | _log_file_handle = open(_cached_log_file, mode, encoding="utf-8", buffering=65536) |
| 80 | return True |
| 81 | except (PermissionError, OSError, IOError) as e: |
| 82 | _file_writing_disabled = True |
| 83 | _disable_reason = str(e) |
| 84 | print(f"Warning: Cannot open log file, disabling file writing: {e}", file=sys.stderr) |
| 85 | print("Log messages will continue to display in console only.", file=sys.stderr) |
| 86 | return False |
| 87 | except Exception as e: |
| 88 | print(f"Warning: Failed to open log file: {e}", file=sys.stderr) |
| 89 | return False |
| 90 | |
| 91 | |
| 92 | def _clear_log_file(): |
no test coverage detected