Logging and Debugging

Enable logging in a Python script

  1. Import the debug_mode script

    from interpret.develop import debug_mode
    
  2. 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

  3. 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 to stdout and stderr 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

  1. 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"
        }
    ]
    
  2. For interpret we want to be sure that ctypes 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 running

    from interpret.develop import debug_mode
    debug_mode(log_filename='log.txt', log_level='INFO', native_debug=True)
    
  3. Make sure the C++ project is compiled in DEBUG mode

  4. Start debugging a Python script / test

  5. 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()))
      
  6. At this point both Python and C++ Attach debuggers should be seen in the VS Code debugger toolbar