updating and refactoring codebase for improved performance and maintainability
This commit is contained in:
+23
-12
@@ -1,13 +1,21 @@
|
||||
"""Unified logging utility for automatic file and function name tracking."""
|
||||
|
||||
import sys
|
||||
import inspect
|
||||
import logging
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, Sequence
|
||||
|
||||
from SYS.rich_display import console_for
|
||||
# SYS.rich_display deferred: rich (~100ms) loaded lazily on first log output.
|
||||
_rich_display_mod: Any = None
|
||||
|
||||
|
||||
def _console_for(file):
|
||||
global _rich_display_mod
|
||||
if _rich_display_mod is None:
|
||||
import SYS.rich_display as _m
|
||||
_rich_display_mod = _m
|
||||
return _rich_display_mod.console_for(file)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -73,7 +81,8 @@ def _is_rich_renderable(value: Any) -> bool:
|
||||
|
||||
|
||||
def _caller_location(depth: int = 1) -> tuple[str, str]:
|
||||
frame = inspect.currentframe()
|
||||
import inspect as _inspect
|
||||
frame = _inspect.currentframe()
|
||||
current = frame
|
||||
try:
|
||||
for _ in range(max(0, int(depth))):
|
||||
@@ -160,7 +169,7 @@ def debug(*args, **kwargs) -> None:
|
||||
|
||||
if len(args) == 1 and _is_rich_renderable(args[0]):
|
||||
renderable = args[0]
|
||||
console_for(target_file).print(renderable)
|
||||
_console_for(target_file).print(renderable)
|
||||
file_name, func_name = _caller_location(depth=1)
|
||||
caller_name = f"{file_name}.{func_name}" if file_name and func_name else ""
|
||||
_debug_db_log(caller_name=caller_name, message=f"<rich:{type(renderable).__name__}>")
|
||||
@@ -200,7 +209,8 @@ def debug_inspect(
|
||||
|
||||
# Compute caller prefix (same as log()).
|
||||
prefix = None
|
||||
frame = inspect.currentframe()
|
||||
import inspect as _inspect
|
||||
frame = _inspect.currentframe()
|
||||
if frame is not None and frame.f_back is not None:
|
||||
caller_frame = frame.f_back
|
||||
try:
|
||||
@@ -215,7 +225,7 @@ def debug_inspect(
|
||||
# Render.
|
||||
from rich import inspect as rich_inspect
|
||||
|
||||
console = console_for(file)
|
||||
console = _console_for(file)
|
||||
# If the caller provides a title, treat it as authoritative.
|
||||
# Only fall back to the automatic [file.func] prefix when no title is supplied.
|
||||
effective_title = title
|
||||
@@ -266,12 +276,13 @@ def log(*args, **kwargs) -> None:
|
||||
add_prefix = _DEBUG_ENABLED
|
||||
|
||||
# Get the calling frame
|
||||
frame = inspect.currentframe()
|
||||
import inspect as _inspect
|
||||
frame = _inspect.currentframe()
|
||||
if frame is None:
|
||||
file = kwargs.pop("file", sys.stdout)
|
||||
sep = kwargs.pop("sep", " ")
|
||||
end = kwargs.pop("end", "\n")
|
||||
console_for(file).print(*args, sep=sep, end=end)
|
||||
_console_for(file).print(*args, sep=sep, end=end)
|
||||
return
|
||||
|
||||
caller_frame = frame.f_back
|
||||
@@ -279,7 +290,7 @@ def log(*args, **kwargs) -> None:
|
||||
file = kwargs.pop("file", sys.stdout)
|
||||
sep = kwargs.pop("sep", " ")
|
||||
end = kwargs.pop("end", "\n")
|
||||
console_for(file).print(*args, sep=sep, end=end)
|
||||
_console_for(file).print(*args, sep=sep, end=end)
|
||||
return
|
||||
|
||||
try:
|
||||
@@ -302,9 +313,9 @@ def log(*args, **kwargs) -> None:
|
||||
end = kwargs.pop("end", "\n")
|
||||
if add_prefix:
|
||||
prefix = f"[{file_name}.{func_name}]"
|
||||
console_for(file).print(prefix, *args, sep=sep, end=end)
|
||||
_console_for(file).print(prefix, *args, sep=sep, end=end)
|
||||
else:
|
||||
console_for(file).print(*args, sep=sep, end=end)
|
||||
_console_for(file).print(*args, sep=sep, end=end)
|
||||
|
||||
# Log to database if available
|
||||
if _DB_LOGGER:
|
||||
@@ -316,4 +327,4 @@ def log(*args, **kwargs) -> None:
|
||||
pass
|
||||
finally:
|
||||
del frame
|
||||
del caller_frame
|
||||
del caller_frame
|
||||
Reference in New Issue
Block a user