OpenThread 記錄

前往 GitHub 查看原始碼

OpenThread 記錄是由多種編譯時間設定常數控制。除非另有註明,否則這些常數會在下列檔案中定義:

openthread/src/core/config/logging.h

輸出方法

OpenThread 支援不同的輸出記錄方法 (定義為 OPENTHREAD_CONFIG_LOG_OUTPUT 的編譯時間設定常數)。記錄方法選項列於以下檔案中:

openthread/src/core/config/logging.h

預設的記錄輸出設定為 OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED

輸出方法的範例是可能需要更新平台層級設定常數 (而非核心設定) 的範例。舉例來說,如要變更模擬範例應用程式中的輸出方法,請編輯 openthread/examples/platforms/simulation/openthread-core-simulation-config.h,而非 openthread/src/core/config/logging.h

記錄層級

記錄可能會輸出不同層級的資訊,定義為 OPENTHREAD_CONFIG_LOG_LEVEL 的編譯時間設定常數。層級選項列於以下檔案中:

openthread/include/openthread/platform/logging.h

您也可以在 Platform 記錄巨集 API 參考資料中取得記錄層級清單。

預設記錄層級為 OT_LOG_LEVEL_CRIT,只會輸出最重要的記錄。視需求變更層級,即可查看更多記錄。如要查看所有 OpenThread 記錄,請使用 OT_LOG_LEVEL_DEBG

記錄地區

記錄地區會決定要為 OpenThread 程式碼啟用哪些區域的記錄功能。地區列舉是由下列檔案定義:

openthread/include/openthread/platform/logging.h

您也可以在 Platform Logging Logging Enumerations API 參考資料中找到記錄地區清單。

記錄地區常做為記錄函式中的參數使用。所有地區都預設為啟用。

預設記錄函式

OpenThread 中記錄的預設函式為 otPlatLog,定義為 OPENTHREAD_CONFIG_PLAT_LOG_FUNCTION 的編譯時間設定常數。

如要進一步瞭解這個函式,請參閱 Platform Logging API 參考資料。

如要直接在 OpenThread 範例應用程式中使用這個函式,請使用 OT_REFERENCE_DEVICE CMake 選項。舉例來說,如要在 CLI 應用程式中使用 CC2538 範例:

./script/build -DOT_REFERENCE_DEVICE=ON

您也可以在更新時更新 openthread/etc/cmake/options.cmake 檔案,讓這些檔案預設為啟用。

如何啟用紀錄

啟用記錄之前,請確認您的環境設定為建構 OpenThread。詳情請參閱「建構 OpenThread」。

啟用所有記錄檔

如要快速啟用所有記錄層級和地區,請使用 OT_FULL_LOGS cmake 選項:

./script/build -DOT_FULL_LOGS=ON

這個開關會將記錄層級設為 OT_LOG_LEVEL_DEBG,並啟用所有地區旗標。

啟用特定層級的記錄檔

如要啟用特定層級的記錄檔,請編輯 openthread/src/core/config/logging.h 並將 OPENTHREAD_CONFIG_LOG_LEVEL 更新為所需的層級,然後建構 OpenThread。例如,如要啟用最多 OT_LOG_LEVEL_INFO 的記錄檔:

#define OPENTHREAD_CONFIG_LOG_LEVEL OT_LOG_LEVEL_INFO
./script/build

查看 syslog 中的記錄

根據預設,系統會將記錄傳送到 syslog。在 Linux 中,此為 /var/log/syslog.

  1. 建立啟用所有記錄檔的模擬範例:
    cd openthread
    ./script/cmake-build simulation -DOT_FULL_LOGS=ON
    
  2. 啟動模擬節點:
    ./build/simulation/examples/apps/cli/ot-cli-ftd 1
    
  3. 在新終端機視窗中設定 OT 記錄的即時輸出:
    tail -F /var/log/syslog | grep "ot-cli-ftd"
    
  4. 在模擬節點上,啟動 Thread:
