示例#1
0
void TLog::tracenb(int level, const unsigned char *buf,int len)
{
    if(CT_boolFALSE == CanTrace(level))
        return;

    if(m_nTraceMode == LOG_TO_NULL || len <= 0)
        return;

    LOCK();

    if(!m_FP)
    {
        UNLOCK();
        return;
    }
    if(m_nTotalCount > m_nMaxLines)
        Backup();

    if(!m_FP)
    {
        UNLOCK();
        return;
    }
    char msg[512]="", tmp[10];
    int i = 0;
    for(i=0; i<len && i<8096; i++ ) 
    {
        sprintf(tmp, "%02x ", buf[i]);
        strcat( msg, tmp);
        if( ((i+1) % 32 ) == 0  )
        {
            strcat( msg, "\n"  );
            fprintf( m_FP, "%s", msg);
            m_nTotalCount ++;
            strcpy(msg,"");
        }
    }
    if( i % 32   )
    {
        strcat( msg, "\n"  );
        fprintf( m_FP, "%s", msg);
    }

    if(len == 0)
        fprintf(m_FP, "\n");

    if(m_nFlushFlag)       
        fflush(m_FP);

    m_nTotalCount++;

    UNLOCK();
}
示例#2
0
void TLog::tracent(int level, const char *format,...)
{
    if(CT_boolFALSE == CanTrace(level))
        return;

    if( m_nTraceMode == LOG_TO_NULL)
        return;

    LOCK();

    if(!m_FP)
    {
        UNLOCK();
        return;
    }
    if(m_nTotalCount > m_nMaxLines)
        Backup();

    if(!m_FP)
    {
        UNLOCK();
        return;
    }
    va_list ap;
    va_start(ap,format);
    char buffer[1024] = {0};

#ifndef WIN32
    vsnprintf(buffer, 1023, format, ap);
#else
    _vsnprintf(buffer, 1023, format, ap);
#endif
    fprintf(m_FP, "%s", buffer);
    va_end(ap);

    if(m_nFlushFlag)
        fflush(m_FP);
    m_nTotalCount ++;

    UNLOCK();
}
long 
LOGCPP::CLog_T::Userlog(unsigned level,const char *fmt,...)
{
	if (!CanTrace(level)) 
		return 0;
	lock();//加锁是为了写文件出现混乱
	int ret = BeginLog(level);
	if( ret<0 )
	{
		unlock();
		return ret;	
	}
	try
	{
		va_list argptr;
		va_start(argptr, fmt);
#ifdef WIN32
		_vsnprintf((char*)&m_strBuf, TBUF_SIZE, fmt, argptr);
#else
		vsnprintf((char*)&m_strBuf, TBUF_SIZE, fmt, argptr);
#endif
		va_end(argptr);
	}
	catch (...)
	{
		unlock();
		return -2;
	}
	long size = fprintf(m_pf,"%s\n",m_strBuf);
#ifdef _DEBUG
	Flush();
#endif

/***********************/
	fflush(m_pf);
/************************/
	
	unlock();

	return size;
}
short 
LOGCPP::CLog_T::SetFile( const char *fileName )
{
	assert(fileName);
	
	if( m_LogOptions&FileByDay || m_LogOptions&DisplayScreen || m_LogFileName == fileName )
		return 0;
	m_LogFileName = fileName;
	
	if (!CanTrace(m_LogLevel)) 
		return 0;

	string tmp;
	tmp = AddPath(fileName);

	if( m_pf )
		fclose(m_pf);
	m_pf = fopen(tmp.c_str(),"a");
	if( !m_pf )
		return -1;
	return 1;
}