File size: 2,730 Bytes
509a107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from pathlib import Path
from datetime import datetime
from typing import Optional, Dict, Any
import base64

class NDJSONLogger:
    def __init__(self, log_dir: str = "logs"):
        self.log_dir = Path(log_dir)
        self.log_dir.mkdir(exist_ok=True)
        self.frames_dir = self.log_dir / "frames"
        self.frames_dir.mkdir(exist_ok=True)
        self.log_file = self.log_dir / "logs.ndjson"
    
    def log_frame(self, frame_id: str, thumbnail: Optional[bytes], timestamp: str):
        try:
            if thumbnail:
                frame_path = self.frames_dir / f"{frame_id}.png"
                with open(frame_path, "wb") as f:
                    f.write(thumbnail)
                
                thumbnail_b64 = base64.b64encode(thumbnail).decode('utf-8')
            else:
                thumbnail_b64 = None
            
            log_entry = {
                "type": "frame_capture",
                "timestamp": timestamp,
                "frame_id": frame_id,
                "thumbnail": thumbnail_b64 if thumbnail_b64 else None,
                "has_thumbnail": thumbnail is not None
            }
            
            self._write_log(log_entry)
            
        except Exception as e:
            print(f"Frame logging error: {e}")
    
    def log_analysis(self, analysis_data: Dict[str, Any]):
        try:
            log_entry = {
                "type": "analysis",
                "timestamp": analysis_data.get("timestamp", datetime.now().isoformat()),
                "data": analysis_data
            }
            
            self._write_log(log_entry)
            
        except Exception as e:
            print(f"Analysis logging error: {e}")
    
    def log_event(self, event_type: str, data: Dict[str, Any]):
        try:
            log_entry = {
                "type": event_type,
                "timestamp": datetime.now().isoformat(),
                "data": data
            }
            
            self._write_log(log_entry)
            
        except Exception as e:
            print(f"Event logging error: {e}")
    
    def _write_log(self, entry: Dict[str, Any]):
        try:
            with open(self.log_file, "a") as f:
                json.dump(entry, f)
                f.write("\n")
                f.flush()
                
        except Exception as e:
            print(f"Write log error: {e}")
    
    def clear_logs(self):
        try:
            if self.log_file.exists():
                self.log_file.unlink()
            
            for frame_file in self.frames_dir.glob("*.png"):
                frame_file.unlink()
                
        except Exception as e:
            print(f"Clear logs error: {e}")