CLog::CLog( void ) : m_hLog_Thread_Handle( NULL ) , m_nLog_Thread_Id( 0 ) , m_bRunLog( false ) , m_bLogSwitch( true ) , m_strLogBasePath( _T("Log\\") /*_T("E:\\Code\\My\\Tool\\Log\\")*/ ) { CreateLogDirectory(); // 开启日志线程 m_hLog_Thread_Handle = (HANDLE)_beginthreadex( NULL, 0, (unsigned(__stdcall*)(void*)) Thread_Log, 0, 0, &m_nLog_Thread_Id ); m_bRunLog = true; }
bool Logger::SetLogFileDirectory(const std::string& strFileDir, bool bDeleteOldDir) { std::string strDirTemp = strFileDir; if (strDirTemp.substr(strDirTemp.length() - 1, 1) == "\\") { strDirTemp = strDirTemp.substr(0, strDirTemp.length() - 1); } if (!CreateLogDirectory(strDirTemp)) { return false; } if (bDeleteOldDir) { Dispose(); DeleteLogDirectory(m_strFileDir); } m_strFileDir = strDirTemp; MakeLogFilePath(); return true; }
/* Log Functions */ void ArxDebug::StartLogSession() { if (CreateLogDirectory()) { unsigned int uiID = static_cast<unsigned int>(time(NULL)); std::ostringstream ossFileName ; ossFileName << "..\\Log\\Log__" << uiID << ".txt"; m_fsFile.open(ossFileName.str().c_str(), std::ios::out | std::ios::app); if (m_fsFile) { m_bOpenLogFile = true ; } else { m_bOpenLogFile = false ; MessageBoxA(NULL, "Log file cannot be create. May be you dont have the permission or enought space disk.", "Log File ", MB_OK | MB_ICONHAND | MB_SETFOREGROUND | MB_TASKMODAL); } } }
DWORD LogDataToDisk(TCHAR *szModuleFilePath, TCHAR* szData, BOOL bAlertData) { TCHAR szFullPathDirectory[MAX_PATH + 100] = {0}; TCHAR szFileName[MAX_PATH + 100] = {0}; TCHAR szLogPath[MAX_PATH + 100] = {0}; TCHAR szTempLogPath[MAX_PATH + 100] = {0}; TCHAR szFullLogPath[MAX_PATH + 100] = {0}; TCHAR szMutexName[MAX_PATH + 100] = {0}; TCHAR szLogFileMapName[MAX_PATH + 100] = {0}; TCHAR* lpTraverser = NULL; TCHAR* szFindChar = NULL; TCHAR* pszString = NULL; TCHAR* pszFinalError = NULL; TCHAR chString = NULL; HANDLE hFile = NULL; HANDLE hMutex = NULL; DWORD nResult = 0; DWORD dwNumOfBytesWritten = 0; long lRet = 0; int iCtr = 0; int iSubCtr = 0; int iLen = 0; BOOL bFirstCreate = FALSE; __try { __try { if((NULL == szData) || (NULL == szModuleFilePath)) { return 1; } iLen = _tcslen(szModuleFilePath); if(0 == iLen) { return 1; } _tcscat(szLogFileMapName,_T("FILE_MAP_")); _tcscat(szMutexName,_T("FILE_MUTEX_")); for(iCtr = 0; iCtr < iLen; ++iCtr) { pszString = _tcsninc(szModuleFilePath,iCtr); chString = _tcsnextc(pszString); if(_T('\\') == chString) { _tcscat(szLogFileMapName,_T("-")); _tcscat(szMutexName,_T("-")); } else { _tcsncat(szLogFileMapName,pszString,1); _tcsncat(szMutexName,pszString,1); } } //Create the Mutex and wait hMutex = CreateMutex(NULL,FALSE,szMutexName); if(NULL == hMutex) { return 1; } nResult = WaitForSingleObject(hMutex,(300 * 1000)); if(WAIT_OBJECT_0 != nResult) { ::ReleaseMutex(hMutex); CloseHandle(hMutex); hMutex = NULL; return 1; } _tcscpy(szFullLogPath,szModuleFilePath); //Remove the file name szFindChar = strrchr(szFullLogPath,'\\'); if(NULL != szFindChar) { _tcscpy(szFileName,szFindChar); *szFindChar = NULL; //++szFindChar; } _tcscpy(szFullPathDirectory,szFullLogPath); _tcscat(szFullPathDirectory,szFileName); if(0 == ::SetFileAttributes(szFullPathDirectory,FILE_ATTRIBUTE_NORMAL)) { CreateLogDirectory(szFullLogPath); bFirstCreate = TRUE; } //::SetFileAttributes(szFullPathDirectory,FILE_ATTRIBUTE_NORMAL); //Create or opens the existing log file hFile = CreateFile(szFullPathDirectory, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { return 1; } if(TRUE == bFirstCreate) { if(FALSE == bAlertData) { ::WriteFile(hFile, "DateTime\tApplication\tApp_Status\tMonitor\tCounter\tCounterStatus\r\n", lstrlen("DateTime\tApplication\tApp_Status\tMonitor\tCounter\tCounterStatus\r\n"), &dwNumOfBytesWritten, NULL); dwNumOfBytesWritten = 0; } } SetFilePointer(hFile, 0, NULL, FILE_END); ::WriteFile(hFile,szData,lstrlen(szData),&dwNumOfBytesWritten,NULL); CloseHandle(hFile); hFile = NULL; ::ReleaseMutex(hMutex); CloseHandle(hMutex); hMutex = NULL; return 0; } __except(EXCEPTION_EXECUTE_HANDLER) { //Its better to flush all the buffers //FlushFileBuffers(hFile); //Close the file handle if(NULL != hFile) { CloseHandle(hFile); hFile = NULL; } if(NULL != hMutex) { ::ReleaseMutex(hMutex); CloseHandle(hMutex); hMutex = NULL; } return 1; } } __finally { //Its better to flush all the buffers //FlushFileBuffers(hFile); //Close the file handle if(NULL != hFile) { CloseHandle(hFile); hFile = NULL; } if(NULL != hMutex) { ::ReleaseMutex(hMutex); CloseHandle(hMutex); hMutex = NULL; } } return 0; }
DWORD LogErrorToDisk(TCHAR *szModuleFilePath, TCHAR* szErrorString) { TCHAR szFullPathDirectory[MAX_PATH + 100] = {0}; TCHAR szFileName[MAX_PATH + 100] = {0}; TCHAR szLogPath[MAX_PATH + 100] = {0}; TCHAR szTempLogPath[MAX_PATH + 100] = {0}; TCHAR szFullLogPath[MAX_PATH + 100] = {0}; TCHAR szMutexName[MAX_PATH + 100] = {0}; TCHAR szBuffer[51] = {0}; TCHAR* szFindChar = NULL; TCHAR* pszString = NULL; TCHAR chString = NULL; HANDLE hFile = NULL; HANDLE hMutex = NULL; HANDLE hWaitHandle[2] = {0}; DWORD nResult = 0; DWORD dwFileSizeLow = 0; DWORD dwFileSizeHigh = 0; DWORD dwBufferSize = 0; DWORD dwSize = 0; DWORD dwNumOfBytesWritten = 0; long lRet = 0; int iCtr = 0; int iLen = 0; BOOL bln = FALSE; BOOL bMutexFail = FALSE; __try { __try { //First check for the ErrorString and the Module Path if((NULL == szErrorString) || (NULL == szModuleFilePath)) { return 1; } //Get the length of the module path iLen = lstrlen(szModuleFilePath); if(0 == iLen) { return 1; } //Replace the Slashes with the '-' for the mutex, since //its the restriction placed by the Mutex for(iCtr = 0; iCtr < iLen; ++iCtr) { if('\\' == szModuleFilePath[iCtr]) { szMutexName[iCtr] = '-'; } else { szMutexName[iCtr] = szModuleFilePath[iCtr]; } } //Create the Mutex and wait hMutex = CreateMutex(NULL,FALSE,szMutexName); if(NULL == hMutex) { return 1; } //Wait for the mutex atleast 5 Minutes nResult = WaitForSingleObject(hMutex,(300 * 1000)); if(WAIT_OBJECT_0 != nResult) { //Unable to get Mutex, return from here ::ReleaseMutex(hMutex); CloseHandle(hMutex); hMutex = NULL; return 1; } lstrcpy(szFullLogPath,szModuleFilePath); //Remove the file name szFindChar = strrchr(szFullLogPath,'\\'); if(NULL != szFindChar) { lstrcpy(szFileName,szFindChar); *szFindChar = NULL; } //Create the full path here lstrcpy(szFullPathDirectory,szFullLogPath); lstrcat(szFullPathDirectory,szFileName); if(0 == ::SetFileAttributes(szFullPathDirectory,FILE_ATTRIBUTE_NORMAL)) { CreateLogDirectory(szFullLogPath); } //Create respective directories and sub directories //CreateRecursiveDirectory(szFullLogPath); //Get the length of the error string dwBufferSize = lstrlen(szErrorString); //Set the file attributes to normal //::SetFileAttributes(szFullPathDirectory,FILE_ATTRIBUTE_NORMAL); //Create or opens the existing log file hFile = CreateFile(szFullPathDirectory, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if((hFile == INVALID_HANDLE_VALUE) || (hFile == NULL)) { //Unable to open file, return from her hFile = NULL; return 1; } //Get the log file size dwFileSizeLow = GetFileSize(hFile,&dwFileSizeHigh); if(dwFileSizeLow > (1024 * 1024)) { //If the log file size is more than 1 MB, need to delete the log file //Close all the previous file handles ::CloseHandle(hFile); hFile = NULL; //Set the file attributes to normal ::SetFileAttributes(szFullPathDirectory,FILE_ATTRIBUTE_NORMAL); //Delete the log file ::DeleteFile(szFullPathDirectory); //Create or opens the existing log file hFile = CreateFile(szFullPathDirectory, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if((hFile == INVALID_HANDLE_VALUE) || (hFile == NULL)) { //Unable to open file for writting, return from here hFile = NULL; return 1; } //Get the log file size dwNumOfBytesWritten = 0; dwFileSizeLow = 0; } if(0 != dwFileSizeLow) { //Set the file pointer to the last offset SetFilePointer(hFile, 0, NULL, FILE_END); } dwNumOfBytesWritten = 0; //Write the data to the file ::WriteFile(hFile,szErrorString,dwBufferSize,&dwNumOfBytesWritten,NULL); //Close the file handle CloseHandle(hFile); hFile = NULL; //Release the mutex now ::ReleaseMutex(hMutex); CloseHandle(hMutex); hMutex = NULL; return 0; } __except(EXCEPTION_EXECUTE_HANDLER) { //Close the file handle if(NULL != hFile) { CloseHandle(hFile); hFile = NULL; } //Release the mutex now if(NULL != hMutex) { ::ReleaseMutex(hMutex); CloseHandle(hMutex); hMutex = NULL; } return 1; } } __finally { //Close the file handle if(NULL != hFile) { CloseHandle(hFile); hFile = NULL; } //Release the nutex now if(NULL != hMutex) { ::ReleaseMutex(hMutex); CloseHandle(hMutex); hMutex = NULL; } } return 0; }