日志记录和调试#
在Python脚本中启用日志记录
导入 debug_mode 脚本
from interpret.develop import debug_mode
将以下代码行添加到您正在运行的脚本/测试中,以使用原生库的调试版本启用日志记录
debug_mode(log_filename='log.txt', log_level='INFO', native_debug=True)
在上述命令示例中,日志被发送到log.txt文件,并使用INFO日志级别
请注意,C++项目应在DEBUG模式下编译
stdout/stderr 也可以用作输出,例如
debug_mode(log_filename=stdout, log_level='INFO', native_debug=True)
请注意,默认情况下
pytest会捕获发送到stdout和stderr的输出,因此在运行测试时您不会看到它。如果您想看到输出,请在.vscode\settings.json中添加以下参数
"python.testing.pytestArgs": [ "python", "-s" ],
如果您选择 运行测试 选项,您应该在 输出窗口 –> Python 测试日志 中看到日志
如果您选择 调试测试 选项,您应该在 调试控制台 中看到日志
在VS Code中调试Python和C++
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" } ]
对于
interpret,我们希望确保ctypes加载的是调试版本。一种方法是导入debug_mode脚本并在你运行的脚本/测试中使用它from interpret.develop import debug_mode debug_mode(log_filename='log.txt', log_level='INFO', native_debug=True)
确保C++项目在DEBUG模式下编译
开始调试一个Python脚本/测试
启动 C++ Attach 并选择要附加的 PID
确定要附加的正确 PID 可能有点困难。一种方法是将以下 Python 代码添加到您的 Python 脚本中以了解其 PID
print('当前 PID = {}'.format(os.getpid()))
此时,VS Code 调试器工具栏中应该可以看到 Python 和 C++ 附加调试器