""" Real-time log file monitor for debug mode Displays log file content in a visible CMD window """ import sys import time import os from pathlib import Path def monitor_log_file(log_path: str, job_name: str): """Monitor log file and display content in real-time""" # Set console title os.system(f'title Job Debug - {job_name}') print("=" * 80) print(f"JOB DEBUG MONITOR - {job_name}") print("=" * 80) print(f"Log file: {log_path}") print("=" * 80) print() print("Waiting for log file to be created...") print() # Wait for log file to exist max_wait = 30 # seconds waited = 0 while not os.path.exists(log_path) and waited < max_wait: time.sleep(0.5) waited += 0.5 if not os.path.exists(log_path): print(f"ERROR: Log file not created after {max_wait} seconds") print(f"Expected path: {log_path}") input("\nPress Enter to close...") return print(f"Log file found! Monitoring output...\n") print("=" * 80) print() # Open file and start tailing with open(log_path, 'r', encoding='utf-8', errors='replace') as f: # Read existing content content = f.read() if content: print(content, end='') # Monitor for new content last_position = f.tell() no_change_count = 0 max_no_change = 600 # 60 seconds with 0.1s sleep = 600 iterations while True: # Check if file still exists (job might have finished and cleaned up) if not os.path.exists(log_path): break # Read new content f.seek(last_position) new_content = f.read() if new_content: print(new_content, end='', flush=True) last_position = f.tell() no_change_count = 0 else: no_change_count += 1 # If no changes for a while, check if parent process still exists if no_change_count >= max_no_change: # Assume job has finished break time.sleep(0.1) # Check every 100ms print() print("=" * 80) print("MONITORING COMPLETE - Job finished or log file closed") print("=" * 80) input("\nPress Enter to close this window...") if __name__ == '__main__': if len(sys.argv) != 3: print("Usage: python log_monitor.py ") sys.exit(1) log_file_path = sys.argv[1] job_name = sys.argv[2] try: monitor_log_file(log_file_path, job_name) except KeyboardInterrupt: print("\n\nMonitoring interrupted by user") except Exception as e: print(f"\n\nERROR: {e}") import traceback traceback.print_exc() input("\nPress Enter to close...")