[经验分享] 【TOOLS使用】OpenHarmony hilog系列之(三) HILOG格式符号列表及应用举例(珍藏) 原创

诚迈_雨哥 显示全部楼层 发表于 2024-6-1 15:17:24

一、HILOG格式符号列表及应用举例

序号 格式符 举例 输出
1 %{public}#x uint32_t ep = 5678;<br>LOGI("Eeexample:Cannot set freq at ep %{public}#x\n", ep);<br> Eeexample:Cannot set freq at ep 0x162e
2 %{public}02x uint16_t value1 = 256;<br>LOGI("Eeexample:packet filter %{public}02x\n", value1);<br> Eeexample:packet filter 100
3 %{public}04x uint32_t value2 = 5678;<br>LOGI("Eeexample:[read]: regAttr->regValue=0x%{public}04x", value2);<br> Eeexample:[read]: regAttr->regValue=0x162e
4 %{public}08x __u32 id = 1234;<br>LOGI("Eeexample:id=%{public}08x",id);<br> Eeexample:id=000004d2
5 %{public}08zx size_t idx = 1234;<br>LOGI("Eeexample:MtdDumpBuf: 0x%{public}08zx", idx);<br> Eeexample:MtdDumpBuf: 0x000004d2
6 %{public}0d int cmd = 706;<br>LOGI("Eeexample:I2cFsIoctl: not support cmd(%{public}0d)!!!", cmd);<br> Eeexample:I2cFsIoctl: not support cmd(706)!!!
7 %{public}0lu unsigned long arg = 123456;<br>LOGI("Eeexample:I2cFsIoctl: not support arg(%{public}0lu)!!!", arg);<br> Eeexample:I2cFsIoctl: not support arg(123456)!!!
8 %{public}0x uint32_t featureMap = 12345;<br>LOGI("Eeexample:%{public}s: featureMap=%{public}0x", __func__, featureMap);<br>uint8_t value3 = 123;<br>LOGI("Eeexample:%{public}s: i2c addr [%{public}0X] write failed", __func__, value3);<br> Eeexample:StartDrag: featureMap=3039<br>Eeexample:StartDrag: i2c addr [7B] write failed
9 %{public}-14s __u8 name[32] = "v4l2 camera";<br>LOGI("Eeexample:%{public}-14s", name);<br> Eeexample:v4l2 camera
10 %{public}c uint8_t buf[8] = "abcdef";<br>LOGI("Eeexample:Product_ID: %{public}c%{public}c%{public}c%{public}c", buf[0], buf[1], buf[2], buf[3]);<br> Eeexample:Product_ID: abcd
11 %{public}d int32_t status = -1;<br>LOGI("Eeexample:%{public}s: Dispatch USB_SERIAL_WRITE_SPEED failed status = %{public}d", __func__, status);<br> Eeexample:StartDrag: Dispatch USB_SERIAL_WRITE_SPEED failed status = -1
12 %{public}f float w = 0.1234;<br>float h = 0.66;<br>LOGI("Eeexample: ratio w: %{public}f ratio h: %{public}f", w, h);<br> Eeexample: ratio w: 0.123400 ratio h: 0.660000
13 %{public}hd int16_t busId = 12;<br>LOGI("Eeexample:I2cManagerAddCntlr: busId:%{public}hd exceed!", busId);<br> Eeexample:I2cManagerAddCntlr: busId:12 exceed!
14 %{public}hhd ///uint16_t fontId = 123;<br>///LOGI("Eeexample:Text::SetFontId invalid fontId(%{public}hhd)", fontId);<br>
15 %{public}hhu uint8_t status2 = 8;<br>LOGI("Eeexample:%{public}s: data status [%{public}hhu]", __func__, status2); Eeexample:StartDrag: data status [8]
16 %{public}hu uint16_t gpio = 123;<br>LOGI("Eeexample:%{public}s %{public}d, error gpio:%{public}hu", __func__, __LINE__, gpio);<br> Eeexample:StartDrag 227, error gpio:123
17 %{public}i uint32_t item = 123;<br>LOGI("Eeexample:chns = %{public}i\n", item);<br> Eeexample:chns = 123
18 %{public}jd off_t addr1 = 123;<br>LOGI("Eeexample:MtdDeviceCheckMsg: not page aligned, addr=%{public}jd!",addr1);<br> Eeexample:MtdDeviceCheckMsg: not page aligned, addr=123!
19 %{public}jx off_t addr2 = 123;<br>LOGI("Eeexample:MtdDeviceWriteReadByPageUnlock: no enough space, addr=0x%{public}jx!", addr2);<br> Eeexample:MtdDeviceWriteReadByPageUnlock: no enough space, addr=0x7b!
20 %{public}ld long int thread_id = atol("123");<br>LOGI("Eeexample:running thread id:%{public}ld start\n", thread_id);<br> Eeexample:running thread id:123 start
21 %{public}u uint32_t spiPort = 123;<br>LOGI("Eeexample:spi %{public}u open error.\n", spiPort);<br> Eeexample:spi 123 open error.
21 %{public}lf double timeUse = 0.12345;<br>LOGI("Eeexample:timeUse = %{public}lf,timeUse1= %{public}.1lf, timeUse2= %{public}.2lf\n", timeUse, timeUse, timeUse);<br> Eeexample:timeUse = 0.123450,timeUse1= 0.1, timeUse2= 0.12
22 %{public}lld int64_t timeOffset = 123456789;<br>int64t frameIndex = 987654321;<br>LOGI("Eeexample:Capture framIndex: %{public}lld, timeOffset: %{public}lld.", frameIndex_, timeOffset);<br>int32_t data = 256;<br>LOGI("Eeexample:tag value:%{public}lld", (long long)(data));<br> Eeexample:Capture framIndex: 987654321, timeOffset: 123456789.<br>Eeexample:tag value:256
23 %{public}llu uint64_t portDirection = 1024;<br>LOGI("Eeexample:Write response data portDirection=%{public}llu failed!", portDirection);<br> Eeexample:Write response data portDirection=1024 failed!
24 %{public}llx uint64_t pid = 123;<br>LOGI("Eeexample:I3cDeviceAdd: device already existed!: 0x%{public}llx!", pid);<br> Eeexample:I3cDeviceAdd: device already existed!: 0x7b!
25 %{public}lu unsigned long size = 4096;<br> LOGI("Eeexample:hinand_read: size=%{public}lu!", size);<br> Eeexample:hinand_read: size=4096!
26 %{public}lx unsigned long action = 8;<br>LOGI("Eeexample:action=0x%{public}lx", action);<br> Eeexample:action=0x8
27 %{public}m -- --
28 %{public}p uint8_t datum = 64;<br>uint8_t *data2 = &datum;<br>LOGI("Eeexample:data %{public}p.", data2);<br> Eeexample:data F767F4F3.
29 %{public}PRId64 const int64_t poolId = 777;<br>LOGI("Eeexample:can't get buffer pool, id = %{public}" PRId64 "", poolId);<br>int64_t appData = 888;<br>LOGI("Eeexample:appData[%{public}" PRId64 "]", appData);<br> Eeexample:can't get buffer pool, id = 777<br>Eeexample:appData[888]
30 %{public}PRIu64 uint64_t id3 = 4096;<br> LOGI("Eeexample:bufferPool is nullptr id: %{public}" PRIu64 "", id3);<br> Eeexample:bufferPool is nullptr id: 4096
31 %{public}PRIx64 uint64_t usage = 888;<br> LOGI("Eeexample:usage = 0x%{public}" PRIx64 "", usage);<br> Eeexample:usage = 0x378
32 %{public}s const char *tmp = "abcd";<br>LOGI("Eeexample:%{public}s",tmp);<br> Eeexample:abcd
33 %{public}u uint8_t srcWidth = 8;<br>LOGI("Eeexample:DmacAlignedTransMax: srcwidth:%{public}u,!", srcWidth);<br> Eeexample:DmacAlignedTransMax: srcwidth:8,!
34 %{public}x uint32_t addr3 = 256;<br>LOGI("Eeexample:Write err regAddr: 0x%{public}x.\n", addr3);<br> Eeexample:Write err regAddr: 0x100.
35 %{public}zd ssize_t ret = 256;<br>LOGI("Eeexample:write data %{public}zd less than.", ret);<br> Eeexample:write data 256 less than.
36 %{public}zu size_t length = 256;<br>LOGI("Eeexample:write data less than %{public}zu.", length);<br> Eeexample:write data less than 256.
37 __func__ LOGI("Eeexample:%{public}s lseek file fail.",__func__);<br> Eeexample:StartDrag lseek file fail.
38 __LINE__ LOGI("Eeexample:Line:%{public}d.",__LINE__);<br> Eeexample:Line:303.
39 %{public}ju -- --

