Пример #1
0
// 写日志信息
long SysLog::Userlog(unsigned level, const char *fmt, ...)
{
	lock();//加锁是为了写文件不出现混乱

	// 调用BeginLog函数,打开日志文件
	int ret = BeginLog();
	if (ret < 0)
	{
		unlock();
		return ret;
	}
	try{
		va_list argptr;
		va_start(argptr, fmt);
#ifdef WIN32
		_vsnprintf((char*)&m_strBuf, MAX_LINE_LEN, fmt, argptr);
#else
		vsnprintf((char*)&m_strBuf, MAX_LINE_LEN, fmt, argptr);
#endif
		va_end(argptr);
	}
	catch(...)
	{
		unlock();
		return -2;
	}

	// 获取当前线程ID
#ifdef WIN32
	m_nID = GetCurrentThreadId();
#else
	m_nID = pthread_self();
#endif

	string _level;
	switch(level)
	{
	case LOG_INF:
		_level = "LOG_INF";
		break;
	case LOG_WAR:
		_level = "LOG_WAR";
		break;
	case LOG_ERR:
		_level = "LOG_ERR";
		break;
	default:
		_level = "LOG_INF";
	}
	long size = fprintf(m_pf, "%s [%lu] %s -> %s\n", 
		FormatTime(SysLog::GetSystemTime()).c_str(), m_nID, _level.c_str(), m_strBuf);

	fflush(m_pf);
	unlock();

	return size;
}
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;
}