Skip to content

Commit

Permalink
add logging_function
Browse files Browse the repository at this point in the history
  • Loading branch information
barabum0 committed Oct 29, 2023
1 parent ec406ff commit edf6d11
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions devtools/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
MYPY = False
if MYPY:
from types import FrameType
from typing import Any, Generator, List, Optional, Union
from typing import Any, Generator, List, Optional, Union, Callable

pformat = PrettyFormat(
indent_step=int(os.getenv('PY_DEVTOOLS_INDENT', 4)),
Expand Down Expand Up @@ -108,9 +108,10 @@ def __repr__(self) -> StrType:
class Debug:
output_class = DebugOutput

def __init__(self, *, warnings: 'Optional[bool]' = None, highlight: 'Optional[bool]' = None):
def __init__(self, *, warnings: 'Optional[bool]' = None, highlight: 'Optional[bool]' = None, logger_function: 'Callable[[str], None]' = None):
self._show_warnings = env_bool(warnings, 'PY_DEVTOOLS_WARNINGS', True)
self._highlight = highlight
self._logger_function = logger_function

def __call__(
self,
Expand All @@ -122,7 +123,10 @@ def __call__(
) -> 'Any':
d_out = self._process(args, kwargs, frame_depth_)
s = d_out.str(use_highlight(self._highlight, file_))
print(s, file=file_, flush=flush_)
if self._logger_function:
self._logger_function(s)
else:
print(s, file=file_, flush=flush_)
if kwargs:
return (*args, kwargs)
elif len(args) == 1:
Expand Down

0 comments on commit edf6d11

Please sign in to comment.