Skip to content

Commit c277cca

Browse files
committed
fix(pc):
1 parent fcbb832 commit c277cca

File tree

2 files changed

+86
-199
lines changed

2 files changed

+86
-199
lines changed

agentic_security/logutils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
# logger.add(sys.stdout, format=LOG_FORMAT, level="DEBUG", colorize=True)
2323
import logging
2424
import logging.config
25+
import time
26+
from collections.abc import Callable, Coroutine
27+
from functools import wraps
2528
from os import getenv
29+
from typing import Any, ParamSpec, TypeVar
2630

2731
LOGGER_NAME = None
2832

@@ -93,3 +97,50 @@ def set_log_level_to_info():
9397

9498
# Set initial log level
9599
set_log_level_to_info()
100+
101+
102+
# Define generic type variables for return type and parameters
103+
R = TypeVar("R")
104+
P = ParamSpec("P")
105+
106+
107+
def time_execution_sync(
108+
additional_text: str = "",
109+
) -> Callable[[Callable[P, R]], Callable[P, R]]:
110+
def decorator(func: Callable[P, R]) -> Callable[P, R]:
111+
@wraps(func)
112+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
113+
start_time = time.time()
114+
result = func(*args, **kwargs)
115+
execution_time = time.time() - start_time
116+
logger.debug(
117+
f"{additional_text} Execution time: {execution_time:.2f} seconds"
118+
)
119+
return result
120+
121+
return wrapper
122+
123+
return decorator
124+
125+
126+
def time_execution_async(
127+
additional_text: str = "",
128+
) -> Callable[
129+
[Callable[P, Coroutine[Any, Any, R]]], Callable[P, Coroutine[Any, Any, R]]
130+
]:
131+
def decorator(
132+
func: Callable[P, Coroutine[Any, Any, R]]
133+
) -> Callable[P, Coroutine[Any, Any, R]]:
134+
@wraps(func)
135+
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
136+
start_time = time.time()
137+
result = await func(*args, **kwargs)
138+
execution_time = time.time() - start_time
139+
logger.debug(
140+
f"{additional_text} Execution time: {execution_time:.2f} seconds"
141+
)
142+
return result
143+
144+
return wrapper
145+
146+
return decorator

0 commit comments

Comments
 (0)