二、日志打印的样例

初学者常用样例,建议备份到本地,参照修改。

// 1 %{public}#x
uint32_t ep = 5678;
LOGI("Eeexample:Cannot set freq at ep %{public}#x\n", ep);
// 2 %{public}02x
uint16_t value1 = 256;
LOGI("Eeexample:packet filter %{public}02x\n", value1);
// 3 %{public}04x
uint32_t value2 = 5678;
LOGI("Eeexample:[read]: regAttr->regValue=0x%{public}04x", value2);
// 4 %{public}08x
__u32 id = 1234;
LOGI("Eeexample:id=%{public}08x",id);
// 5 %{public}08zx
size_t idx = 1234;
LOGI("Eeexample:MtdDumpBuf: 0x%{public}08zx", idx);
// 6 %{public}0d
int cmd = 706;
LOGI("Eeexample:I2cFsIoctl: not support cmd(%{public}0d)!!!", cmd);
// 7 %{public}0lu
unsigned long arg = 123456;
LOGI("Eeexample:I2cFsIoctl: not support arg(%{public}0lu)!!!", arg);
// 8 %{public}0x
uint32_t featureMap = 12345;
LOGI("Eeexample:%{public}s: featureMap=%{public}0x", __func__, featureMap);

uint8_t value3 = 123;
LOGI("Eeexample:%{public}s: i2c addr [%{public}0X] write failed", __func__, value3);
// 9 %{public}-14s
__u8 name[32] = "v4l2 camera";
LOGI("Eeexample:%{public}-14s", name);
// 10 %{public}c
uint8_t buf[8] = "abcdef";
LOGI("Eeexample:Product_ID: %{public}c%{public}c%{public}c%{public}c", buf[0], buf[1], buf[2], buf[3]);
// 11 %{public}d
int32_t status = -1;
LOGI("Eeexample:%{public}s: Dispatch USB_SERIAL_WRITE_SPEED failed status = %{public}d", __func__, status);
// 12 %{public}f
float w = 0.1234;
float h = 0.66;
LOGI("Eeexample: ratio w: %{public}f  ratio h: %{public}f", w, h);
// 13 %{public}hd
int16_t busId = 12;
LOGI("Eeexample:I2cManagerAddCntlr: busId:%{public}hd exceed!", busId);
// 14 %{public}hhd
///uint16_t fontId = 123;
///LOGI("Eeexample:Text::SetFontId invalid fontId(%{public}hhd)", fontId);
// 15 %{public}hhu
uint8_t status2 = 8;
LOGI("Eeexample:%{public}s: data status [%{public}hhu]", __func__, status2);
// 16 %{public}hu
uint16_t gpio = 123;
LOGI("Eeexample:%{public}s %{public}d, error gpio:%{public}hu", __func__, __LINE__, gpio);
// 17 %{public}i
uint32_t item = 123;
LOGI("Eeexample:chns = %{public}i\n", item);
// 18 %{public}jd
off_t addr1 = 123;
LOGI("Eeexample:MtdDeviceCheckMsg: not page aligned, addr=%{public}jd!",addr1);
// 19 %{public}jx
off_t addr2 = 123;
LOGI("Eeexample:MtdDeviceWriteReadByPageUnlock: no enough space, addr=0x%{public}jx!", addr2);
// 20 %{public}ld
long int thread_id = atol("123");
LOGI("Eeexample:running thread id:%{public}ld start\n", thread_id);
// 21 %{public}u
uint32_t spiPort = 123;
LOGI("Eeexample:spi %{public}u open error.\n", spiPort);
// 21 %{public}lf
double timeUse = 0.12345;
LOGI("Eeexample:timeUse = %{public}lf,timeUse1= %{public}.1lf, timeUse2= %{public}.2lf\n", timeUse, timeUse, timeUse);
// 22 %{public}lld
int64_t timeOffset = 123456789;
int64_t frameIndex_ = 987654321;
LOGI("Eeexample:Capture framIndex: %{public}lld, timeOffset: %{public}lld.", frameIndex_, timeOffset);
int32_t data = 256;
LOGI("Eeexample:tag value:%{public}lld", (long long)(data));
// 23 %{public}llu
uint64_t portDirection = 1024;
LOGI("Eeexample:Write response data portDirection=%{public}llu failed!", portDirection);
// 24 %{public}llx
uint64_t pid = 123;
LOGI("Eeexample:I3cDeviceAdd: device already existed!: 0x%{public}llx!", pid);
// 25 %{public}lu
unsigned long size = 4096;
 LOGI("Eeexample:hinand_read: size=%{public}lu!", size);
