File size: 2,800 Bytes
c262d1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
"""
Shared logging utilities for GAIA test scripts
"""

import sys
from datetime import datetime
from contextlib import contextmanager


class TeeOutput:
    """Class to write to both console and log file simultaneously"""
    def __init__(self, log_file):
        self.log_file = log_file
        self.terminal = sys.stdout
        
    def write(self, message):
        self.terminal.write(message)
        self.log_file.write(message)
        self.log_file.flush()  # Ensure immediate write to file
        
    def flush(self):
        self.terminal.flush()
        self.log_file.flush()


@contextmanager
def test_logger(test_name: str, question_id: str = None):
    """
    Context manager for test logging that writes to both console and file
    
    Args:
        test_name: Name of the test (e.g., "specific_question", "routing")
        question_id: Optional question ID for specific question tests
    
    Usage:
        with test_logger("specific_question", "abc123") as log_file:
            print("This will go to both console and log file")
    """
    # Create timestamped log file
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    
    if question_id:
        log_filename = f"logs/test_{test_name}_{question_id[:8]}_{timestamp}.log"
        log_title = f"GAIA {test_name.title().replace('_', ' ')} Test - Question: {question_id}"
    else:
        log_filename = f"logs/test_{test_name}_{timestamp}.log"
        log_title = f"GAIA {test_name.title().replace('_', ' ')} Test"
    
    # Set up logging to both console and file
    with open(log_filename, 'w') as log_file:
        # Write header to log file
        log_file.write(f"{log_title}\n")
        log_file.write(f"Timestamp: {datetime.now().isoformat()}\n")
        log_file.write("=" * 60 + "\n\n")
        
        # Redirect stdout to both console and log file
        original_stdout = sys.stdout
        sys.stdout = TeeOutput(log_file)
        
        try:
            print(f"📝 Logging to: {log_filename}")
            yield log_filename
        finally:
            # Restore original stdout
            sys.stdout = original_stdout
            
        # Final message (only to console)
        print(f"\n📋 Test completed. Full log saved to: {log_filename}")


def create_log_filename(test_name: str, question_id: str = None) -> str:
    """
    Create a standardized log filename
    
    Args:
        test_name: Name of the test
        question_id: Optional question ID
        
    Returns:
        Formatted log filename with timestamp
    """
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    
    if question_id:
        return f"logs/test_{test_name}_{question_id[:8]}_{timestamp}.log"
    else:
        return f"logs/test_{test_name}_{timestamp}.log"