Print output level control, easy to debug and release version

Print output level control, convenient for debugging and release version, we often need to look at some print information when writing code, but when the version is released, there can be too many irrelevant prints, so some tips are needed to control the print level of the output

/*打印等级,修改后面的宏定义可以改变函数输出打印等级*/
#define ALG_PRTINT_LEVER PRINT_LEVEL_WRN

#define ALG_PRTINT(...)  SAL_printf(__VA_ARGS__)
#define ALG_PRT(...)     ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_UNLIMIT, __VA_ARGS__)
#define ALG_DBG(...)     ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_DBG,     __VA_ARGS__)
#define ALG_WAR(...)     ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_WRN,     __VA_ARGS__)
#define ALG_ERR(...)     ALG_PRTINT(__FUNCTION__, __LINE__, PRINT_LEVEL_ERR,     __VA_ARGS__)

/***********************************************************************************************//**
* @enum     HAT_SAL_PRT_LEVEL_E
* @brief    打印输出的等级
***************************************************************************************************/
typedef enum _PRT_LEVEL_E_
{
    
    
	PRINT_LEVEL_ERR = 0,        /*错误打印信息*/
	PRINT_LEVEL_WRN = 1,        /*警告打印信息*/
	PRINT_LEVEL_DBG = 2,         /*调试打印信息*/
	PRINT_LEVEL_UNLIMIT = 3,    /*无限制打印信息*/
	PRINT_LEVEL_NOPRT = 4,      /*没有打印信息*/
} PRT_LEVEL_E;

/*******************************************************************************
	Function:	SAL_printf
	Description: 该函数能够通过设置的打印等级ALG_PRTINT_LEVER,来控制是否输出相关语句
	Input:
	Output:
	Return:
			0:			Successful
			ohters:		Failed
*******************************************************************************/
void SAL_printf(const char *pFun, UINT line, PRT_LEVEL_E levelParam, const char *fmt, ...)
{
    
    
	static INT8 g_printfInfo[4][16] = {
    
     "ERR", "WAR", "DBG", "INF" };
	va_list p;
	if (ALG_PRTINT_LEVER == PRINT_LEVEL_NOPRT || levelParam == PRINT_LEVEL_NOPRT)
	{
    
    
		return;
	}
	if (levelParam <= ALG_PRTINT_LEVER )
	{
    
    
		va_start(p, fmt);
		printf("[DSP][%s][%s][%4d] ", g_printfInfo[levelParam], pFun, line);
		vprintf(fmt, p);
		va_end(p);
	}
}

Effect picture

Enter the following statement

		printf("[ALG ERROR][函数:%s][行号:%d],图片正常读取\n", __FUNCTION__, __LINE__);
		ALG_ERR("你好\n");
		ALG_ERR("你好%d\n", 245);
		ALG_WAR("你好\n");
		ALG_WAR("你好%d\n", 245);
		ALG_DBG("你好\n");
		ALG_PRT("你好%d\n", 245);
		ALG_ERR("你好%d\n", 245);

Set level PRINT_LEVEL_WRN

Insert picture description here

Set level PRINT_LEVEL_UNLIMIT

Insert picture description here

Guess you like

Origin blog.csdn.net/mao_hui_fei/article/details/113065839