// 26 %{public}lx
unsigned long action = 8;
LOGI("Eeexample:action=0x%{public}lx", action);
// 27 %{public}m

// 28 %{public}p
uint8_t datum = 64;
uint8_t *data2 = &datum;
LOGI("Eeexample:data %{public}p.", data2);
// 29 %{public}PRId64 
const int64_t poolId = 777;
LOGI("Eeexample:can't get buffer pool, id = %{public}" PRId64 "", poolId);

int64_t appData = 888;
LOGI("Eeexample:appData[%{public}" PRId64 "]", appData);
// 30 %{public}PRIu64
uint64_t id3 = 4096;
 LOGI("Eeexample:bufferPool is nullptr id: %{public}" PRIu64 "", id3);
// 31 %{public}PRIx64
uint64_t usage = 888;
 LOGI("Eeexample:usage = 0x%{public}" PRIx64 "", usage);
// 32 %{public}s
const char *tmp = "abcd";
LOGI("Eeexample:%{public}s",tmp);
// 33 %{public}u
uint8_t srcWidth = 8;
LOGI("Eeexample:DmacAlignedTransMax: srcwidth:%{public}u,!", srcWidth);
// 34 %{public}x
uint32_t addr3 = 256;
LOGI("Eeexample:Write err regAddr: 0x%{public}x.\n", addr3);
// 35 %{public}zd
ssize_t ret = 256;
LOGI("Eeexample:write data %{public}zd less than.", ret);
// 36 %{public}zu
size_t length = 256;
LOGI("Eeexample:write data less than %{public}zu.",  length);

