Skip to content

Commit

Permalink
Make debug output redirectable
Browse files Browse the repository at this point in the history
  • Loading branch information
pschanely committed Aug 17, 2024
1 parent e03d270 commit 17c92dc
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions crosshair/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
MutableMapping,
Optional,
Set,
TextIO,
Tuple,
Type,
TypeVar,
Expand All @@ -48,7 +49,7 @@
from crosshair.auditwall import opened_auditwall
from crosshair.tracers import COMPOSITE_TRACER, NoTracing, ResumedTracing, is_tracing

_DEBUG = False
_DEBUG_STREAM: Optional[TextIO] = None


def is_iterable(o: object) -> bool:
Expand Down Expand Up @@ -197,14 +198,16 @@ def frame_summary_for_fn(
return sourcelines(fn)[:2]


def set_debug(debug: bool):
global _DEBUG
_DEBUG = debug
def set_debug(new_debug: bool, output: TextIO = sys.stderr):
global _DEBUG_STREAM
if new_debug:
_DEBUG_STREAM = output
else:
_DEBUG_STREAM = None


def in_debug() -> bool:
global _DEBUG
return _DEBUG
return bool(_DEBUG_STREAM)


def debug(*a):
Expand All @@ -218,7 +221,7 @@ def debug(*a):
symbolic will change the path exploration that CrossHair normally takes, leading to
different outcomes in verbose and non-verbose mode.
"""
if not _DEBUG:
if not _DEBUG_STREAM:
return
with NoTracing():
stack = traceback.extract_stack()
Expand All @@ -228,7 +231,7 @@ def debug(*a):
"{:06.3f}|{}|{}() {}".format(
time.monotonic(), " " * indent, frame.name, " ".join(map(str, a))
),
file=sys.stderr,
file=_DEBUG_STREAM,
)


Expand Down

0 comments on commit 17c92dc

Please sign in to comment.