Skip to content

Instantly share code, notes, and snippets.

@thinkhy
Created January 13, 2011 02:37
Show Gist options
  • Save thinkhy/777307 to your computer and use it in GitHub Desktop.
Save thinkhy/777307 to your computer and use it in GitHub Desktop.
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);
}
@thinkhy
Copy link
Author

thinkhy commented Feb 24, 2011

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");
}

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);
char szTime[128] = {0};
sprintf_s(szTime, 128, "\r\n%d-%d-%d %d:%d:%d,", systime.wYear, systime.wMonth, systime.wDay, 
    systime.wHour, systime.wMinute, systime.wSecond);
int nLen = strlen(szTime);
DWORD dwNumberOfBytesToWrite = nLen;
WriteFile( hFile, szTime, dwNumberOfBytesToWrite, &nWrite, NULL );

int nlen = strLog.size() * 2 + 2;
char* pTextStr = new char[nlen];
memset(pTextStr, 0, nlen);

BOOL bOutOfEncoding = FALSE;
WideCharToMultiByte(GetACP(), 0, 
    strLog.c_str(), strLog.size(), pTextStr, nlen , " ", &bOutOfEncoding);

WriteFile( hFile, pTextStr, nlen, &nWrite, NULL );  
::FlushFileBuffers(hFile);
CloseHandle(hFile);

delete pTextStr;

}

@thinkhy
Copy link
Author

thinkhy commented Feb 26, 2011

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