天天看點

#夏日挑戰賽#OpenHarmony——hilog指令行使用手冊

本文正在參加星光計劃3.0–夏日挑戰賽

作者:陳會

為了讓剛入門的鴻蒙開發者迅速掌握hilog工具的使用方法,特編寫了該篇指令行使用手冊供開發者查閱,讓開發者在自己的的開發和調試的過程中充分認識hilog工具的優勢和便捷。

一、查詢日志

1.1、指定子產品查詢

-t

用于指定輸出日志的類型/子產品,目前系統共包含app/core/kmsg/init四種日志類型。

hilog -t app|core|kmsg|init

阻塞式實時查詢子產品日志,

-t

可以與其他參數一起使用。

hilog -t app|core|kmsg|init -x

非阻塞式實時查詢子產品日志,

-x

選項也可以和hilog的其他選項同時使用。

示例如下:

# hilog -t kmsg
01-01 00:00:03.909     0     0 I fffff/kmsg: Booting Linux on physical CPU 0x0
01-01 00:00:03.909     0     0 W fffff/kmsg: Linux version 5.10.79 (osm@ubuntu) (OHOS () clang version 10.0.1.480513  (llvm-project b2cadc87d64786377748bdb6bc5e6949492f01ab), GNU ld (Linaro_Binutils-2019.12) 2.28.2.20170706) #1 SMP Wed Apr 6 09:37:37 CST 2022
01-01 00:00:03.909     0     0 I fffff/[410fc075]: CPU: ARMv7 Processor  revision 5 (ARMv7), cr=10c5387d
01-01 00:00:03.909     0     0 I fffff/kmsg: CPU: div instructions available: patching division code
01-01 00:00:03.909     0     0 I fffff/kmsg: CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
01-01 00:00:03.909     0     0 I fffff/kmsg: OF: fdt: Machine model: Hisilicon HI3516DV300 DEMO Board
01-01 00:00:03.909     0     0 I fffff/kmsg: Memory policy: Data cache writealloc
01-01 00:00:03.909     0     0 D fffff/kmsg: cma: dma_contiguous_reserve(limit ffffffff)
01-01 00:00:03.909     0     0 D fffff/kmsg: cma: dma_contiguous_reserve: reserving 64 MiB for global area
           

1.2、指定tag查詢

-T

選項為指定tag。标簽:用于指定日志辨別,可以為任意字元串,建議辨別調用所在的類或者業務行為。

hilog -t app|core|kmsg|init -T <tag>

子產品内指定tag,

-T

可以與其他參數一起使用。

hilog -T <tag>

隻指定tag

示例如下:

# hilog -t core -T BatteryThread

01-02 02:10:03.771   725   820 I 02500/BatteryThread: UpdateBatteryInfo: BatteryInfo capacity=11, voltage=4123456, temperature=222, healthState=1, pluggedType=2, pluggedMaxCurrent=-1, pluggedMaxVoltage=-1, chargeState=3, chargeCounter=4000000, present=0, technology=Li-ion
01-02 02:10:03.771   725   820 I 02500/BatteryThread: UpdateBatteryInfo g_callback is nullptr
01-02 02:10:03.771   725   820 I 02500/BatteryThread: LoopingThreadEntry: timeout=-1, nevents=1
01-02 02:10:13.772   725   820 I 02500/BatteryThread: UpdateBatteryInfo: BatteryInfo capacity=11, voltage=4123456, temperature=222, healthState=1, pluggedType=2, pluggedMaxCurrent=-1, pluggedMaxVoltage=-1, chargeState=3, chargeCounter=4000000, present=0, technology=Li-ion
01-02 02:10:13.772   725   820 I 02500/BatteryThread: UpdateBatteryInfo g_callback is nullptr
01-02 02:10:13.772   725   820 I 02500/BatteryThread: LoopingThreadEntry: timeout=-1, nevents=1
01-02 02:10:23.772   725   820 I 02500/BatteryThread: UpdateBatteryInfo: BatteryInfo capacity=11, voltage=4123456, 
           
# hilog -T BatteryThread