// 37 __func__
LOGI("Eeexample:%{public}s lseek file fail.", __func__);
// 38 __LINE__
LOGI("Eeexample:Line:%{public}d.", __LINE__);
// 39 %{public}ju

三、补充说明

1.日志格式符的说明

  • 如果是用户态的日志,必须添加{public}才能被打印出来;
  • 如果是内核态的日志,%后面直接跟格式符;

2. __func__,__LINE__ 受到markdown 格式限制,使用时没有一撇

3. 很多子系统对LOG 都进行了二次封装,一定要使用封装后的函数

4. 日志等级的选择

// 日志定义了5个级别,优先级从低到高依次是:debug、info、warn、error、fatal。

级别 描述
LOG_DEBUG = 3 输出DEBUG级别的日志,表示仅用于调试,默认不输出
LOG_INFO = 4 输出INFO级别的日志,表示普通的信息,一般都会输出
LOG_WARN = 5 输出WARN级别的日志,表示存在警告,满足级别才会输出
LOG_ERROR = 6 输出ERROR级别的日志,表示存在错误,一定输出
LOG_FATAL = 7 输出FATAL级别的日志,表示致命错误,一定输出

5. 千万不要在时钟、线程内不加限制地打印 INFO 日志,否则日志会爆屏

四、参考链接

无用

©著作权归作者所有,转载或内容合作请联系作者

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部