Logging and Debugging#
Enable logging in a Python script
Import the debug_mode script
from interpret.develop import debug_mode
Add the following line of code to the script/test you are running to enable logging using the debug build of the native library
debug_mode(log_filename='log.txt', log_level='INFO', native_debug=True)
In the above command example logs are sent to log.txt file and the INFO logging level is used
Note that the C++ project should be compiled in DEBUG mode
stdout/stderr can also be used as output, e.g.
debug_mode(log_filename=stdout, log_level='INFO', native_debug=True)
Note that by default
pytest
captures output sent tostdout
andstderr
so you do not see it when running tests. If you want to see the output add the following parameter in.vscode\settings.json
"python.testing.pytestArgs": [ "python", "-s" ],
If you choose the option Run Test you should see logs in the Output Window –> Python Test Log
If you choose the option Debug Test you should see logs in the DEBUG CONSOLE
Debugging Python and C++ in VS Code
Set up debugging configurations for Python and C++ Attach. As an example, the launch configuration file (
launch.json
) should contain"configurations": [ { "name": "(Windows) Attach", "type": "cppvsdbg", "request": "attach", "processId": "${command:pickProcess}" }, { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal" } ]
For
interpret
we want to be sure thatctypes
loads the debug build. One way to do this is to import the debug_mode script and use it in the script/test you are runningfrom interpret.develop import debug_mode debug_mode(log_filename='log.txt', log_level='INFO', native_debug=True)
Make sure the C++ project is compiled in DEBUG mode
Start debugging a Python script / test
Launch C++ Attach and choose which PID to attach to
It can be a bit hard to determine which is the correct PID to attach to. One way is to add the following Python code to your Python script to know its PID
print('Current PID = {}'.format(os.getpid()))
At this point both Python and C++ Attach debuggers should be seen in the VS Code debugger toolbar