Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions nanshe/util/prof.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def log_call(logger,
to_log_call=True,
to_print_args=False,
to_print_time=True,
to_print_exception=False):
to_print_exception=False,
to_print_result=False):
"""
Takes a given logger and uses it to log entering and leaving the
decorated callable. Intended to be used as a decorator that takes a few
Expand Down Expand Up @@ -165,6 +166,9 @@ def log_call(logger,
function, which can be changed at
runtime.

to_print_result(bool): Whether to print the result from the
function call.

Returns:
log_call_decorator: For performing the actual wrapping.
"""
Expand All @@ -189,7 +193,8 @@ def log_call_decorator(callable):
@wrappers.static_variables(to_log_call=to_log_call,
to_print_args=to_print_args,
to_print_time=to_print_time,
to_print_exception=to_print_exception)
to_print_exception=to_print_exception,
to_print_result=to_print_result)
def log_call_callable_wrapped(*args, **kwargs):
"""
This is what will replace the original callable. It will behave
Expand Down Expand Up @@ -243,6 +248,12 @@ def log_call_callable_wrapped(*args, **kwargs):
end_time = time.time()
diff_time += (end_time - start_time)

# Log the result if needed.
if log_call_callable_wrapped.to_print_result:
logger.debug(
"Result: \"" + str(result) + "\"" + "\"."
)

# Log that we have exited the callable in question.
logger.debug(
"Exiting callable: \"" + callable.__name__ + "\"."
Expand Down