dataset init new
Done
dataset
Active Timestamp: 1
Channel: 13
Channel Mask: 07fff800
Ext PAN ID: d63e8e3e495ebbc3
Mesh Local Prefix: fd3d:b50b:f96d:722d/64
Network Key: dfd34f0f05cad978ec4e32b0413038ff
Network Name: OpenThread-8f28
PAN ID: 0x8f28
PSKc: c23a76e98f1a6483639b1ac1271e2e27
Security Policy: 0, onrcb
Done
dataset commit active
Done
ifconfig up
Done
thread start
Done

切換回執行 tail 指令的終端機視窗。模擬節點應即時顯示記錄。記下輸出記錄標記:[INFO][DEBG][NOTE]。這些全部都對應至記錄層級。舉例來說,如果您將記錄層級變更為 OT_LOG_LEVEL_INFO,則 DEBG 記錄會從輸出內容中消失。

ot-cli-ftd[30055]: [1] [DEBG]-MAC-----: SrcAddrMatch - Cleared all entries
ot-cli-ftd[30055]: [1] [INFO]-CORE----: Non-volatile: Read NetworkInfo {rloc:0x9c00, extaddr:1a4aaf5e97c852de, role:Leader, mode:0x0f, keyseq:0x0, ...
ot-cli-ftd[30055]: [1] [INFO]-CORE----: Non-volatile: ... pid:0x8581bc9, mlecntr:0x3eb, maccntr:0x3e8, mliid:05e4b515e33746c8}
ot-cli-ftd[30055]: [1] [INFO]-CORE----: Notifier: StateChanged (0x7f133b) [Ip6+ Ip6- LLAddr MLAddr Rloc+ KeySeqCntr NetData Ip6Mult+ Channel PanId NetName ExtPanId MstrKey PSKc SecPolicy]
ot-cli-ftd[30055]: [1] [INFO]-CLI-----: execute command: dataset panid
ot-cli-ftd[30055]: [1] [INFO]-CLI-----: execute command: dataset commit active
ot-cli-ftd[30055]: [1] [INFO]-MESH-CP-: Active dataset set
ot-cli-ftd[30055]: [1] [DEBG]-MAC-----: Idle mode: Radio sleeping
ot-cli-ftd[30055]: [1] [DEBG]-MAC-----: RadioPanId: 0x8f28
ot-cli-ftd[30055]: [1] [INFO]-CORE----: Notifier: StateChanged (0x007f0100) [KeySeqCntr Channel PanId NetName ExtPanId MstrKey PSKc SecPolicy]
ot-cli-ftd[30055]: [1] [INFO]-CLI-----: execute command: ifconfig up
ot-cli-ftd[30055]: [1] [DEBG]-MAC-----: Idle mode: Radio receiving on channel 11
ot-cli-ftd[30055]: [1] [INFO]-CLI-----: execute command: thread start
ot-cli-ftd[30055]: [1] [NOTE]-MLE-----: Role Disabled -> Detached
ot-cli-ftd[30055]: [1] [INFO]-MLE-----: Attempt to become router
ot-cli-ftd[30055]: [1] [INFO]-CORE----: Non-volatile: Read NetworkInfo {rloc:0x9c00, extaddr:1a4aaf5e97c852de, role:Leader, mode:0x0f, keyseq:0x0, ...
ot-cli-ftd[30055]: [1] [INFO]-CORE----: Non-volatile: ... pid:0x8581bc9, mlecntr:0x3eb, maccntr:0x3e8, mliid:05e4b515e33746c8}
ot-cli-ftd[30055]: [1] [INFO]-CORE----: Non-volatile: Saved NetworkInfo {rloc:0x9c00, extaddr:1a4aaf5e97c852de, role:Leader, mode:0x0f, keyseq:0x0, ...
ot-cli-ftd[30055]: [1] [INFO]-CORE----: Non-volatile: ... pid:0x8581bc9, mlecntr:0x7d4, maccntr:0x7d0, mliid:05e4b515e33746c8}
ot-cli-ftd[30055]: [1] [DEBG]-MLE-----: Store Network Information
ot-cli-ftd[30055]: [1] [INFO]-MLE-----: Send Link Request (ff02:0:0:0:0:0:0:2)

