-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDebugOutput.h
260 lines (235 loc) · 7.72 KB
/
DebugOutput.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// sktoolslib - common files for SK tools
// Copyright (C) 2012, 2015, 2017, 2020-2024 - Stefan Kueng
// 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; either version 2
// of the License, or (at your option) any later version.
// 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, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#pragma once
#include "Registry.h"
#include <cassert>
#include <io.h>
#include <fcntl.h>
// A little like a release build assert. Always evaluate expr.
// Does not abort in this variant.
#define APPVERIFY(expr) CTraceToOutputDebugString::Instance().Verify(expr, __FUNCTION__, __LINE__, (const char*)nullptr)
#define APPVERIFYM(expr, msg, ...) CTraceToOutputDebugString::Instance().Verify(expr, __FUNCTION__, __LINE__, msg, ##__VA_ARGS__)
class CTraceToOutputDebugString
{
public:
static CTraceToOutputDebugString& Instance()
{
if (m_pInstance == nullptr)
m_pInstance = new CTraceToOutputDebugString;
return *m_pInstance;
}
static bool Active()
{
return Instance().m_bActive;
}
bool SetLogFile(PCWSTR path)
{
auto handle = CreateFile(path, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (handle == INVALID_HANDLE_VALUE)
return false;
// Associates a C run-time file descriptor with a file HANDLE.
auto fd = _open_osfhandle(reinterpret_cast<intptr_t>(handle), _O_BINARY | _O_TEXT);
if (fd == -1)
{
CloseHandle(handle);
return false;
}
// Associates a stream with a C run-time file descriptor.
m_fi = _fdopen(fd, "w+");
if (m_fi != nullptr)
{
setvbuf(m_fi, nullptr, _IONBF, 0);
return true;
}
return false;
}
// Non Unicode output helper
void operator()(PCSTR pszFormat, ...) const
{
if (m_bActive)
{
va_list ptr;
va_start(ptr, pszFormat);
TraceV(pszFormat, ptr);
va_end(ptr);
}
}
// Unicode output helper
void operator()(PCWSTR pszFormat, ...) const
{
if (m_bActive)
{
va_list ptr;
va_start(ptr, pszFormat);
TraceV(pszFormat, ptr);
va_end(ptr);
}
}
void Verify(bool expr, const char* function, int line, const char* msg, ...) const
{
if (!expr)
{
OutputDebugStringA(function);
OutputDebugStringA(", line ");
OutputDebugStringA(std::to_string(line).c_str());
OutputDebugStringA(" : ");
if (m_fi)
{
fputs(function, m_fi);
fputs(", line ", m_fi);
fputs(std::to_string(line).c_str(), m_fi);
fputs(" : ", m_fi);
}
if (msg)
{
va_list ptr;
va_start(ptr, msg);
TraceV(msg, ptr);
va_end(ptr);
}
else
{
OutputDebugStringA("An unexpected error occurred");
if (m_fi)
fputs("An unexpected error occurred", m_fi);
}
OutputDebugStringA("\n");
if (m_fi)
fputs("\n", m_fi);
// Verification failures are bugs so draw attention to them while debugging.
assert(false);
}
}
void Verify(bool expr, const char* function, int line, const wchar_t* msg, ...) const
{
if (!expr)
{
OutputDebugStringA(function);
OutputDebugStringA(", line ");
OutputDebugStringA(std::to_string(line).c_str());
OutputDebugStringA(" : ");
if (m_fi)
{
fputs(function, m_fi);
fputs(", line ", m_fi);
fputs(std::to_string(line).c_str(), m_fi);
fputs(" : ", m_fi);
}
if (msg)
{
va_list ptr;
va_start(ptr, msg);
TraceV(msg, ptr);
va_end(ptr);
}
else
{
OutputDebugStringA("An unexpected error occurred");
if (m_fi)
fputs("An unexpected error occurred", m_fi);
}
OutputDebugStringA("\n");
if (m_fi)
fputs("\n", m_fi);
// Verification failures are bugs so draw attention to them while debugging.
assert(false);
}
}
private:
CTraceToOutputDebugString()
: m_fi(nullptr)
{
m_lastTick = GetTickCount64();
#ifdef _DEBUG
m_bActive = true;
#else
m_bActive = !!CRegStdDWORD(DEBUGOUTPUTREGPATH, FALSE);
#endif
}
~CTraceToOutputDebugString()
{
delete m_pInstance;
}
ULONGLONG m_lastTick;
bool m_bActive;
FILE* m_fi;
static CTraceToOutputDebugString* m_pInstance;
// Non Unicode output helper
void TraceV(PCSTR pszFormat, va_list args) const
{
// Format the output buffer
auto len = _vscprintf(pszFormat, args);
std::string szBuffer;
if (len > 0)
{
szBuffer.resize(len + 1);
_vsnprintf_s((char *const)szBuffer.c_str(), len+1, len, pszFormat, args);
OutputDebugStringA(szBuffer.c_str());
if (m_fi)
fputs(szBuffer.c_str(), m_fi);
}
}
// Unicode output helper
void TraceV(PCWSTR pszFormat, va_list args) const
{
auto len = _vscwprintf(pszFormat, args);
std::wstring szBuffer;
if (len > 0)
{
szBuffer.resize(len+1);
_vsnwprintf_s((wchar_t *const)szBuffer.c_str(), len+1, len, pszFormat, args);
OutputDebugStringW(szBuffer.c_str());
if (m_fi)
fputws(szBuffer.c_str(), m_fi);
}
}
bool IsActive()
{
#ifdef _DEBUG
return true;
#else
if (GetTickCount64() - m_lastTick > 10000)
{
m_lastTick = GetTickCount64();
m_bActive = !!CRegStdDWORD(DEBUGOUTPUTREGPATH, FALSE);
}
return m_bActive;
#endif
}
};
class ProfileTimer
{
public:
ProfileTimer(LPCWSTR text)
: info(text)
{
QueryPerformanceCounter(&startTime);
}
~ProfileTimer()
{
LARGE_INTEGER endTime;
QueryPerformanceCounter(&endTime);
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
LARGE_INTEGER milliseconds;
milliseconds.QuadPart = endTime.QuadPart - startTime.QuadPart;
milliseconds.QuadPart *= 1000;
milliseconds.QuadPart /= frequency.QuadPart;
CTraceToOutputDebugString::Instance()(L"%s : %lld ms\n", info.c_str(), milliseconds.QuadPart);
}
private:
LARGE_INTEGER startTime;
std::wstring info;
};