Skip to content

Commit 42d611c

Browse files
committed
nanshe/util/prof.py: Provide the ability to log the result.
1 parent 4813b36 commit 42d611c

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

nanshe/util/prof.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def log_call(logger,
124124
to_log_call=True,
125125
to_print_args=False,
126126
to_print_time=True,
127-
to_print_exception=False):
127+
to_print_exception=False,
128+
to_print_result=False):
128129
"""
129130
Takes a given logger and uses it to log entering and leaving the
130131
decorated callable. Intended to be used as a decorator that takes a few
@@ -163,6 +164,9 @@ def log_call(logger,
163164
function, which can be changed at
164165
runtime.
165166
167+
to_print_result(bool): Whether to print the result from the
168+
function call.
169+
166170
Returns:
167171
log_call_decorator: For performing the actual wrapping.
168172
"""
@@ -187,7 +191,8 @@ def log_call_decorator(callable):
187191
@wrappers.static_variables(to_log_call=to_log_call,
188192
to_print_args=to_print_args,
189193
to_print_time=to_print_time,
190-
to_print_exception=to_print_exception)
194+
to_print_exception=to_print_exception,
195+
to_print_result=to_print_result)
191196
def log_call_callable_wrapped(*args, **kwargs):
192197
"""
193198
This is what will replace the original callable. It will behave
@@ -241,6 +246,12 @@ def log_call_callable_wrapped(*args, **kwargs):
241246
end_time = time.time()
242247
diff_time += (end_time - start_time)
243248

249+
# Log the result if needed.
250+
if log_call_callable_wrapped.to_print_result:
251+
logger.debug(
252+
"Result: \"" + str(result) + "\"" + "\"."
253+
)
254+
244255
# Log that we have exited the callable in question.
245256
logger.debug(
246257
"Exiting callable: \"" + callable.__name__ + "\"."

0 commit comments

Comments
 (0)