在 CLI 應用程式中查看記錄

您可以直接在 OpenThread CLI 範例應用程式中查看記錄檔。

  1. 編輯範例平台的設定檔,並將記錄輸出變更為應用程式。對於模擬範例,此為 openthread/examples/platforms/simulation/openthread-core-simulation-config.h
    #define OPENTHREAD_CONFIG_LOG_OUTPUT OPENTHREAD_CONFIG_LOG_OUTPUT_APP
    
  2. 使用所需的記錄層級建構模擬範例。啟用所有記錄檔:
    ./script/cmake-build simulation -DOT_FULL_LOGS=ON
    
  3. 啟動模擬節點:
    ./build/simulation/examples/apps/cli/ot-cli-ftd 1
    
  4. 處理指令時,您應該會在 OpenOpen CLI 的同一視窗中看到記錄輸出內容。

如果您已新增自訂記錄並啟用所有記錄,CLI 行緩衝區或 UART 傳輸緩衝區可能不夠大,無法處理其他自訂記錄。如果部分記錄未正確顯示,請嘗試增加 CLI 行緩衝區的大小 (在 /openthread/src/cli/cli_config.h 中定義為 OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH),或是增加 UART 傳輸緩衝區的大小 (在平台設定檔 (例如 /src/nrf52840/openthread-core-nrf52840-config.h) 中定義為 OPENTHREAD_CONFIG_CLI_UART_TX_BUFFER_SIZE)

查看 NCP 的記錄

NCP 的記錄可透過 wpantund 傳送至主機的 syslog。如果是 Linux 主機,這是指 /var/log/syslog.

使用 OPENTHREAD_CONFIG_LOG_OUTPUT_APPOPENTHREAD_CONFIG_LOG_OUTPUT 值啟用 NCP 記錄。在平台的設定檔中變更。

舉例來說,如要為連接至 Linux 主機的 nrf52840 啟用這項功能:

  1. 編輯平台的設定檔,並將記錄輸出變更為應用程式。如果是 nrf52840,則在 ot-nrf528xx 存放區中為 ./src/nrf52840/openthread-core-nrf52840-config.h
    #define OPENTHREAD_CONFIG_LOG_OUTPUT OPENTHREAD_CONFIG_LOG_OUTPUT_APP
    
  2. 使用想要的記錄檔層級和其他 NCP 專用的旗標來建構 nrf52840 範例。如要建立啟用所有記錄檔的彙整工具:
    ./script/build nrf52840 UART_trans -DOT_JOINER=ON -DOT_FULL_LOGS=ON
    
  3. 刷新 NCP,將其連接至 Linux 主機,然後如 wpantund 存放區中所述,啟動 wpantund

  4. NCP 執行後,請檢查 Linux 電腦上的 syslog

    tail -F /var/log/syslog | grep "wpantund"
    

  5. 您應該會看到 NCP 即時顯示的 OpenThread 記錄。您也可以在 wpantund 輸出中看到這些資料。

在執行階段變更記錄層級

如果啟用動態記錄層級控制項,在執行階段可能會變更記錄層級。

  1. 使用 -DOT_LOG_LEVEL_DYNAMIC=ON 選項建構應用程式。例如:
    ./script/build nrf52840 UART_trans -DOT_JOINER=ON -DOT_FULL_LOGS=ON -DOT_LOG_LEVEL_DYNAMIC=ON
    
  2. 根據實作方式變更記錄層級:
    1. 如果是晶片系統 (SoC),請使用 OpenThread 應用程式中的 Logging API
    2. 如果是 NCP,請在指令列中使用 wpanctl。請參閱 wpantund 存放區中的 wpan-properties.h,瞭解 wpanctl 的所有屬性以及 Spinel API 的記錄層級定義。
      wpanctl set OpenThread:LogLevel 5