Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b6de17

Browse files
committed
Python: CG trace: Use logging module for debuging
1 parent acc5f70 commit 8b6de17

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

python/tools/recorded-call-graph-metrics/src/cg_trace/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import sys
34
from io import StringIO
@@ -24,6 +25,8 @@ def record_calls(code, globals):
2425

2526

2627
def main(args=None) -> int:
28+
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
29+
2730
if args is None:
2831
# first element in argv is program name
2932
args = sys.argv[1:]
@@ -53,6 +56,7 @@ def main(args=None) -> int:
5356
elif opts.xml:
5457
XMLExporter.export(recorded_calls, opts.xml)
5558
else:
59+
print("Recorded calls:")
5660
for (call, callee) in recorded_calls:
5761
print(f"{call} --> {callee}")
5862

python/tools/recorded-call-graph-metrics/src/cg_trace/tracer.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import dataclasses
22
import dis
3+
import logging
34
import os
45
import sys
56
from typing import Optional
67

7-
# copy-paste For interactive ipython sessions
8-
# import IPython; sys.stdout = sys.__stdout__; IPython.embed(); sys.exit()
8+
LOGGER = logging.getLogger(__name__)
99

1010

11-
def debug_print(*args, **kwargs):
12-
# print(*args, **kwargs, file=sys.__stderr__)
13-
pass
11+
# copy-paste For interactive ipython sessions
12+
# import IPython; sys.stdout = sys.__stdout__; IPython.embed(); sys.exit()
1413

1514

1615
_canonic_filename_cache = dict()
@@ -47,9 +46,8 @@ class Call:
4746
def from_frame(cls, frame):
4847
code = frame.f_code
4948

50-
# Uncomment to see the bytecode
5149
b = dis.Bytecode(frame.f_code, current_offset=frame.f_lasti)
52-
debug_print(b.dis())
50+
LOGGER.debug(f"bytecode: \n{b.dis()}")
5351

5452
return cls(
5553
filename=canonic_filename(code.co_filename),
@@ -189,7 +187,7 @@ def profilefunc(self, frame, event, arg):
189187
if event not in ["call", "c_call"]:
190188
return
191189

192-
debug_print(f"profilefunc {event=}")
190+
LOGGER.debug(f"profilefunc {event=}")
193191
if event == "call":
194192
# in call, the `frame` argument is new the frame for entering the callee
195193
call = Call.from_frame(frame.f_back)
@@ -201,6 +199,5 @@ def profilefunc(self, frame, event, arg):
201199
call = Call.from_frame(frame)
202200
callee = ExternalCallee.from_arg(arg)
203201

204-
debug_print(f"{call} --> {callee}")
205-
debug_print("\n" * 5)
202+
LOGGER.debug(f"{call} --> {callee}")
206203
self.recorded_calls.add((call, callee))

0 commit comments

Comments
 (0)