01-02 02:14:03.771   725   820 I 02500/BatteryThread: UpdateBatteryInfo: BatteryInfo capacity=11, voltage=4123456, temperature=222, healthState=1, pluggedType=2, pluggedMaxCurrent=-1, pluggedMaxVoltage=-1, chargeState=3, chargeCounter=4000000, present=0, technology=Li-ion
01-02 02:14:03.771   725   820 I 02500/BatteryThread: UpdateBatteryInfo g_callback is nullptr
01-02 02:14:03.771   725   820 I 02500/BatteryThread: LoopingThreadEntry: timeout=-1, nevents=1
01-02 02:14:13.772   725   820 I 02500/BatteryThread: UpdateBatteryInfo: BatteryInfo capacity=11, voltage=4123456, temperature=222, healthState=1, pluggedType=2, pluggedMaxCurrent=-1, pluggedMaxVoltage=-1, chargeState=3, chargeCounter=4000000, present=0, technology=Li-ion
           

1.3、指定level查詢

-L

指定日志級别,HiLog中定義了DEBUG、INFO、WARN、ERROR、FATAL五種日志級别。

hilog -t app|core|kmsg|init -L D|I|W|E|F

子產品内指定level查詢,D:debug, I:info, W:warning, E:error, F:fatal。

hilog -L D|I|W|E|F

隻指定level查詢,可以與其他參數一起使用

示例如下:

# hilog -t app -L I

01-01 08:15:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: TimeManager --> notifyTimeChange
01-01 08:15:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: EventManager --> publish localEvent type: Time_Change_Event
01-01 08:16:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: CommonEvent_TimeManager --> handle common event: usual.event.TIME_TICK
01-01 08:16:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: TimeManager --> notifyTimeChange
01-01 08:16:00.032   875   875 I 02200/JsApp: SystemUI_Default tag: EventManager --> publish localEvent type: Time_Change_Event
01-01 08:17:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: CommonEvent_TimeManager --> handle common event: usual.event.TIME_TICK
           
# hilog -L I

01-01 08:15:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: TimeManager --> notifyTimeChange
01-01 08:15:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: EventManager --> publish localEvent type: Time_Change_Event
01-01 08:16:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: CommonEvent_TimeManager --> handle common event: usual.event.TIME_TICK
01-01 08:16:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: TimeManager --> notifyTimeChange
01-01 08:16:00.032   875   875 I 02200/JsApp: SystemUI_Default tag: EventManager --> publish localEvent type: Time_Change_Event
01-01 08:17:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: CommonEvent_TimeManager --> handle common event: usual.event.TIME_TICK
           

1.4、指定domain查詢

-D

選項為指定domain。服務域:日志的服務域,用于指定輸出日志所對應的業務領域。這是一個十六進制整數,範圍從 0x0 到 0xFFFFF。建議使用0xAAABB格式,其中AAA表示子系統,BB表示子產品。

hilog -t app|core|kmsg|init -D <domian>

子產品内指定domian查詢。

hilog -D <domain>

隻指定domian查詢,可以與其他參數一起使用。

# hilog -D 0x0 -t core                                                          

01-01 04:36:03.140    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                       
01-01 04:36:05.299    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:36:07.459    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:36:09.619    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:36:11.780    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:36:13.939    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:36:16.099    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:36:18.259    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:36:20.419    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                              
           
# hilog -D 0x0                                                                  

01-01 04:37:01.459    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:37:03.619    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:37:05.779    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:37:07.939    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:37:10.100    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:37:12.260    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:37:14.419    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                                          
01-01 04:37:16.579    91   145 I 00000/AppSpawnServer: AppSpawnServer::ConnectionPeer connectFd is -11                                
           

1.5、排除查詢

type、level、domain、tag支援排除查詢,排除查詢可以使用以"^"開頭的參數和分隔符","來完成

hilog [-t ^app|kmsg|init|core] [-T ^<tag>] [-L ^D|I|W|E|F] [-D ^<domain>]

示例如下:

# hilog -t ^app,kmsg,init -T ^BatteryThread

