⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logfile.h

📁 SMAL是short message abstract library的缩写,是由风起水流软件工作室(www.zealware.com)开发的一个支持短信网关系统开发的C++底层抽象接口库
💻 H
字号:
#ifndef _ATA_LOGFILE_
#define _ATA_LOGFILE_

#include	<afxmt.h>

/////////////////////////////////////////////////////////////////
//features:	not depends on MFC ore ATL.
//			file name could use absolute path or just the name, in which case the 
//			file will be created at the same place with the binary module, no concern 
//			with curret directory, which always bring me truble.
//			every log line has a time stamp attached, with million seconds.
//			uses printf like format to write log lines
//			uses a preprocessor definition _DEBUG_LOG to switch if write log file
//			multi thread safe, finally added:)

////////////////////////////////////////////////////
//	CLogFile, a debug log file wrapper
class CLogFile
{
public:
	//////////////////////////////
	//	Constructor, open the logfile
	CLogFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 1024*1024*10)
	{
		InitializeCriticalSection(&m_cs);
		
		m_lTruncate = lTruncate;
		TCHAR	szFile[MAX_PATH+1];
		
		if (_tcslen(strFile)>3 && strFile[1]!=':')	//no absolute path designated
		{
			::GetModuleFileName(NULL, szFile, MAX_PATH);
			long llength = (long)_tcslen(szFile);
			TCHAR*	pcat = szFile+(llength - 1);	//point to the last char
			while (llength--)
			{
				pcat--;
				if (*pcat == '\\')
					break;
			}
			if (*pcat == '\\')
			{
				pcat++;
				_tcscpy(pcat, strFile);
			}
			else	//something wrong, use the original filename, ignore path problem
				_tcscpy(szFile, strFile);
		}
		else
			_tcscpy(szFile, strFile);
		
		m_pLogFile = fopen(szFile, bAppend ? "a" : "w");

		setvbuf( m_pLogFile, "", _IONBF, 0);
	}
	
	/////////////////////////////////
	//	Destructor, close if logfile if opened
	~CLogFile()
	{
		if (m_pLogFile)
		{
			fputs("\n===============Finish Loging ZEALWARE SMG V4.00================\n\n", m_pLogFile);
			fclose(m_pLogFile);
			//Modified by Zhou De Qiang for creating new file exception 
			//Date:2003-07-11
			m_pLogFile	=	NULL;
		}

		DeleteCriticalSection(&m_cs);
	}
	
	//Modified by Zhou De Qiang for creating new file exception 
	//Date:2003-07-22
	void ChangeFile(LPCTSTR strFile,bool bAppend = TRUE)
	{
		::EnterCriticalSection(&m_cs);
		
		TCHAR	szFile[MAX_PATH+1];
		if (_tcslen(strFile)>3 && strFile[1]!=':')	//no absolute path designated
		{
			::GetModuleFileName(NULL, szFile, MAX_PATH);
			long llength = (long)_tcslen(szFile);
			TCHAR*	pcat = szFile+(llength - 1);	//point to the last char
			while (llength--)
			{
				pcat--;
				if (*pcat == '\\')
					break;
			}
			if (*pcat == '\\')
			{
				pcat++;
				_tcscpy(pcat, strFile);
			}
			else	//something wrong, use the original filename, ignore path problem
				_tcscpy(szFile, strFile);
		}
		else
			_tcscpy(szFile, strFile);
		
		if(m_pLogFile)
		{
			fputs("\n===============Finish Loging ZEALWARE SMG V4.00================\n\n", m_pLogFile);
			fclose(m_pLogFile);
			//Modified by Zhou De Qiang for creating new file exception 
			//Date:2003-07-11
			m_pLogFile	=	NULL;
		}
		
		m_pLogFile = fopen(szFile, bAppend ? "a" : "w");

		setvbuf( m_pLogFile, "", _IONBF, 0);
		
		::LeaveCriticalSection(&m_cs);
	}
	
	/////////////////////////////////
	//	Write log info into the logfile, with printf like parameters support
	void Write(LPCTSTR pszFormat, ...)
	{

		::EnterCriticalSection(&m_cs);
		
		if (!m_pLogFile)
		{
			LeaveCriticalSection(&m_cs);
			return;
		}

		try{

			//write the formated log string to szLog
			TCHAR	szLog[1024];
			va_list argList;
			va_start( argList, pszFormat );
			vsprintf( szLog, pszFormat, argList );
			va_end( argList );
			
			//Trancate if the file grow too large
			long	lLength = ftell(m_pLogFile);
			if (lLength > m_lTruncate)
				rewind(m_pLogFile);
			
			//Get current time
			SYSTEMTIME	time;
			::GetLocalTime(&time);
			TCHAR	szLine[1024];
			
			sprintf(szLine, "%02d:%02d:%02d:%03d \t%s\n", 
				time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
				szLog);
			
			fputs(szLine, m_pLogFile);
		}
		catch(...){
			LeaveCriticalSection(&m_cs);
			return;
		}

		LeaveCriticalSection(&m_cs);
	}
	
	void WriteEx(LPCTSTR pszFormat, ...)
	{
		::EnterCriticalSection(&m_cs);
		
		if (!m_pLogFile)
		{
			LeaveCriticalSection(&m_cs);
			return;
		}
		

		try{

			//write the formated log string to szLog
			TCHAR	szLog[1024*50];
			va_list argList;
			va_start( argList, pszFormat );
			vsprintf( szLog, pszFormat, argList );
			va_end( argList );
			
			//Trancate if the file grow too large
			long	lLength = ftell(m_pLogFile);
			if (lLength > m_lTruncate)
				rewind(m_pLogFile);
			
			//Get current time
			SYSTEMTIME	time;
			::GetLocalTime(&time);
			TCHAR	szLine[1024*50];
			
			sprintf(szLine, "%02d:%02d:%02d:%03d \t%s\n", 
				time.wHour, time.wMinute, time.wSecond, time.wMilliseconds,
				szLog);
			
			fputs(szLine, m_pLogFile);
		}
		catch(...){
			LeaveCriticalSection(&m_cs);
			return;
		}

		LeaveCriticalSection(&m_cs);
	}
	
private:
	FILE*	m_pLogFile;
	long	m_lTruncate;
	CRITICAL_SECTION	m_cs;
};

#endif //_ATA_LOGFILE_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -