Python: CG trace: Use logging module for debuging

This commit is contained in:
Rasmus Wriedt Larsen
2020-07-18 17:55:51 +02:00
parent acc5f70d4a
commit 8b6de17461
2 changed files with 11 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
import logging
import os
import sys
from io import StringIO
@@ -24,6 +25,8 @@ def record_calls(code, globals):
def main(args=None) -> int:
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
if args is None:
# first element in argv is program name
args = sys.argv[1:]
@@ -53,6 +56,7 @@ def main(args=None) -> int:
elif opts.xml:
XMLExporter.export(recorded_calls, opts.xml)
else:
print("Recorded calls:")
for (call, callee) in recorded_calls:
print(f"{call} --> {callee}")

View File

@@ -1,18 +1,17 @@
import dataclasses
import dis
import logging
import os
import sys
from typing import Optional
LOGGER = logging.getLogger(__name__)
# copy-paste For interactive ipython sessions
# import IPython; sys.stdout = sys.__stdout__; IPython.embed(); sys.exit()
def debug_print(*args, **kwargs):
# print(*args, **kwargs, file=sys.__stderr__)
pass
_canonic_filename_cache = dict()
@@ -47,9 +46,8 @@ class Call:
def from_frame(cls, frame):
code = frame.f_code
# Uncomment to see the bytecode
b = dis.Bytecode(frame.f_code, current_offset=frame.f_lasti)
debug_print(b.dis())
LOGGER.debug(f"bytecode: \n{b.dis()}")
return cls(
filename=canonic_filename(code.co_filename),
@@ -189,7 +187,7 @@ class CallGraphTracer:
if event not in ["call", "c_call"]:
return
debug_print(f"profilefunc {event=}")
LOGGER.debug(f"profilefunc {event=}")
if event == "call":
# in call, the `frame` argument is new the frame for entering the callee
call = Call.from_frame(frame.f_back)
@@ -201,6 +199,5 @@ class CallGraphTracer:
call = Call.from_frame(frame)
callee = ExternalCallee.from_arg(arg)
debug_print(f"{call} --> {callee}")
debug_print("\n" * 5)
LOGGER.debug(f"{call} --> {callee}")
self.recorded_calls.add((call, callee))