-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
108 lines (82 loc) · 3.08 KB
/
debug.py
File metadata and controls
108 lines (82 loc) · 3.08 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
MacAgent-OS Debug - Debug exports like OpenWork
Export runtime state for troubleshooting
"""
import json
import logging
from pathlib import Path
from datetime import datetime
from typing import Dict, Any
logger = logging.getLogger("MacAgent-Debug")
class DebugExporter:
"""Export debug information"""
def __init__(self, storage_dir: str = None):
if storage_dir is None:
storage_dir = Path(__file__).parent / "data" / "debug"
self.storage_dir = Path(storage_dir)
self.storage_dir.mkdir(parents=True, exist_ok=True)
def export_runtime(self, state: Dict) -> str:
"""Export runtime state"""
timestamp = datetime.now().isoformat()
filename = f"runtime_{timestamp.replace(':', '-')}.json"
filepath = self.storage_dir / filename
data = {
"exported_at": timestamp,
"runtime": state
}
with open(filepath, "w") as f:
json.dump(data, f, indent=2)
logger.info(f"Exported runtime to {filepath}")
return str(filepath)
def export_logs(self, log_file: str = "/tmp/macagent.log") -> str:
"""Export logs"""
timestamp = datetime.now().isoformat()
filename = f"logs_{timestamp.replace(':', '-')}.log"
filepath = self.storage_dir / filename
try:
with open(log_file, "r") as src:
content = src.read()
with open(filepath, "w") as f:
f.write(content)
logger.info(f"Exported logs to {filepath}")
return str(filepath)
except FileNotFoundError:
return ""
def export_full(self, runtime_state: Dict) -> Dict[str, str]:
"""Export everything"""
files = {}
# Runtime
files["runtime"] = self.export_runtime(runtime_state)
# Logs
log_file = self.export_logs()
if log_file:
files["logs"] = log_file
timestamp = datetime.now().isoformat()
summary = {
"exported_at": timestamp,
"files": files
}
# Export summary
summary_file = self.storage_dir / f"debug_summary_{timestamp.replace(':', '-')}.json"
with open(summary_file, "w") as f:
json.dump(summary, f, indent=2)
files["summary"] = str(summary_file)
return files
def list_exports(self) -> list:
"""List all exports"""
exports = list(self.storage_dir.glob("*.json"))
exports.extend(self.storage_dir.glob("*.log"))
return sorted(exports, key=lambda p: p.stat().st_mtime, reverse=True)
# Singleton
_exporter: DebugExporter = None
def get_debug_exporter() -> DebugExporter:
global _exporter
if _exporter is None:
_exporter = DebugExporter()
return _exporter
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
exporter = get_debug_exporter()
print("Debug Exports:")
for f in exporter.list_exports():
print(f" {f.name}")