printf函数封装

封装一

#define my_debug_firmware printf
#define my_app_debug(x,y...) my_debug_firmware("app debug =>fun(%s)line:%d:->"x,__FUNCTION__,__LINE__,##y)
#define my_net_debug(x,y...) my_debug_firmware("net debug =>fun(%s)line:%d:->"x,__FUNCTION__,__LINE__,##y)

#define print_array_debug(data, datalength) 	do\
{\
    int i;\
	my_debug_firmware("datalen=%d.\r\n", datalength);\
	for (i = 0; i < datalength; i++) {\
		my_debug_firmware("%02X ",(unsigned char)data[i]);\
	}\
	my_debug_firmware("\r\n");\
}while(0)

#define print_string_debug(data, datalength) 	do\
{\
	int i;\
	my_debug_firmware("datalen=%d.\r\n", datalength);\
	for (i = 0; i < datalength; i++) {\
		my_debug_firmware("%c",(unsigned char)data[i]);\
	}\
	my_debug_firmware("\r\n");\
}while(0)

封装二

#include <stdio.h>  
#include <stdarg.h>  
//系统宏定义 __FILE__ __FUNCTION__ __LINE__ __DATE__  __TIME__
void MyPrintf(const char *cmd, ...)  
{  
	struct timeval tv;
    gettimeofday(&tv,NULL);
    printf("MS: %ld ", tv.tv_sec*1000 + tv.tv_usec/1000);  
    printf("file:%s line:%s fun: %s -> ", __FILE__,__LINE__, __FUNCTION__);  
    va_list args;       //定义一个va_list类型的变量,用来储存单个参数  
    va_start(args,cmd); //使args指向可变参数的第一个参数  
    vprintf(cmd,args);  //必须用vprintf等带V的  
    va_end(args);       //结束可变参数的获取
    printf("\n");  
}  
#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
int main(){
    struct timeval tv;
    /*
	函数说明int gettimeofday (struct timeval * tv, struct timezone * tz)
	1、返回值:该函数成功时返回0,失败时返回-1 
	2、参数 
	struct timeval{ 
	long tv_sec; //秒 
	long tv_usec; //微秒 
	}; 
	struct timezone 
	{ 
	int tz_minuteswest; //和Greenwich 时间差了多少分钟 
	int tz_dsttime; //日光节约时间的状态 
	};
	 */
    gettimeofday(&tv,NULL);
    printf("second:%ld\n",tv.tv_sec);  //秒
    printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000);  //毫秒
    printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec);  //微秒
 	sleep(1); 
    MyPrintf("test");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liurunjiang/article/details/107364041