01-02 02:07:00.071   332   332 E 01400/OHOS::ROSEN: RsDebug RSHardwareProcessor::ProcessSurface surfaceNode id:3758096384048 name:[SystemUi_NavigationBar] dst [0 912 480 48]SrcRect [480 48] rawbuffer [480 48] surfaceBuffer [480 48] buffaddr:0x1249230, z:2.000000, globalZOrder:2, blendType = 3
01-02 02:07:00.071   332   332 D 01400/OHOS::ROSEN: RsDebug RSHardwareProcessor::CropLayers this layer no need to crop
01-02 02:07:00.071   332   332 D 01400/OHOS::ROSEN: RsDebug RSHardwareProcessor::CropLayers this layer no need to crop
01-02 02:07:00.071   332   332 D 01400/OHOS::ROSEN: RsDebug RSHardwareProcessor::CropLayers this layer no need to crop
01-02 02:07:00.071   332   332 D 01400/Composer: Repaint: Repaint: start
01-02 02:07:00.071   332   332 D 01400/DISP: [SetLayerAlpha@hdi_session.cpp:273]
01-02 02:07:00.071   332   332 D 01400/DISP: [CallLayerFunction@hdi_session.h:48] device Id : 0
01-02 02:07:00.071   332   332 D 01400/DISP: [GetHdiLayer@hdi_display.cpp:221] id : 3
           

二、查詢和設定資訊

2.1、查詢和設定子產品的buffer

2.1.1、查詢子產品的buffer

顯示的日志内容為各子產品buffer裡目前緩存的内容,并實時更新,超出buffer的内容會覆寫掉舊的内容。

hilog -g -t app|core|kmsg|init

查詢日志類型為app/core/kmsg/init的buffer大小:

示例如下:

# hilog -g -t core

core buffer size is 256K

#
           

2.1.2、設定子產品的buffer

hilog -G <size> -t app|core|kmsg|init

設定指定<type>日志類型緩沖區的大小,配合-t指定某一類型使用,預設app和core, 可使用B/K/M/G為機關

示例如下:

# hilog -t app -g

app buffer size is 256K

# hilog -t app -G 2M

app buffer size is 2M

# hilog -t app -g

app buffer size is 2M

#
           

2.1.3、清除buffer日志

hilog -t app|core|kmsg|init -r

-r

選項清除buffer日志,配合-t指定某一類型使用,預設app和core

# hilog -t app -r

app log clear success

# hilog -t app

#
           

2.2、查詢和清除統計資訊

2.2.1、查詢統計資訊

hilog -t app|core|kmsg|init -s

-s

查詢統計資訊,需配合-t或-D使用

示例如下:

# hilog -t app -s

app print log length is 1M
app cache log length is 461K
app dropped log lines is 0B
#
           

2.2.2、清除統計資訊

hilog -t app|core|kmsg|init -S

-S

清除統計資訊,需配合-t或-D使用

# hilog -t app -s

app print log length is 1M
app cache log length is 461K
app dropped log lines is 0B
#
#

# hilog -t app -S

app statistic info clear success

# hilog -t app -s

app print log length is 0B
app cache log length is 0B
app dropped log lines is 0B
#
           

三、日志落盤

3.1 使用方法

hilog -w start [-f "xxx"] [-l <length>] [-j <jobid>] [-m <algorithm>] [-n <number>]

啟動落盤任務

hilog -w query [-j <jobid>]

查詢落盤任務

hilog -w stop [-j <jobid>]

停止落盤任務

示例如下:

# hilog -w query
#
#hilog -w start -f "hilog" -l 66k -j 222 -m zlib -n 2                          
Persist task [jobid:222] started successfully
#cd /data/log/hilog
#
# ls -al

total 100
drwxr-x--- 2 logd   log     4096 1970-01-02 03:34 .
drwxrwx--- 6 system log     4096 1970-01-01 00:38 ..
-rw-r----- 1 logd   system 65540 1970-01-02 03:34 .persisterInfo_222
-rw-r----- 1 logd   system   132 1970-01-02 03:34 .persisterInfo_222.info
-rw-r----- 1 logd   system 12239 1970-01-02 03:34 hilog.002.19700102-033431.gz
-rw-r----- 1 logd   system  6883 1970-01-02 03:34 hilog.003.19700102-033431.gz

# hilog -w query -j 222 #指定jobid查詢

