C language journal printing

First-come renderings

To achieve that requires several conditions
1. only a defined color debugging software, such as SencureCRT
2. color definition according to the format of the output string

Source as follows:
1. Configure whether an output color is identified by BSP_CFG_LOG_COLOR
2. By BSP_CFG_LOG_LOCAL_LEVEL configuration log output level
3. BSP_LOG_OUT by configurable output interface
4. The time stamp obtaining interfaces BSP_LOG_TIMETAMP configuration, need not be formulated directly into the line 0

headers

  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 is  
39  
40  #ifdef the __cplusplus
 41 is  extern  " C " {
 42 is  #endif 
43 is  
44 is  
45  / * ********** **************** user configuration ******************************** *********** * / 
46 is  #define BSP_CFG_LOG_COLOR. 1 // whether the print color 
47  #define BSP_CFG_LOG_LOCAL_LEVEL BSP_LOG_VERBOSE // log output level 
48  
49  // log output interface 
50 #define BSP_LOG_OUT (the format, ...) printf (the format, ## __ VA_ARGS__)
 51 is  
52 is  / * for achieving the no printf
 53  // log output interface
 54 is  extern uint8_t log_bug [256];
 55  #define BSP_LOG_OUT (the format, .. .) do {\
 56 is  sprintf (log_bug, the format, ## __ VA_ARGS__); \
 57 is  hw_puart_send_bytes (log_bug, strlen (log_bug)); \
 58  } the while (0);
 59  
60  * / 
61 is  // stamp obtaining interface 
62 is  extern BSP_TIM_getRunTime int32_t ( void );
 63 is  #define BSP_LOG_TIMETAMP (uint32_t) BSP_TIM_getRunTime ()
 64  
65  
66  
67   
68  
69   
70  
71 is  
72  / * ************************** implementation code ************* ****************************** * / 
73 is  
74  // log level 
75 typedef enum {
 76 BSP_LOG_NONE, / * ! <None log output * / 
77 BSP_LOG_ERROR, / * ! <serious error, software modules can not recover on their own * / 
78 BSP_LOG_WARN, / * ! <have taken measures of reinstatement error condition * / 
79 BSP_LOG_INFO, / * ! <describe the normal flow of events information message * / 
80 BSP_LOG_DEBUG, / *! <Normal unwanted additional information (value, pointer, size, etc.). * / 
81 BSP_LOG_VERBOSE / * <large or frequent debugging information block message could submerge output! * / 
82  } Bsp_log_level_t;
 83  
84  // If not defined to the default level of logging level 
85  #ifndef BSP_CFG_LOG_LOCAL_LEVEL
 86  #define BSP_CFG_LOG_LOCAL_LEVEL BSP_LOG_INFO
 87  #endif 
88  
89   
90  
91 is  // color definition 
92  #if BSP_CFG_LOG_COLOR
 93  #define LOG_COLOR_BLACK "30"
 94  #define LOG_COLOR_RED "31 is"
 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 } the while ( 0 )
 147  
148   
149  
150  / * ************************************************************ External function ********** ********************************* * / 
151  
152  // error log output 
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  // alert log 
157  # the DEFINE BSP_LOGW (Tag, format, ...) BSP_EARLY_LOGW (Tag, format, ## __ VA_ARGS__)
 158  #defineBSP_LOG_HEXW (day, info, buf, len) BSP_LOG_HEX (day, 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 (day, info, buf, len) BSP_LOG_HEX (day, 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 (day, info, buf, len) BSP_LOG_HEX (day, 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__ */

 

Sample Code

#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);
}

 




 

Guess you like

Origin www.cnblogs.com/skyraker/p/12563271.html