日志打印


定义打印函数

参考ESP8266的打印,定义如下打印函数

#ifndef _A_LOG_H_
#define _A_LOG_H_
#include "stdio.h"

#define Log(format, args...)  printf( format, ##args ) /*这个地方替换成平台相关的打印函数*/

#ifdef  LOG_COLOR_EANBLE
#define LOG_COLOR_BLACK   "30"
#define LOG_COLOR_RED     "31"
#define LOG_COLOR_GREEN   "32"
#define LOG_COLOR_BROWN   "33"
#define LOG_COLOR_BLUE    "34"
#define LOG_COLOR_PURPLE  "35"
#define LOG_COLOR_CYAN    "36"
#define LOG_COLOR(COLOR)  "\033[0;" COLOR "m"
#define LOG_BOLD(COLOR)   "\033[1;" COLOR "m"
#define LOG_RESET_COLOR   "\033[0m"
#define LOG_COLOR_C       LOG_BOLD(LOG_COLOR_RED)
#define LOG_COLOR_E       LOG_COLOR(LOG_COLOR_RED)
#define LOG_COLOR_W       LOG_COLOR(LOG_COLOR_BROWN)
#define LOG_COLOR_I       LOG_COLOR(LOG_COLOR_GREEN)
#define LOG_COLOR_D       LOG_COLOR(LOG_COLOR_CYAN)
#define LOG_COLOR_V
#else
#define LOG_COLOR_C
#define LOG_COLOR_E
#define LOG_COLOR_W
#define LOG_COLOR_I
#define LOG_COLOR_D
#define LOG_COLOR_V
#define LOG_RESET_COLOR
#endif

typedef enum {
    LOG_NONE,       /*!< No log output */
    LOG_CRITICAL,   /*!< Critical log*/
    LOG_ERROR,      /*!< errors, software module can not recover on its own */
    LOG_WARN,       /*!< Error conditions from which recovery measures have been taken */
    LOG_INFO,       /*!< Information messages which describe normal flow of events */
    LOG_DEBUG,      /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
    LOG_VERBOSE     /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
&#125;loglevel;

#define LOG_FORMAT(letter, format)  LOG_COLOR_ ## letter "%-10.10s ""|%-04d | " "["#letter"]" " [%s]"  format LOG_RESET_COLOR
#define LOG_IMPL(tag, format, log_level, log_tag_letter, ...) do &#123;   \
            Log(LOG_FORMAT(log_tag_letter, format),__FUNCTION__,__LINE__, tag, ##__VA_ARGS__); \
        &#125;while(0)


#define logD( tag, format, ... )  LOG_IMPL( tag,format,LOG_DEBUG,   D,##__VA_ARGS__ )
#define logI( tag, format, ... )  LOG_IMPL( tag,format,LOG_INFO,    I,##__VA_ARGS__ )
#define logW( tag, format, ... )  LOG_IMPL( tag,format,LOG_WARN,    W,##__VA_ARGS__ )
#define logE( tag, format, ... )  LOG_IMPL( tag,format,LOG_ERROR,   E,##__VA_ARGS__ )
#define logC( tag, format, ... )  LOG_IMPL( tag,format,LOG_CRITICAL,C,##__VA_ARGS__ )

#define logDump( tag,level,data,len  ) (&#123;if(len>0)&#123;int i;Log("%-10.10s " "|%-04d | " "*********************** %04d **********************",__FUNCTION__,__LINE__,(int)len );\
for( i=0;i<len;i++ )\
&#123;\
    if( 0==i%20 ) \
    &#123;\
    Log("\r\n""%-10.10s " "|%-04d | ",__FUNCTION__,__LINE__);\
    &#125;\
    Log("%02x ",*((unsigned char*)(data+i)) );\
&#125;\
Log("\r\n\r\n");&#125;\
&#125;)
#endif

文章作者: Alex.Lin
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Alex.Lin !
 上一篇
Qt打包发布 Qt打包发布
这是你自定义的文章摘要内容,如果这个属性有值,文章卡片摘要就显示这段文字,否则程序会自动截取文章的部分内容作为摘要
2022-06-25 Alex.Lin
下一篇 
小白学习-蓝牙-01-NRF51822环境搭建 小白学习-蓝牙-01-NRF51822环境搭建
1. nRF-51822_xxAA 记录笔记 1. nRF-51822_xxAA 记录笔记 1.1. Date:2021-03-12 1.2. SDK 说明 1.2.1. Device family pack(nRF MDK) 1.2.
2021-03-09 Alex.Lin
  目录