Created
January 13, 2011 02:37
-
-
Save thinkhy/777307 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void WriteLog(CString strLog) | |
{ | |
EnterCriticalSection(g_cs); | |
static CString strFileName; | |
if (strFileName.IsEmpty()) | |
{ | |
strFileName.GetBuffer(MAX_PATH); | |
::GetModuleFileName(NULL, strFileName.GetBuffer(0), MAX_PATH); | |
::PathRemoveFileSpec(strFileName.GetBuffer(0)); | |
::PathAppend(strFileName.GetBuffer(0), L"Log.txt"); | |
} | |
CFile logFile; | |
logFile.Open(strFileName, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite); | |
if (logFile.GetLength() == 0) // First of all, Write UTF-16 Lead Bytes. | |
{ | |
const unsigned char LeadBytes[] = {0xFF, 0xFE}; | |
logFile.Write(LeadBytes, sizeof(LeadBytes)); | |
} | |
logFile.SeekToEnd(); | |
// Write current time | |
SYSTEMTIME systime ={0}; | |
GetLocalTime(&systime); | |
TCHAR szTime[128] = {0}; | |
wsprintf(szTime, L"\r\n%d-%d-%d %d:%d:%d ", systime.wYear, systime.wMonth, systime.wDay, | |
systime.wHour, systime.wMinute, systime.wSecond); | |
int nLen = wcslen(szTime); | |
DWORD dwNumberOfBytesToWrite = nLen *sizeof(TCHAR); | |
logFile.Write(szTime, dwNumberOfBytesToWrite); | |
logFile.Write(strLog.GetBuffer(0), sizeof(TCHAR) * strLog.GetLength()); | |
logFile.Flush(); | |
logFile.Close(); | |
LeaveCriticalSection(g_cs); | |
} |
EnterCriticalSection(&g_logcs);
static TCHAR szFileName[MAX_PATH] = L"";
if (wcscmp(szFileName, L"") == 0)
{
::GetModuleFileName(NULL, szFileName, MAX_PATH);
::PathRemoveFileSpec(szFileName);
::PathAppend(szFileName, L"Log");
::PathAppend(szFileName, L"Log.txt");
}
HANDLE hFile = ::CreateFile(szFileName, GENERIC_WRITE, 0,
NULL, OPEN_ALWAYS, 0, NULL );
DWORD nWrite = 0;
int nSize = 0;
if (hFile)
nSize = ::GetFileSize(hFile, NULL );
if (nSize == 0) // First of all, Write UTF-16 Lead Bytes.
{
const unsigned char LeadBytes[] = {0xFF, 0xFE};
WriteFile( hFile, LeadBytes, 2, &nWrite, NULL );
::FlushFileBuffers(hFile);
}
// SeekToEnd
LONG dwMoveH = 0;
DWORD dwMoveL = 0;
dwMoveL = ::SetFilePointer(hFile, dwMoveL, &dwMoveH, FILE_END);
// Write current time
SYSTEMTIME systime ={0};
GetLocalTime(&systime);
WCHAR szTime[128] = {0};
swprintf_s(szTime, 128, L"\r\n%d-%d-%d %d:%d:%d ", systime.wYear, systime.wMonth, systime.wDay,
systime.wHour, systime.wMinute, systime.wSecond);
DWORD dwNumberOfBytesToWrite = wcslen(szTime);
WriteFile( hFile, szTime, dwNumberOfBytesToWrite, &nWrite, NULL );
// Write Log
WriteFile( hFile, strLog.c_str(), strLog.size() * sizeof(WCHAR), &nWrite, NULL );
::FlushFileBuffers(hFile);
CloseHandle(hFile);
LeaveCriticalSection(&g_logcs);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use Win SDK without MFC:
void WriteLog(std::wstring strLog)
{
static TCHAR szFileName[MAX_PATH] = L"";
if (wcscmp(szFileName, L"") == 0)
{
::GetModuleFileName(NULL, szFileName, MAX_PATH);
::PathRemoveFileSpec(szFileName);
::PathAppend(szFileName, L"EPS_Log.csv");
}
}