add console.timing context

Log debug messages with timing for different processes.
This commit is contained in:
Masen Furer 2025-02-06 11:15:39 -08:00
parent 8a0e288f71
commit a944a34e63
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95

View File

@ -2,8 +2,10 @@
from __future__ import annotations
import contextlib
import inspect
import shutil
import time
from pathlib import Path
from types import FrameType
@ -317,3 +319,20 @@ def status(*args, **kwargs):
A new status.
"""
return _console.status(*args, **kwargs)
@contextlib.contextmanager
def timing(msg: str):
"""Create a context manager to time a block of code.
Args:
msg: The message to display.
Yields:
None.
"""
start = time.time()
try:
yield
finally:
debug(f"[white]\\[timing] {msg}: {time.time() - start:.2f}s[/white]")