forked from kugelrund/LolSceneSwitch
/
Log.cpp
127 lines (103 loc) · 1.99 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "Log.h"
#include <fstream>
#include <sstream>
#ifdef UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif
std::ofstream ofs;
void InitLog(_In_ TCHAR const * pluginDataPath)
{
tstring logPath(pluginDataPath);
logPath.append(TEXT("\\LolSceneSwitch.log"));
ofs.open(logPath, std::ofstream::out | std::ofstream::trunc);
}
void CloseLog()
{
ofs.close();
}
void Log(_In_ char const * text)
{
if (!ofs.bad())
{
ofs << text << std::endl;
}
}
void Log(_In_ char const * text, _In_opt_ char const * value)
{
if (!ofs.bad())
{
if (value != nullptr)
{
ofs << text << " '" << value << "'" << std::endl;
}
else
{
ofs << text << " 'NULL'" << std::endl;
}
}
}
void Log(_In_ char const * text, _In_opt_ WCHAR const * value)
{
if (!ofs.bad())
{
if (value != nullptr)
{
size_t length;
char converted[256];
wcstombs_s(&length, converted, value, 256);
ofs << text << " '" << converted << "'" << std::endl;
}
else
{
ofs << text << " 'NULL'" << std::endl;
}
}
}
void Log(_In_ char const * text, long long value)
{
Log(text, value, false);
}
void Log(_In_ char const * text, long long value, bool hex)
{
if (hex)
{
if (!ofs.bad())
{
std::stringstream stream;
stream << std::hex << value;
ofs << text << " '" << stream.str() << "'" << std::endl;
}
}
else
{
ofs << text << " '" << value << "'" << std::endl;
}
}
void LogLastError()
{
if (!ofs.bad())
{
DWORD eNum = GetLastError();
TCHAR sysMsg[256];
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, eNum,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), sysMsg, 256, NULL) == 0)
{
Log("ERROR | LogLastError | Couldn't format error message with number", eNum, true);
return;
}
// find end of string
TCHAR *p = sysMsg;
while ((*p > 31) || (*p == 9))
{
++p;
}
// remove unused end
do
{
*p-- = 0;
} while ((p >= sysMsg) && ((*p == '.') || (*p < 33)));
Log(" | GetLastError |", sysMsg);
}
}