C log代码

    本文地址:http://tongxinmao.com/Article/Detail/id/298

    //转自https://www.amobbs.com/thread-5687433-1-1.html?_dsign=f99f1341
    #ifndef __DEBUG_H
    #define        __DEBUG_H
    
    #include "stdarg.h"
    #define SHORT_FILE strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__
    
    #define _STR(s)     #s
    #define STR(s)      _STR(s)   
    
    #define LOG_OUT   //定义 LOG_OUT,则打开调试
    
    #define LOG_INFO   1       // =1 打开LOG_INFO;=0 关闭LOG_INFO
    #define LOG_DEBUG  1   // =1 打开LOG_DEBUG;=0 关闭LOG_DEBUG
    #define LOG_TRACE  1   // =1 打开LOG_TRACE;=0 关闭LOG_TRACE
    #define LOG_ERROR  1   // =1 打开LOG_ERROR;=0 关闭LOG_ERROR
    
    #define LOG_Send   USART1_Send   //USART2_Send   //USB_TxWrite //定义log输出通道
    void _printf( u8 *Data,...);
    
    #ifdef LOG_OUT
    #define LOG(level, format, ...)    do {          \
            if(level==1)                             \
            _printf("[%s][%s@%s,%d] " format "\r\n", \
            _STR(_##level), __func__, SHORT_FILE, __LINE__, ##__VA_ARGS__ ); } \
            while (0)  
    #else
    #define LOG(level, format, ...)
    #endif      
    
    #endif 
    
    
    
    
    #include    "debug.h"
    #include    "bsp_usart1.h"  //对应的bsp_usart1.c这里面实现了void USART1_Send(uint8_t *pbuffer, uint32_t size)
    
    
    static u8 DebugBuffer[255];
    static u8 DebugSize;
    /********************************************************
     * 函数名:itoa
     * 描述  :将整形数据转换成字符串,支持2-16进制,支持负数
     * 输入  :-radix =10 表示10进制,
     *         -value 要转换的整形数
     *         -buf 转换后的字符串
     *         -radix = 10
    
     *******************************************************/
    static char *itoa(signed int num, char *str, int radix)   
    {
        char  string[] = "0123456789ABCDEF";
            
        char* ptr = str;
            char temp;
        int i;
        int j;
        
             if(radix<2||radix>16)//增加了对错误的检测  
        {  
          
            return str;  
        }  
            
         if (num < 0)
        {
            /* Make the value positive. */
            num = -num;
        }
            
        do{
            *ptr++  = string[num % radix];
            num    /= radix;
    
        }while (num);
    
        j = ptr - str-1;
        for (i = 0; i < (ptr - str) / 2; i++)
        {
            temp = str[i];
            str[i]   = str[j];
            str[j--] = temp;
        }
            
        j = ptr - str-1;
        if(num<0)
        {
            for(i=j;i==0;i--)
            {
                    str[i+1]=str[i];
            }
            str[0]='-';
            str[j+2]=0;        
        }
        else
        {
            str[j+1]=0;
        }
            
    
        return str;
    } 
    
    /************************************************************************
     * 函数名:_printf
     * 描述  :格式化输出,类似于C库中的printf,但这里没有用到C库
     *                     -Data   要发送到串口的内容的指针
     *                           -...    其他参数
     * 输出  :无
     * 返回  :无 
     * 调用  :外部调用
     *         典型应用_printf( "\r\n this is a demo \r\n" );
     *                             _printf(  "\r\n %d \r\n", i );
     *                             _printf(  "\r\n %s \r\n", j );
     ***************************************************************************/
    void _printf( u8 *Data,...)
    {
      const char *s;
      int d;   
      char buf[20];
      va_list ap;
      va_start(ap, Data);
      DebugSize = 0;
      while ( *Data != 0)                   // 判断是否到达字符串结束符
      {                                                          
          if ( *Data == 0x5c )              //'\'
          {                                                                          
                  switch ( *++Data )
                  {
                          case 'r':                //回车符
                                  DebugBuffer[DebugSize++]=0x0d;
                                  Data ++;
                                  break;
    
                          case 'n':                //换行符
                                  DebugBuffer[DebugSize++]=0x0a;        
                                  Data ++;
                                  break;
                          
                          default:
                                  Data ++;
                              break;
                  }                         
          }
          else if ( *Data == '%')
          {                                        //
              switch ( *++Data )
              {                                
                  case 's':                        //字符串
                       s = va_arg(ap, const char *);
                       for ( ; *s; s++) 
                       {
                           DebugBuffer[DebugSize++] = *s;
                       }
                       Data++;
                       break;
                       
                  case 'b':   
                       d = va_arg(ap, int);
                       itoa(d, buf, 2);
                       for (s = buf; *s; s++) 
                       {
                          DebugBuffer[DebugSize++]= *s;
                       }
                       Data++;
                       break;
                  case 'o':   
                     d = va_arg(ap, int);
                       itoa(d, buf, 8);
                       for (s = buf; *s; s++) 
                       {
                          DebugBuffer[DebugSize++]= *s;
                       }
                       Data++;
                       break;
                  case 'x':
                       d = va_arg(ap, int);
                       itoa(d, buf, 16);
                       for (s = buf; *s; s++) 
                       {
                          DebugBuffer[DebugSize++]= *s;
                       }
                       Data++;
                       break;
                  case 'd':                        //十进制
                       d = va_arg(ap, int);
                       itoa(d, buf, 10);
                       for (s = buf; *s; s++) 
                       {
                          DebugBuffer[DebugSize++]= *s;
                       }
                       Data++;
                       break;
                       
                 default:
                       Data++;
                       break;
            }                 
         } /* end of else if */
        else DebugBuffer[DebugSize++]= *Data++;
      }
      
      LOG_Send(DebugBuffer, DebugSize);
    
    }
    
    
    
    在应用层可以这么使用:
    LOG(LOG_INFO,"PIR = %d at %d:%d:%d\r\n",keycount,Hour,Minite,Second);  //


    上一篇:lyy
    下一篇:HP DeskJet 1110 series 喷墨打印机USB描述符