//////////////////////////////////////////////////
//Title    : pub_log.c
//Auhtor   : Liu Wei
//Desc     : public output log implementation
//Created  : 2007-05-21
//Revision :
//
//Revision :
//
//////////////////////////////////////////////////


#include "./include/pub_log.h"

void FLogMsg( u8 LogMask , const char *pFmt, ...)
{
	va_list ap;

    va_start(ap, pFmt);
    switch( LogMask )
    {
    case PIF_INFO:
    	break;
    case PIF_WARN:
    	break;
    case PIF_GERR:
    	break;
    case PIF_CERR:
    	break;
    case PIF_DBG:
    	break;
    case PIF_UNKN:
    	break;
    case PIF_DEFAULT:
    	break;
    case PIF_ALL:
    	break;
    default:
    	break;
    }
    printf(pFmt, ap);
    va_end(ap);
    return;
}

char *ByteBinToStr( char *pDst , const u8 *pSrc , int nByte )
{
	register int i , j , k = 0;
	
	for( i =0  ; i < nByte ; i++ )
    {
    	for( j = 7 ; j >= 0 ; j --)
    	{
    	   pDst[k++] = (pSrc[i] >>j  & 0x01) + '0';
        }
    }
    pDst[k++] = CNULL;
    return pDst;
}

char *TxtColor ( int nAttr, int nFg, int nBg, char *pStr )
{
    register int   nPos ;
    int     nLen = strlen(pStr);
    char    *p = strdup (pStr);
    
    nPos = 0;
    pStr[nPos++] = 27;
    pStr[nPos++] = '['; 
    pStr[nPos++] = nAttr + '0';  
    pStr[nPos++] = ';';
    pStr[nPos++] = 3 + '0';
    pStr[nPos++] = nFg + '0';
    pStr[nPos++] = ';';
    pStr[nPos++] = 4 + '0';
    pStr[nPos++] = nBg + '0';
    pStr[nPos++] = 'm';
    strcpy( pStr + nPos , p);
    nPos += nLen;
    pStr[nPos++] = 27;
    pStr[nPos++] = '[';
    pStr[nPos++] = RESET + '0';
    pStr[nPos++] = ';';
    pStr[nPos++] = 3 + '0';
    pStr[nPos++] = WHITE + '0';
    pStr[nPos++] = ';';
    pStr[nPos++] = 4 + '0';
    pStr[nPos++] = BLACK + '0';
    pStr[nPos++] = 'm';
    pStr[nPos]   = CNULL;
    
    free(p);
    return pStr;
}



int MsgToFmtLog ( const BYTE * pBcd, int nBcdLen, char *pStr, int nStrBuffSize )
{
    register  int i, len = 0;
    int     nChar;

    nChar = ( nBcdLen / 16 ) * 49;
    if( (nBcdLen %16) > 8 )
    {
    	nChar+=8*3 +1;
    	
    } 
    nChar += (nBcdLen %8)*3 +1 + 2 ; //add "\r\n" at end

    WxcAssert ( ( nChar < nStrBuffSize ), "sprint bcd string buffer flow" );

    for ( i = 0; i <= nBcdLen-1; i++ )
    {
    	pStr[len++] = HexToCh(pBcd[i] >> 4  );
    	pStr[len++] = HexToCh(pBcd[i] & 0x0F);
    	pStr[len++] = ' ';
        if ( 0 == ( (i+1) % 16 )  && i)
        {
        	pStr[len++] = '\n';
        }
        else if ( 0 == ( (i+1) % 8 ) )
        {
            pStr[len++] = ' ';
        }
    }
    if ( 0 != ( len % 49 ) )
    {
        pStr[len++] = ' ';
    }
    pStr[len++] = '\r';
    pStr[len++] = '\n';
    pStr[len++] = CNULL;
    return len;
}
