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

📄 coollog.cpp

📁 一种基于启发式算法(互信息熵)的粗糙集约简源代码
💻 CPP
字号:
#include "stdafx.h"
#include <windows.h> 
#include <io.h>
#include "CoolLog.h"

const char DIVIDER[10] = { 13, 10, '-', '-', '-', '-', '-', '-', 13, 10 };
const char CrLf[3]	   = { 13, 10, 0 };

const char MUTEX_NAME[]	  = "LogByJzh";	
CDebugPrintf debug;

/*-----------------------------------------------------------------------
 *  Function    : 
 *  Prototype   : CDebugPrintf::CDebugPrintf ()
 *  Description : 
 *  Parameters  :                   
 *  Return      : 
 *  History     : 
 */
CDebugPrintf::CDebugPrintf ()
{
	// 得到默认的日志文件名,即SetLogFileName的方法没有被调用
	TCHAR szExeFilePath[_MAX_PATH], szTemp[_MAX_PATH];
	GetModuleFileName(NULL, szExeFilePath, _MAX_PATH);
	
	// 得到并产生日志文件名
	_tcscpy(m_szShowConsolePath ,strrchr(szExeFilePath, '\\') + 1);// EXE的文件全名
	*(strrchr(m_szShowConsolePath, '.') + 1) = '\0';//EXE的文件名,不包括扩展名
	_tcscpy(szTemp, m_szShowConsolePath);//EXE的文件名,不包括扩展名
	*(strrchr(szTemp, '.')) = '\0';
	_tcscat(m_szShowConsolePath, _T("log"));//日志文件名,Test.log

	// 得到路径
	*(strrchr(szExeFilePath, '\\') + 1) = '\0';// 得到EXE文件的路径

	// 默认日志文件名为当前目录下的\\Log\\Exe文件名.log
	_tcscpy(m_szLogFileName, szExeFilePath);
	_tcscat(m_szLogFileName, _T("Log\\"));
	_tcscat(m_szLogFileName, m_szShowConsolePath);//日志文件的全路径c:\\test\log\test.log

	// 另外如果当前进程路径下存在: 文件名_SC文件夹时,则显示消息,否则不显示;
	_tcscpy(m_szShowConsolePath, szExeFilePath);
	_tcscat(m_szShowConsolePath, szTemp);
	_tcscat(m_szShowConsolePath, _T("_SC"));
	if (_access(m_szShowConsolePath, 0) == -1)
	{
		//文件夹不存在,不显示控制台窗口
		m_bUseConsole = false;
	}
	else
	{
		m_bUseConsole = true;
	}

	m_bStartLogFlag = false;
	m_hConWrite = NULL;

	InitializeCriticalSection( &m_hLock );
	
	// 释放控制台
	FreeConsole();
}

/*-----------------------------------------------------------------------
 *  Function    : 
 *  Prototype   : CDebugPrintf::CDebugPrintf ()
 *  Description : 
 *  Parameters  :                   
 *  Return      : 
 *  History     : 
 */
CDebugPrintf::~CDebugPrintf ()
{
	FreeConsole();
	CloseLog();
}

/*-----------------------------------------------------------------------
 *  Function    : 
 *  Prototype   : CDebugPrintf::Init()
 *  Description : 
 *  Parameters  :                   
 *  Return      : 
 *  History     : 
 */
