C语言实现日志打印效果

先来个效果图

要实现这个效果需要几个条件
1.一个只是颜色定义的调试软件,如SencureCRT
2.按照颜色定义的格式输出字符串

源码如下:
1.通过BSP_CFG_LOG_COLOR 配置是否输出颜色标识
2.通过BSP_CFG_LOG_LOCAL_LEVEL 配置日志输出等级
3.通过BSP_LOG_OUT 配置输出接口
4.通过BSP_LOG_TIMETAMP 配置时间戳获取接口,不需要直接配制成0就行了

头文件

  1 /**
  2 ************************************* Copyright ****************************** 
  3 * FileName : bsp_log.h 
  4 * Version : v1.0    
  5 * Author : skyraker    
  6 * Date : 2020-01-13 
  7 * Description: 从ESP32 中抠出来改的 
  8 demo:
  9 #include "bsp_log.h"
 10 #define TAG "MAIN"
 11 void BSP_LOG_test(){
 12 BSP_LOG_HEXE(TAG,"HEX TEST","123456789",9);
 13 BSP_LOG_HEXW(TAG,"HEX TEST","123456789",9);
 14 BSP_LOG_HEXI(TAG,"HEX TEST","123456789",9);
 15 BSP_LOG_HEXD(TAG,"HEX TEST","123456789",9);
 16 BSP_LOG_HEXV(TAG,"HEX TEST","123456789",9);
 17 
 18 BSP_LOGI(TAG,"%s","info");
 19 BSP_LOGW(TAG,"%s","warning");
 20 BSP_LOGD(TAG,"%s","debug");
 21 BSP_LOGV(TAG,"%s","verbose");
 22 }
 23 int main(void)
 24 {
 25 BSP_LOG_test();
 26 while(1);
 27 }
 28 ******************************************************************************
 29 */
 30 
 31 
 32 
 33 #ifndef __BSP_LOG_H__
 34 #define __BSP_LOG_H__
 35 
 36 #include <stdint.h>
 37 #include <stdarg.h>
 38 
 39 
 40 #ifdef __cplusplus
 41 extern "C" {
 42 #endif
 43 
 44 
 45 /***************************用户配置********************************************/
 46 #define BSP_CFG_LOG_COLOR 1 //是否需要打印颜色
 47 #define BSP_CFG_LOG_LOCAL_LEVEL BSP_LOG_VERBOSE //日志输出水平
 48 
 49 //日志输出接口
 50 #define BSP_LOG_OUT(format,...) printf(format,##__VA_ARGS__)
 51 
 52 /*对于没有printf的实现
 53 //日志输出接口
 54 extern uint8_t log_bug[256];
 55 #define BSP_LOG_OUT(format,...) do{ \
 56 sprintf(log_bug,format,##__VA_ARGS__); \
 57 hw_puart_send_bytes(log_bug,strlen(log_bug)); \
 58 }while(0);
 59 
 60 */
 61 //时间戳获取接口
 62 extern int32_t BSP_TIM_getRunTime(void);
 63 #define BSP_LOG_TIMETAMP (uint32_t)BSP_TIM_getRunTime()
 64 
 65  
 66 
 67  
 68 
 69  
 70 
 71 
 72 /***************************实现代码********************************************/
 73 
 74 //日志等级
 75 typedef enum {
 76 BSP_LOG_NONE, /*!< 无日志输出 */
 77 BSP_LOG_ERROR, /*!< 严重错误,软件模块无法自行恢复 */
 78 BSP_LOG_WARN, /*!< 已采取恢复措施的错误条件 */
 79 BSP_LOG_INFO, /*!< 描述正常事件流的信息消息 */
 80 BSP_LOG_DEBUG, /*!< 正常使用不需要的其他信息(值,指针,大小等)。 */
 81 BSP_LOG_VERBOSE /*!< 较大的调试信息块或频繁的消息可能会淹没输出 */
 82 } Bsp_log_level_t;
 83 
 84 //如果没有定义日志等级就使用默认等级
 85 #ifndef BSP_CFG_LOG_LOCAL_LEVEL
 86 #define BSP_CFG_LOG_LOCAL_LEVEL BSP_LOG_INFO
 87 #endif
 88 
 89  
 90 
 91 // 颜色定义
 92 #if BSP_CFG_LOG_COLOR
 93 #define LOG_COLOR_BLACK "30"
 94 #define LOG_COLOR_RED "31"
 95 #define LOG_COLOR_GREEN "32"
 96 #define LOG_COLOR_BROWN "33"
 97 #define LOG_COLOR_BLUE "34"
 98 #define LOG_COLOR_PURPLE "35"
 99 #define LOG_COLOR_CYAN "36"
100 #define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
101 #define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
102 #define LOG_RESET_COLOR "\033[0m"
103 #define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
104 #define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
105 #define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
106 #define LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLUE)
107 #define LOG_COLOR_V LOG_COLOR(LOG_COLOR_BLACK)
108 #else //BSP_CFG_LOG_COLOR
109 #define LOG_COLOR_E
110 #define LOG_COLOR_W
111 #define LOG_COLOR_I
112 #define LOG_COLOR_D
113 #define LOG_COLOR_V
114 #define LOG_RESET_COLOR 
115 #endif //BSP_CFG_LOG_COLOR
116 
117 #define LOG_FORMAT(letter, format) LOG_COLOR_ ##letter #letter "(%d)[%s]: " format LOG_RESET_COLOR "\r\n"
118 #define BSP_EARLY_LOGE( tag, format, ... ) BSP_LOG_EARLY_IMPL(tag, format, BSP_LOG_ERROR, E, ##__VA_ARGS__)
119 #define BSP_EARLY_LOGW( tag, format, ... ) BSP_LOG_EARLY_IMPL(tag, format, BSP_LOG_WARN, W, ##__VA_ARGS__)
120 #define BSP_EARLY_LOGI( tag, format, ... ) BSP_LOG_EARLY_IMPL(tag, format, BSP_LOG_INFO, I, ##__VA_ARGS__)
121 #define BSP_EARLY_LOGD( tag, format, ... ) BSP_LOG_EARLY_IMPL(tag, format, BSP_LOG_DEBUG, D, ##__VA_ARGS__)
122 #define BSP_EARLY_LOGV( tag, format, ... ) BSP_LOG_EARLY_IMPL(tag, format, BSP_LOG_VERBOSE, V, ##__VA_ARGS__)
123 
124 #define BSP_LOG_EARLY_IMPL(tag, format, log_level, log_tag_letter, ...) do { \
125 if (BSP_CFG_LOG_LOCAL_LEVEL >= log_level) { \
126 BSP_LOG_OUT(LOG_FORMAT(log_tag_letter, format), BSP_LOG_TIMETAMP ,tag, ##__VA_ARGS__); \
127 }} while(0)
128 
129 
130 //HEX输出头    
131 #define HEX_FORMAT_HEARDER(letter,info) LOG_COLOR_ ##letter #letter "(%d)[%s]: " info
132 //HEX输出结尾    
133 #define HEX_FORMAT_END LOG_RESET_COLOR "\r\n"
134 #define HEXDUMP(P,L) do { \
135 uint16_t _log_n=0;\
136 for( _log_n=0;log_n<L;n++){ \
137 BSP_LOG_OUT("%02X ",P[_log_n]);\
138 }\
139 }while(0)
140 #define BSP_LOG_HEX(tag,info,buf,len,log_level,log_tag_letter) do{ \
141 if(BSP_CFG_LOG_LOCAL_LEVEL>log_level){\
142 BSP_LOG_OUT(HEX_FORMAT_HEARDER(log_tag_letter,info),BSP_LOG_TIMETAMP,tag);\
143 HEXDUMP(buf,len);\
144 BSP_LOG_OUT(HEX_FORMAT_END);\
145 }\
146 }while(0)
147 
148  
149 
150 /***************************对外函数********************************************/
151 
152 //错误日志输出 
153 #define BSP_LOGE( tag, format, ... ) BSP_EARLY_LOGE(tag, format, ##__VA_ARGS__)
154 #define BSP_LOG_HEXE( tag,info,buf,len) BSP_LOG_HEX(tag,info,buf,len,BSP_LOG_ERROR,E)
155 
156 //警告日志
157 #define BSP_LOGW( tag, format, ... ) BSP_EARLY_LOGW(tag, format, ##__VA_ARGS__)
158 #define BSP_LOG_HEXW( tag,info,buf,len) BSP_LOG_HEX(tag,info,buf,len,BSP_LOG_WARN,W)
159 
160 //信息日志
161 #define BSP_LOGI( tag, format, ... ) BSP_EARLY_LOGI(tag, format, ##__VA_ARGS__)
162 #define BSP_LOG_HEXI( tag,info,buf,len) BSP_LOG_HEX(tag,info,buf,len,BSP_LOG_INFO,I)
163 
164 //调试日志
165 #define BSP_LOGD( tag, format, ... ) BSP_EARLY_LOGD(tag, format, ##__VA_ARGS__)
166 #define BSP_LOG_HEXD( tag,info,buf,len) BSP_LOG_HEX(tag,info,buf,len,BSP_LOG_DEBUG,D)
167 
168 //详细日志
169 #define BSP_LOGV( tag, format, ... ) BSP_EARLY_LOGV(tag, format, ##__VA_ARGS__)
170 #define BSP_LOG_HEXV( tag,info,buf,len) BSP_LOG_HEX(tag,info,buf,len,BSP_LOG_VERBOSE,V)
171 
172  
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 
178 
179 #endif /* __BSP_LOG_H__ */

示例代码

#include "bsp_log.h"
#define TAG "MAIN"
void BSP_LOG_test(){
BSP_LOG_HEXE(TAG,"HEX TEST","123456789",9);
BSP_LOG_HEXW(TAG,"HEX TEST","123456789",9);
BSP_LOG_HEXI(TAG,"HEX TEST","123456789",9);
BSP_LOG_HEXD(TAG,"HEX TEST","123456789",9);
BSP_LOG_HEXV(TAG,"HEX TEST","123456789",9);    
BSP_LOGI(TAG,"%s","info");
BSP_LOGW(TAG,"%s","warning");
BSP_LOGD(TAG,"%s","debug");
BSP_LOGV(TAG,"%s","verbose");
}
int main(void)
{
BSP_LOG_test();
while(1);
}




猜你喜欢

转载自www.cnblogs.com/skyraker/p/12563271.html