# 崩溃报告

如果OAK相机发生固件崩溃，它会自动生成崩溃报告并存储在设备中。 崩溃报告包含有关崩溃的信息，例如堆栈跟踪、设备配置以及崩溃时设备的状态。 可以从设备读取崩溃报告，并将其发送给Luxonis用于调试。

## 演示

如果在设备上发现了崩溃报告，以下示例将读取该报告并将其保存为json文件：

```bash
> python crash_report.py
    Crash dump found on your device!
    Saved to crashDump_0_184430102163DB0F00_3575b77f20e796b4e79953bf3d2ba22f0416ee8b.json
    Please report to developers!
```

请将崩溃报告连同[最小可重现示例
(MRE)](https://stackoverflow.com/help/minimal-reproducible-example)（DepthAI问题）一起发送到我们的[讨论论坛](https://discuss.luxonis.com/)。谢谢！

## 设置

这个示例需要DepthAI v3 API，参见[安装说明](https://docs.luxonis.com/software-v3/depthai.md)。

## 源代码

#### Python

```python
#!/usr/bin/env python3

import cv2
import depthai as dai
from json import dump
from os.path import exists

# Connect to device and start pipeline
with dai.Device() as device:

    if device.hasCrashDump():
        crashDump = device.getCrashDump()
        commitHash = crashDump.depthaiCommitHash
        deviceId = crashDump.deviceId

        json = crashDump.serializeToJson()
        
        i = -1
        while True:
            i += 1
            destPath = "crashDump_" + str(i) + "_" + deviceId + "_" + commitHash + ".json"
            if exists(destPath):
                continue

            with open(destPath, 'w', encoding='utf-8') as f:
                dump(json, f, ensure_ascii=False, indent=4)

            print("Crash dump found on your device!")
            print(f"Saved to {destPath}")
            print("Please report to developers!")
            break
    else:
        print("There was no crash dump found on your device!")
```

#### C++

```cpp
#include <fstream>
#include <iostream>

// Includes common necessary includes for development using depthai library
#include "depthai/depthai.hpp"

static bool fileExists(dai::Path path) {
    std::ifstream file(path);
    return file.is_open();
}

int main() {
    using namespace std;

    // Connect to device and start pipeline
    dai::Device device;
    if(device.hasCrashDump()) {
        auto crashDump = device.getCrashDump();
        std::string commitHash = crashDump.depthaiCommitHash;
        std::string deviceId = crashDump.deviceId;

        auto json = crashDump.serializeToJson();

        for(int i = 0;; i++) {
            dai::Path destPath = "crashDump_" + to_string(i) + "_" + deviceId + "_" + commitHash + ".json";
            if(fileExists(destPath)) continue;

            std::ofstream ob(destPath);
            ob << std::setw(4) << json << std::endl;

            std::cout << "Crash dump found on your device!" << std::endl;
            std::cout << "Saved to " << destPath.string() << std::endl;
            std::cout << "Please report to developers!" << std::endl;
            break;
        }
    } else {
        std::cout << "There was no crash dump found on your device!" << std::endl;
    }

    return 0;
}
```

### 需要帮助？

请前往 [OAKChina 官网](https://www.oakchina.cn/) 获取技术支持或解答您的任何疑问。
