分类显示
[出错(颜色)][日期时间][模块][等级][文件][函数][行号][信息~~~~~~~~~~~]
开关需求
->1.出错开关带颜色
->2.重要信息开关带颜色
->3.常用信息开关带颜色
->4.调试信息开关带颜色
打印总开关->5.日期开关
->6.文件开关
->7.函数开关
->8.行号开关
->9.模块开关
宏控制与代码控制的优点:
减少栈分配
提升性能
1.方便移植
2.调试追踪
3.日期追踪,时间戳计算,测试出错时间点,测试时间戳
4.打印输出控制,关闭打开特定输出
5.红色关键错误闪烁显示
6.红色错误显示
7.黄色警告显示
8.打印文件位置函数行号,定位快速定位错误点
9.分类显示注重关注点
10.颜色有助于观察出错,调试,关键信息点
cfg_printf.h
#ifndef __CFG_PRINTF_H__
#ifdef __cplusplus
extern "C" {
#endif
#define ALL_PRINTF_STATUS 1 //all printf switch
#define TIME_PRINTF_STATUS 1 //date and time control switch
#define TIMESTAMP_PRINTF_STATUS 0 //timestamp switch
#define DATE_PRINTF_STATUS 1 //date switch
#define MODULE_PRINTF_STATUS 0 //module switch
#define FILE_PRINTF_STATUS 1 //file name switch
#define FUNC_PRINTF_STATUS 1 //function name switch
#define LINE_PRINTF_STATUS 1 //line number switch
#define COLOR_PRINTF_STATUS 1 //printf with color switch
//close different type printf
#define CLOSE_TRA_PRINTF 0 //tracing for debug
#define CLOSE_INF_PRINTF 0 //information
#define CLOSE_WAR_PRINTF 0 //warning
#define CLOSE_ERR_PRINTF 0 //error
#define CLOSE_CRI_PRINTF 0 //critical
/*
typedef enum {
DEBUG_L,//for debug
INFO_L,//for necessary show
WARNING_L,//for warnning show
ERROR_L,//must show for development
CRITICAL_L,//must show for development,for critical use
EMERG_L,//we must correct this error if this happen
}ENUM_PF_LEVEL;
*/
extern void _drv_printf(const char *p_format, ...);
extern void _date_printf(void);
#define DRV_PRINTF _drv_printf
#define DATE_PRINTF _date_printf
//module printf
#if (MODULE_PRINTF_STATUS == 1)
#define MODULE_PRINTF(module) DRV_PRINTF("[%s]", #module)
#else
#define MODULE_PRINTF(...)
#endif
#define LEVEL_PRINTF(level) DRV_PRINTF("[%s]", #level)
//file printf
#if (FILE_PRINTF_STATUS == 1)
#define FILE_PRINTF(...) DRV_PRINTF("[%s]", __FILE__)
#else
#define FILE_PRINTF(...)
#endif
//function printf
#if (FUNC_PRINTF_STATUS == 1)
#define FUNCTION_PRINTF(...) DRV_PRINTF("[%s]", __FUNCTION__)
#else
#define FUNCTION_PRINTF(...)
#endif
//line printf
#if (LINE_PRINTF_STATUS == 1)
#define LINE_PRINTF(...) DRV_PRINTF("[%d]", __LINE__)
#else
#define LINE_PRINTF(...)
#endif
#if (COLOR_PRINTF_STATUS == 1)
#define BLUE_STR "\033[22;34m"
#define WHITE_STR "\033[22;37m"
#define YELLOW_STR "\033[22;33m"
#define RED_STR "\033[22;31m"
#define L_RED_STR "\033[01;31m"
#define BLING_STR "\033[5m"
#define BLING_RED_STR "\033[5;31m"
#define DEFAULT_STR "\033[0m"
#else
#define BLUE_STR ""
#define WHITE_STR ""
#define YELLOW_STR ""
#define RED_STR ""
#define L_RED_STR ""
#define BLING_STR ""
#define BLING_RED_STR ""
#define DEFAULT_STR ""
#endif
#if (COLOR_PRINTF_STATUS == 1)
#define COLOR_PRINTF DRV_PRINTF
#else
#define COLOR_PRINTF(...)
#endif
#if (ALL_PRINTF_STATUS == 1)
#define cfg_printf(module, level, ...) do {\
DATE_PRINTF(); \
MODULE_PRINTF(module);\
LEVEL_PRINTF(level);\
FILE_PRINTF();\
FUNCTION_PRINTF();\
LINE_PRINTF();\
DRV_PRINTF(__VA_ARGS__);\
}while(0)
#else
#define cfg_printf(...)
#endif
#if (CLOSE_TRA_PRINTF == 1)
#define tra_printf(...)
#else
#define tra_printf(...) do { \
COLOR_PRINTF(BLUE_STR); \
cfg_printf(tracing, tracing,__VA_ARGS__); \
COLOR_PRINTF(DEFAULT_STR); \
} while(0)
#endif
#if (CLOSE_INF_PRINTF == 1)
#define inf_printf(...)
#else
#define inf_printf(...) do { \
COLOR_PRINTF(WHITE_STR); \
cfg_printf(info, info,__VA_ARGS__); \
COLOR_PRINTF(DEFAULT_STR); \
} while(0)
#endif
#if (CLOSE_INF_PRINTF == 1)
#define war_printf(...)
#else
#define war_printf(...) do { \
COLOR_PRINTF(YELLOW_STR); \
cfg_printf(warning, warning,__VA_ARGS__); \
COLOR_PRINTF(DEFAULT_STR); \
} while(0)
#endif
#if (CLOSE_INF_PRINTF == 1)
#define err_printf(...)
#else
#define err_printf(...) do { \
COLOR_PRINTF(RED_STR); \
cfg_printf(error, error,__VA_ARGS__); \
COLOR_PRINTF(DEFAULT_STR); \
} while(0)
#endif
#if (CLOSE_CRI_PRINTF == 1)
#define cri_printf(...)
#else
#define cri_printf(...) do { \
COLOR_PRINTF(BLING_RED_STR); \
cfg_printf(critical, critical,__VA_ARGS__); \
COLOR_PRINTF(DEFAULT_STR); \
} while(0)
#endif
#ifdef __cplusplus
}
#endif
#endif
cfg_printf.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "cfg_printf.h"
//driver printf
void _drv_printf(const char *p_format, ...){
va_list ap;
va_start(ap, p_format);
vprintf((const char *)p_format, ap);
va_end(ap);
}
//time printf
#if (TIME_PRINTF_STATUS == 1)
#include <time.h>
void _date_printf(void) {
time_t time_stamp;
struct tm *local_time;
time_stamp= time(NULL);
local_time = localtime(&time_stamp);
#if (TIMESTAMP_PRINTF_STATUS == 1)
_drv_printf("[%ld]", time_stamp);
#endif
#if (DATE_PRINTF_STATUS == 1)
_drv_printf("[%04d-%02d-%02d %02d:%02d:%02d]", local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday, local_time->tm_hour, local_time->tm_min, local_time->tm_sec);
#endif
}
#else
void _date_printf(void) {
}
#endif
/*
void need_show(void) {
cfg_printf(test, cfg, "test start\n");
}
int main(void)
{
need_show();
printf("end\n");
}
*/
main.c
#include <stdio.h>
#include "cfg_printf.h"
int main(void) {
cfg_printf(test, cfg, "test start\n");
tra_printf("tracing start\n");
inf_printf("information start\n");
war_printf("warning start\n");
err_printf("error start\n");
cri_printf("critical start\n");
//need_show();
return 0;
}