forked from Davidyz/VectorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugging.py
More file actions
65 lines (50 loc) · 1.85 KB
/
Copy pathdebugging.py
File metadata and controls
65 lines (50 loc) · 1.85 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
import atexit
import cProfile
import logging
import os
import pstats
from datetime import datetime
__LOG_DIR = os.path.expanduser("~/.local/share/vectorcode/logs/")
logger = logging.getLogger(name=__name__)
__profiler: cProfile.Profile | None = None
def _ensure_log_dir():
"""Ensure the log directory exists"""
os.makedirs(__LOG_DIR, exist_ok=True)
def finish():
"""Clean up profiling and save results"""
if __profiler is not None:
try:
__profiler.disable()
stats_file = os.path.join(
__LOG_DIR,
f"cprofile-{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.stats",
)
__profiler.dump_stats(stats_file)
print(f"cProfile stats saved to: {stats_file}")
# Print summary stats
stats = pstats.Stats(__profiler)
stats.sort_stats("cumulative")
stats.print_stats(20)
except Exception as e:
logger.warning(f"Failed to save cProfile output: {e}")
def enable():
"""Enable cProfile-based profiling and crash debugging"""
global __profiler
try:
_ensure_log_dir()
# Initialize cProfile for comprehensive profiling
__profiler = cProfile.Profile()
__profiler.enable()
atexit.register(finish)
logger.info("cProfile profiling enabled successfully")
try:
import coredumpy # noqa: F401
logger.info("coredumpy crash debugging enabled successfully")
coredumpy.patch_except(directory=__LOG_DIR)
except Exception as e:
logger.warning(
f"Crash debugging will not be available. Failed to import coredumpy: {e}"
)
except Exception as e:
logger.error(f"Failed to initialize cProfile: {e}")
logger.warning("Profiling will not be available for this session")