-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cpp
89 lines (74 loc) · 2.28 KB
/
Log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This file is part of the msamoila distribution (https://github.com/msamoila).
// Copyright (c) 2011 Marius Samoila.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Marius Samoila, 2011
//
#include "stdafx.h"
#include "Log.h"
#include "Ini.h"
extern CIni ini;
CLog::CLog(LPCTSTR szFileName, BOOL bIsCsv)
{
m_hFile = INVALID_HANDLE_VALUE;
ATLASSERT(szFileName);
m_szFileName = szFileName;
m_bIsCsv = bIsCsv;
}
BOOL CLog::Write(LPCTSTR szFormat, ...)
{
BOOL bRet = FALSE;
DWORD dwWritten = 0;
long lMaxLen = _tcslen(szFormat) + 256;
LPTSTR szText = (LPTSTR)malloc(lMaxLen*sizeof(TCHAR));
if(szText == NULL)
return FALSE;
DWORD i, len;
SYSTEMTIME st;
GetLocalTime(&st);
i = GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &st, _T("dd'/'MMM'/'yyyy'"), szText, lMaxLen);
if(i--)
len = i;
if(m_bIsCsv)
{
len += _sntprintf(szText+len, lMaxLen-len, _T(","));
}
else
{
if(ini.m_bEnableLog == FALSE)
goto $end;
i = GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &st, _T(" HH':'mm':'ss'"), szText+len, lMaxLen-len);
if(i--)
{
len += i;
len += _sntprintf(szText+len, lMaxLen-len, _T(".%03d "), st.wMilliseconds);
}
}
va_list va;
va_start( va, szFormat);
len += _vsntprintf(szText+len, lMaxLen-len, szFormat, va);
va_end( va );
m_hFile = CreateFile(m_szFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(m_hFile == INVALID_HANDLE_VALUE)
goto $end;
SetFilePointer(m_hFile, 0, NULL, FILE_END);
WriteFile(m_hFile, szText, len*sizeof(TCHAR), &dwWritten, NULL);
if(dwWritten == len*sizeof(TCHAR))
bRet = TRUE;
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
$end:
free(szText);
return bRet;
}