清空日志文件(启动时调用,此时 writer 线程尚未启动,直接操作安全)
()
| 90 | |
| 91 | |
| 92 | def _clear_log_file(): |
| 93 | """清空日志文件(启动时调用,此时 writer 线程尚未启动,直接操作安全)""" |
| 94 | global _file_writing_disabled, _disable_reason |
| 95 | try: |
| 96 | with open(_cached_log_file, "w", encoding="utf-8") as f: |
| 97 | pass # 覆盖清空 |
| 98 | _open_log_file("a") |
| 99 | except (PermissionError, OSError, IOError) as e: |
| 100 | _file_writing_disabled = True |
| 101 | _disable_reason = str(e) |
| 102 | print( |
| 103 | f"Warning: File system appears to be read-only or permission denied. " |
| 104 | f"Disabling log file writing: {e}", |
| 105 | file=sys.stderr, |
| 106 | ) |
| 107 | print("Log messages will continue to display in console only.", file=sys.stderr) |
| 108 | except Exception as e: |
| 109 | print(f"Warning: Failed to clear log file: {e}", file=sys.stderr) |
| 110 | |
| 111 | |
| 112 | # ----------------------------------------------------------------- |