-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwlog.cpp
90 lines (71 loc) · 1.53 KB
/
wlog.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
90
#include "stdafx.h"
_NT_BEGIN
#include "wlog.h"
void WLog::operator >> (HWND hwnd)
{
HLOCAL hMem = (HLOCAL)SendMessage(hwnd, EM_GETHANDLE, 0, 0);
SendMessage(hwnd, EM_SETHANDLE, (WPARAM)_BaseAddress, 0);
_BaseAddress = 0;
LocalFree(hMem);
}
ULONG WLog::Init(SIZE_T RegionSize)
{
if (PVOID BaseAddress = LocalAlloc(0, RegionSize))
{
_RegionSize = (ULONG)RegionSize, _Ptr = 0, _BaseAddress = BaseAddress;
*(WCHAR*)BaseAddress = 0;
return NOERROR;
}
return GetLastError();
}
WLog::~WLog()
{
if (_BaseAddress)
{
LocalFree(_BaseAddress);
}
}
WLog& WLog::operator ()(PCWSTR format, ...)
{
va_list args;
va_start(args, format);
int len = _vsnwprintf_s(_buf(), _cch(), _TRUNCATE, format, args);
if (0 < len)
{
_Ptr += len * sizeof(WCHAR);
}
va_end(args);
return *this;
}
WLog& WLog::operator << (PCWSTR str)
{
if (!wcscpy_s(_buf(), _cch(), str))
{
_Ptr += (ULONG)wcslen(str) * sizeof(WCHAR);
}
return *this;
}
WLog& WLog::operator[](HRESULT dwError)
{
LPCVOID lpSource = 0;
ULONG dwFlags = FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
if (dwError & FACILITY_NT_BIT)
{
dwError &= ~FACILITY_NT_BIT;
__nt:
dwFlags = FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_IGNORE_INSERTS;
static HMODULE ghnt;
if (!ghnt && !(ghnt = GetModuleHandle(L"ntdll"))) return *this;
lpSource = ghnt;
}
if (dwFlags = FormatMessageW(dwFlags, lpSource, dwError, 0, _buf(), _cch(), 0))
{
_Ptr += dwFlags * sizeof(WCHAR);
}
else if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
{
goto __nt;
}
return *this;
}
_NT_END