C++寫錯誤日誌函數

在編寫程序,有些問題藉助錯誤日誌更好跟蹤。

UNICODE代碼:

DWORD  WriteErrLog ( CString strError)
{
DWORD nWrite=0, bytesWritten;
HANDLE hFile;
    BOOL res;
    CString strErrorLog, strTime ,strLogName;

SYSTEMTIME dt;//錯誤信息 +日期
GetLocalTime(&dt);
strTime.Format("  %04d/%02d/%02d %02d:%02d:%02d\r\n",dt.wYear, dt.wMonth, dt.wDay, dt.wHour, dt.wMinute, dt.wSecond );
TCHAR tstrCurrentPath[MAX_PATH];
TCHAR g_pInfPath[MAX_PATH];
memset(tstrCurrentPath, 0, MAX_PATH);
memset(g_pInfPath, 0, MAX_PATH);
//Get
GetCurrentDirectory(MAX_PATH, tstrCurrentPath);
CString strPath;
strPath.Format(_T("%s"),tstrCurrentPath);
strcpy(g_pInfPath,tstrCurrentPath);
strcat(g_pInfPath,_T("\\錯誤日誌.ini"));
strLogName= g_pInfPath;
hFile = CreateFile( strLogName, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 ) ;
if( ERROR_ALREADY_EXISTS == GetLastError() )
{
DWORD lDistance =0;
DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_END);
if ( INVALID_SET_FILE_POINTER == dwPtr )
        { 
CloseHandle(hFile);
            return -1;
}


strErrorLog=strError+strTime;        
DWORD lengh =strErrorLog.GetLength();
res = WriteFile(hFile,strErrorLog,lengh*sizeof(WCHAR),&nWrite,NULL);

}
else if( 0x0 == GetLastError() )
{
//LPCVOID
BYTE   head[2];  
head[0]   =   0xff;   
head[1]   =   0xfe;   

//Unicode   格式文本,需要寫入文件頭兩個字節 0xff   0xfe   
WriteFile(hFile, (BYTE*)head, 2, &bytesWritten, NULL);       
//再寫入內容    
DWORD lDistance =2;
DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_BEGIN);
if ( INVALID_SET_FILE_POINTER == dwPtr )
        { 
CloseHandle(hFile);
            return -1;
}

strErrorLog=strError+strTime;        
DWORD lengh =strErrorLog.GetLength();
res = WriteFile(hFile,strErrorLog,lengh*sizeof(WCHAR),&nWrite,NULL);
}
CloseHandle(hFile);
return (DWORD)res;
}


多字節代碼

DWORD  WriteErrLog ( CString strError)
{
DWORD nWrite=0, bytesWritten;
HANDLE hFile;
    BOOL res;
    CString strErrorLog, strTime ,strLogName;

SYSTEMTIME dt;//錯誤信息 +日期
GetLocalTime(&dt);
strTime.Format("  %04d/%02d/%02d %02d:%02d:%02d\r\n",dt.wYear, dt.wMonth, dt.wDay, dt.wHour, dt.wMinute, dt.wSecond );
TCHAR tstrCurrentPath[MAX_PATH];
TCHAR g_pInfPath[MAX_PATH];
memset(tstrCurrentPath, 0, MAX_PATH);
memset(g_pInfPath, 0, MAX_PATH);
//Get
GetCurrentDirectory(MAX_PATH, tstrCurrentPath);
CString strPath;
strPath.Format(_T("%s"),tstrCurrentPath);
strcpy(g_pInfPath,tstrCurrentPath);
strcat(g_pInfPath,_T("\\錯誤日誌.ini"));
strLogName= g_pInfPath;
hFile = CreateFile( strLogName, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 ) ;
if( ERROR_ALREADY_EXISTS == GetLastError() )
{
DWORD lDistance =0;
DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_END);
if ( INVALID_SET_FILE_POINTER == dwPtr )
        { 
CloseHandle(hFile);
            return -1;
}


strErrorLog=strError+strTime;        
DWORD lengh =strErrorLog.GetLength();
res = WriteFile(hFile,strErrorLog,lengh*sizeof(CHAR),&nWrite,NULL);

}
else if( 0x0 == GetLastError() )

//再寫入內容    
DWORD lDistance =0;
DWORD dwPtr = SetFilePointer(hFile, lDistance, NULL, FILE_BEGIN);
if ( INVALID_SET_FILE_POINTER == dwPtr )
        { 
CloseHandle(hFile);
            return -1;
}

strErrorLog=strError+strTime;        
DWORD lengh =strErrorLog.GetLength();
res = WriteFile(hFile,strErrorLog,lengh*sizeof(CHAR),&nWrite,NULL);
}
CloseHandle(hFile);
return (DWORD)res;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章