222 core,app zlib /data/log/hilog/hilog 67584 2

# hilog -w query    #查詢全部落盤任務

222 core,app zlib /data/log/hilog/hilog 67584 2

# hilog -w stop -j 222

Persist task [jobid:222] stopped successfully

# ls -al

total 36
drwxr-x--- 2 logd   log     4096 1970-01-02 03:35 .
drwxrwx--- 6 system log     4096 1970-01-01 00:38 ..
-rw-r----- 1 logd   system 12239 1970-01-02 03:34 hilog.002.19700102-033431.gz
-rw-r----- 1 logd   system 13000 1970-01-02 03:35 hilog.003.19700102-033431.gz

# 
           

3.2 案例展示

案例需求:抓取子產品最早的log,即:開機啟動過程中子產品産生的log,并将内容儲存到檔案導出裝置

第一步:啟動落盤任務

示例如下:

#

# hilog -w start -f "hilog" -l 66k -j 111 -m zlib -n 2 -t kmsg

Persist task [jobid:111] started successfully
#
#cd /data/log/hilog
#
# ls -al 

total 116
drwxr-x--- 2 logd   log     4096 1970-01-02 19:31 .
drwxrwx--- 6 system log     4096 1970-01-01 00:38 ..
-rw-r----- 1 logd   system 65540 1970-01-02 19:31 .persisterInfo_111
-rw-r----- 1 logd   system   132 1970-01-02 19:31 .persisterInfo_111.info
-rw-r----- 1 logd   system 19245 1970-01-02 19:31 hilog.000.19700102-193128.gz
-rw-r----- 1 logd   system 16069 1970-01-02 19:31 hilog.001.19700102-193128.gz
#
           

第二步:重新開機裝置

# reboot

D:\share>
           

第三步:裝置起來後,停止落盤任務。

D:\share>hdc_std.exe shell
#
# cd /data/log/hilog/
#
# ls -al

total 108
drwxr-x--- 2 logd   log     4096 1970-01-02 19:32 .
drwxrwx--- 6 system log     4096 1970-01-01 00:38 ..
-rw-r----- 1 logd   system 65540 1970-01-02 19:32 .persisterInfo_111
-rw-r----- 1 logd   system   132 1970-01-02 19:32 .persisterInfo_111.info
-rw-r----- 1 logd   system 16192 1970-01-02 19:31 hilog.003.19700102-193147.gz #注意:log檔案的命名是随時間實時更新的
-rw-r----- 1 logd   system  8500 1970-01-02 19:32 hilog.004.19700102-193213.gz
#
# hilog -w stop

Persist task [jobid:111] stopped successfully

# ls -al

total 36
drwxr-x--- 2 logd   log     4096 1970-01-02 19:34 .
drwxrwx--- 6 system log     4096 1970-01-01 00:38 ..
-rw-r----- 1 logd   system 16192 1970-01-02 19:31 hilog.003.19700102-193147.gz
-rw-r----- 1 logd   system  8500 1970-01-02 19:32 hilog.004.19700102-193213.gz
#
#
           

第四步:将log檔案導出裝置,即可得到子產品的啟動log檔案。

#
# exit

D:\share>hdc_std.exe file recv /data/log/hilog/hilog.003.19700102-193147.gz D:\share
FileTransfer finish, Size:16192 time:55ms rate:294.40kB/s

D:\share>hdc_std.exe file recv /data/log/hilog/hilog.004.19700102-193213.gz D:\share
FileTransfer finish, Size:8500 time:25ms rate:340.00kB/s

D:\share>
D:\share>dir
......
2022/05/09  14:04         3,424,768 hdc_std.exe
2022/05/20  10:24            16,192 hilog.003.19700102-193147.gz
2022/05/20  10:25             8,500 hilog.004.19700102-193213.gz
......
D:\share>
           

四、其他輔助指令

-a <n>

隻顯示前<n>行日志

-z <n> 隻顯示後<n>行日志

示例如下;

# hilog -t app -a 5

