-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbg.cpp
66 lines (48 loc) · 1.48 KB
/
dbg.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
#include "dbg.h"
#include <Shlwapi.h>
namespace Debug {
OutputTypes output_type;
CRITICAL_SECTION CS = { 0 };
HANDLE dbg_file_handle = 0;
bool Init(OutputTypes type) {
output_type = type;
if (type == OutputTypes::kFile) {
InitializeCriticalSection(&CS);
dbg_file_handle = CreateFileW(L"C:\\ProgramData\\fastldr.log", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, 0);
if (dbg_file_handle == INVALID_HANDLE_VALUE) {
return FALSE;
}
WORD bom = 0xFEFF;
DWORD written = 0;
WriteFile(dbg_file_handle, &bom, sizeof(bom), &written, NULL);
}
return TRUE;
}
void OutToDbg(LPCWSTR message_text, va_list args) {
WCHAR dbg_message[1025] = { 0 };
wvsprintfW(dbg_message, message_text, args);
OutputDebugStringW(dbg_message);
}
void OutToFile(LPCWSTR message_text, va_list args) {
EnterCriticalSection(&CS);
WCHAR dbg_message[1025] = { 0 };
int char_count = wvsprintfW(dbg_message, message_text, args);
DWORD written = 0;
WriteFile(dbg_file_handle, dbg_message, char_count * 2, &written, nullptr);
WriteFile(dbg_file_handle, L"\r\n", 4, &written, nullptr);
LeaveCriticalSection(&CS);
}
void DebugMsg(LPCWSTR message_text, ...) {
va_list args = nullptr;
va_start(args, message_text);
switch (output_type) {
case OutputTypes::kDbgConsole:
OutToDbg(message_text, args);
case OutputTypes::kFile:
OutToFile(message_text, args);
default:
break;
}
va_end(args);
}
}