嵌入式log打印格式输出技巧

Log 信息格式

参考目前主流嵌入式、安卓等输出方式:

[日志级别] 文件名 : 日志信息
例:[info] main.c : init ok!
例:[debug] adc.c : adc_getvalue -> 3.3v

参考 Java 日志框架的输出方式:

[ 文件名] 日志级别 : 日志信息
例:[ main] info : init ok!
例: [ adc] debug : adc_getvalue -> 3.3v

条件编译

因为在进行单片机开发的过程中,需要大量的 Log 信息,但是在开发结束后,一直打印log会拖慢单片机的速度,所以在开发完成后需要关闭log。

在头文件中添加:

#define _DEBUG_ 1 //打印Log信息,不想打印时改为0即可

再把.c 文件中将所有的 printf 包裹上 #if _DEBUG_#endif

#if _DEBUG_
printf("test");
#endif

可变参数宏

#ifdef _DEBUG_

#define user_printf(s,...)	printf(s"\r\n",##__VA_ARGS__)
#define user_info(format,...)   printf("[info] %s() %d "format"\r\n",__func__,__LINE__,##__VA_ARGS__)
#define user_error(format,...)  printf("[error] %s %s()%d "format"\r\n",__FILE__,__func__,__LINE__,##__VA_ARGS__)

#else

#define user_printf(s,...)
#define user_info(s,...)
#define user_error(format,...)

#endif

使用可变参数宏不仅达到了代码的格式化,同时也完成了条件编译,不用每次打印都包裹上 #if _DEBUG_#endif

打印效果如下:

user_printf("test 1");
user_printf("test 2");
user_info("test 3");
user_error("test 4");

打印效果

C标准中一些预定义的宏

__DATE__

进行预处理的日期(“Mmm dd yyyy”形式的字符串文字)

__FILE__

代表当前源代码文件名的字符串文字

__LINE__

代表当前源代码中的行号的整数常量

__TIME__

源文件编译时间,格式微“hh:mm:ss”

__func__

当前所在函数名

格式输出16进制数组

void dbg_hexarray(char *title, unsigned char *buf, unsigned int sz)
{
	unsigned int i = 0;
	if (title){
		printf((title));
	}
		
	for(i = 0; i < sz; i++)
	{
		if((i%8) == 0){
			printf("[%04x] ",(unsigned int)i);
		}
			
		printf("%02x ",(unsigned int)buf[i]);

		if(((i+1)%8) == 0){
			printf("\r\n");
		}
	}

	if((i%8) != 0){
		printf("\r\n");
	}	
}

打印效果如下:

unsigned char buff[10]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09}; 
dbg_hexarray("buff:",buff,10);

打印效果

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值