01-02 03:12:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: CommonEvent_TimeManager --> handle common event: usual.event.TIME_TICK
01-02 03:12:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: TimeManager --> notifyTimeChange
01-02 03:12:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: EventManager --> publish localEvent type: Time_Change_Event
01-02 03:13:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: CommonEvent_TimeManager --> handle common event: usual.event.TIME_TICK
01-02 03:13:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: TimeManager --> notifyTimeChange
#
# 
# hilog -t app -z 5

01-02 04:02:00.032   875   875 I 02200/JsApp: SystemUI_Default tag: EventManager --> publish localEvent type: Time_Change_Event
01-02 04:02:00.032   875   875 I 02200/JsApp: SystemUI_Default tag: TimeManager --> notifyTimeChange
01-02 04:02:00.031   875   875 I 02200/JsApp: SystemUI_Default tag: CommonEvent_TimeManager --> handle common event: usual.event.TIME_TICK
01-02 04:01:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: EventManager --> publish localEvent type: Time_Change_Event
01-02 04:01:00.030   875   875 I 02200/JsApp: SystemUI_Default tag: TimeManager --> notifyTimeChange
#
           

這裡隻示範了常用的指令選項,如果需要其他選項請自行查閱附件表格,或通過hilog -h查詢。

附:hilog指令行使用表格

短選項 長選項 參數 說明
-h --help 幫助指令
預設 預設 阻塞讀日志,不退出
-x --exit 非阻塞讀日志,讀完退出
-g 查詢buffer的大小,配合-t指定某一類型使用,預設app和core
-G --buffer-size <size> 設定指定<type>日志類型緩沖區的大小,配合-t指定某一類型使用,預設app和core, 可使用B/K/M/G為機關
-r 清除buffer日志,配合-t指定某一類型使用,預設app和core
-p --privacy <on/off> 支援系統調試時日志隐私開關控制
on 打開隐私開關,顯示<private>
off 關閉隐私開關,顯示明文
-k <on/off> Kernel日志讀取開關控制
on 打開讀取kernel日志
off 關閉讀取kernel日志
-s --statistics 查詢統計資訊,需配合-t或-D使用
-S 清除統計資訊,需配合-t或-D使用
-Q <control-type> 流控預設配額開關控制
pidon 程序流控開關打開
pidoff 程序流控開關關閉
domainon domain流控開關打開
domainoff domain流控開關關閉
-L --level <level> 指定級别的日志,示例:-L D/I/W/E/F
-t --type <type> 指定類型的日志,示例:-t app core init
-D --domain <domain> 指定domain
-T --Tag <tag> 指定tag
-a --head <n> 隻顯示前<n>行日志
-z --tail <n> 隻顯示後<n>行日志
-P --pid <pid> 辨別不同的pid
-e --regex <expr> 隻列印日志消息與<expr>比對的行,其中<expr>是一個正規表達式
-f --filename <filename> 設定落盤的檔案名
-l --length <length> 設定落盤的檔案大小,需要大于等于64K
-n --number <number> 設定落盤檔案的個數
-j --jobid <jobid> 設定落盤任務的ID
-w --write <control> 落盤任務控制
query 落盤任務查詢
start 落盤任務開始,指令行參數為檔案名、單檔案大小、落盤算法、rotate檔案數目.
stop 落盤任務停止
-m --stream <algorithm> 落盤方式控制
none 無壓縮方式落盤
zlib zlib壓縮算法落盤,落盤檔案為.gz
zstd zstd壓縮算法落盤,落盤檔案為.zst
-v --format <format>
time 顯示本地時間
color 顯示不同級别顯示不同顔色,參數預設級别顔色模式處理(按黑白方式)
epoch 顯示相對1970時間
monotonic 顯示相對啟動時間
usec 顯示微秒精度時間
nsec 顯示納秒精度時間
year 顯示将年份添加到顯示的時間
zone 顯示将本地時區添加到顯示的時間
-b --baselevel <loglevel> 設定可列印日志的最低等級:D(DEBUG)/I(INFO)/W(WARN)/E(ERROR)/F(FATAL)

更多原創内容請關注:深開鴻技術團隊

入門到精通、技巧到案例,系統化分享HarmonyOS開發技術,歡迎投稿和訂閱,讓我們一起攜手前行共建鴻蒙生态。