bool CDebugPrintf::Init()
{
	DWORD x;
	
	m_hDebugMutex = OpenMutex( MUTEX_ALL_ACCESS, FALSE, MUTEX_NAME);  
	
	// We are the first application who wants to print to log file
	if ( m_hDebugMutex == 0 )
	{
		m_hDebugMutex = CreateMutex( 0, FALSE, MUTEX_NAME );

		// Save old log file (up to 2)
		char newFile1[255];
		char newFile2[255];
		
		lstrcpy( newFile1, m_szLogFileName );
		lstrcat( newFile1, "~" );

		lstrcpy( newFile2, newFile1 );
		lstrcat( newFile2, "~" );

		CopyFile( newFile1,     newFile2, FALSE );
		CopyFile( m_szLogFileName, newFile1, FALSE );

		// Write time stamp to log-file
		HANDLE hFile = CreateFile( m_szLogFileName, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );

		// DWORD dwTime = GetTickCount();
		// WriteFile( hFile, &dwTime, sizeof(dwTime),  &x, 0 );	
		// WriteFile( hFile, DIVIDER, sizeof(DIVIDER), &x, 0 );	
		CloseHandle( hFile );
	}

	m_hFile = CreateFile( m_szLogFileName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
	ReadFile( m_hFile, &m_dwStartTime, sizeof(m_dwStartTime), &x, 0 );	

	m_bStartLogFlag = true;
	
	// 如果需要显示控制台,则显示
	if (m_bUseConsole)
		ShowConsole();

	return true;
}

/*-----------------------------------------------------------------------
 *  Function    : 
 *  Prototype   : void CDebugPrintf::CloseLog ()
 *  Description : Closes log file and releases handles. 
 *  Parameters  :                   
 *  Return      : void
 *  History     : 
 */
void CDebugPrintf::CloseLog ()
{
	DeleteCriticalSection( &m_hLock );

	if (!m_bStartLogFlag)
		return;

	CloseHandle( m_hFile );

	ReleaseMutex( m_hDebugMutex );
	CloseHandle( m_hDebugMutex );
}

/*-----------------------------------------------------------------------
 *  Function    : 
 *  Prototype   : void CDebugPrintf::printf ( const char *fmt, ... )
 *  Description : Prints formatted string to log-file
 *  Parameters  : const char *fmt - format string
                  ...             - list of parameters
 *  Return      : void
 *  History     : 
 */
void CDebugPrintf::printf ( const char *fmt, ... )
{
	if (!m_bStartLogFlag)
		Init();

	EnterCriticalSection( &m_hLock );

	// Format string
	char buffer[1024*50];
	va_list arglist;
	va_start( arglist, fmt );
	wvsprintf( buffer, fmt, arglist );
	va_end(arglist);

	// Format time stamp
	DWORD x;
	char  buf[256];

	SYSTEMTIME time;

	GetLocalTime(&time);

	DWORD dwDiffTime = GetTickCount()-m_dwStartTime;
	
	wsprintf( buf, "%d.%03d %02d/%02d/%02d %02d:%02d:%02d  ", 
					dwDiffTime/1000, dwDiffTime%1000, 
					time.wMonth, time.wDay,    time.wYear,
					time.wHour,  time.wMinute, time.wSecond);

	// Write time stamp and formatted string to log-file
	SetFilePointer( m_hFile, 0, 0, FILE_END );
	WriteFile( m_hFile,	buf,    lstrlen(buf),    &x, 0 );	
	WriteFile( m_hFile,	buffer, lstrlen(buffer), &x, 0 );	
	WriteFile( m_hFile,	CrLf,   lstrlen(CrLf),   &x, 0 );	

	// 判断ShowConsolePath的文件夹是否存在
	if (!m_bUseConsole)
	{
		if (_access(m_szShowConsolePath, 0) != -1)
		{
			ShowConsole();
		}
	}
	else
	{
		if (_access(m_szShowConsolePath, 0) == -1)
		{
			HideConsole();
		}
	}

	if ( m_bUseConsole )
	{
		WriteFile( m_hConWrite,	buf,    lstrlen(buf),    &x, 0 );	
		WriteFile( m_hConWrite,	buffer, lstrlen(buffer), &x, 0 );	
		WriteFile( m_hConWrite,	CrLf,   lstrlen(CrLf),   &x, 0 );	
	}

	LeaveCriticalSection( &m_hLock );
}

/*-----------------------------------------------------------------------
 *  Function    : ShowConsole
 *  Prototype   : void CDebugPrintf::ShowConsole() 
 *  Description : Shows log-console
 *  Parameters  :                   
 *  Return      : void 
 *  History     : 
 */
void CDebugPrintf::ShowConsole() 
{
	if (!m_bStartLogFlag)
		Init();

	EnterCriticalSection( &m_hLock );
	
	m_bUseConsole = true;
	AllocConsole();
	
	m_hConWrite = GetStdHandle(STD_OUTPUT_HANDLE);

	// 设置控制台的标题和属性
	TCHAR l_szTemp[_MAX_PATH];
	_tcscpy(l_szTemp, _T("LogFilePath - "));
	_tcscat(l_szTemp, m_szLogFileName);
	SetConsoleTitle(l_szTemp);
	//SetConsoleTextAttribute(m_hConWrite,FOREGROUND_BLUE|BACKGROUND_INTENSITY);

	LeaveCriticalSection( &m_hLock );
}

/*-----------------------------------------------------------------------
 *  Function    : HideConsole
 *  Prototype   : void CDebugPrintf::HideConsole() 
 *  Description : Hides log-console
 *  Parameters  :                   
 *  Return      : void 
 *  History     : 
 */
void CDebugPrintf::HideConsole() 
{
	EnterCriticalSection( &m_hLock );
	
	FreeConsole();
	m_bUseConsole = false;
	
	LeaveCriticalSection( &m_hLock );
}

/*-----------------------------------------------------------------------
 *  Function    : ShowLastError
 *  Prototype   : void CDebugPrintf::ShowLastError() 
 *  Description : 
 *  Parameters  :                   
 *  Return      : void 
 *  History     : 
 */
void CDebugPrintf::ShowLastError() 
{
	EnterCriticalSection( &m_hLock );

	char* lpMsgBuf;

	FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,    
		NULL,
		GetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &lpMsgBuf,    
		0,    
		NULL );

	int len = lstrlen(lpMsgBuf)-1;

	if ( len>0 )
	{
		while ( len>=0 && (lpMsgBuf[len]==0x0d || lpMsgBuf[len]==0x0a) )
			len--;
		lpMsgBuf[len+1] = 0; 
		printf ( "GetLastError(): %s", (char*)lpMsgBuf );
		LocalFree( lpMsgBuf ); // Free the buffer.
	}

	LeaveCriticalSection( &m_hLock );
}

/*-----------------------------------------------------------------------
 *  Function    : SetLogFileName
 *  Prototype   : CDebugPrintf::SetLogFileName(char *szFilePath) 
 *  Description : 
 *  Parameters  : szFilePath - LogFile Path                  
 *  Return      : bool 
 *  History     : 
 */
bool CDebugPrintf::SetLogFileName(const char *szFilePath)
{
	if (_tcslen(szFilePath) < 4)
	{
		return false;
	}

	_tcscpy(m_szLogFileName, szFilePath);

	return true;
}

⌨️ 快捷键说明

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