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); | |
} |
Author
thinkhy
commented
Feb 26, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment