Skip to content

Commit 705f2da

Browse files
author
nitrocaster
committed
xrDebug: Refactor Fail/Error/Fatal/Backend functions.
1 parent 28c2d19 commit 705f2da

File tree

5 files changed

+206
-172
lines changed

5 files changed

+206
-172
lines changed

src/xrCore/xrDebug.cpp

Lines changed: 49 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -116,39 +116,41 @@ size_t xrDebug::BuildStackTrace(EXCEPTION_POINTERS* exPtrs, char *buffer, size_t
116116
return frameCount;
117117
}
118118

119-
void xrDebug::GatherInfo(const char *expression, const char *description, const char *arg0, const char *arg1,
120-
const char *file, int line, const char *function, char *assertionInfo)
119+
void xrDebug::GatherInfo(char *assertionInfo, const ErrorLocation &loc, const char *expr,
120+
const char *desc, const char *arg1, const char *arg2)
121121
{
122122
char *buffer = assertionInfo;
123-
bool extendedDesc = description && strchr(description, '\n');
123+
if (!expr)
124+
expr = "<no expression>";
125+
bool extendedDesc = desc && strchr(desc, '\n');
124126
char *prefix = "[error] ";
125127
buffer += sprintf(buffer, "\nFATAL ERROR\n\n");
126-
buffer += sprintf(buffer, "%sExpression : %s\n", prefix, expression);
127-
buffer += sprintf(buffer, "%sFunction : %s\n", prefix, function);
128-
buffer += sprintf(buffer, "%sFile : %s\n", prefix, file);
129-
buffer += sprintf(buffer, "%sLine : %d\n", prefix, line);
128+
buffer += sprintf(buffer, "%sExpression : %s\n", prefix, expr);
129+
buffer += sprintf(buffer, "%sFunction : %s\n", prefix, loc.Function);
130+
buffer += sprintf(buffer, "%sFile : %s\n", prefix, loc.File);
131+
buffer += sprintf(buffer, "%sLine : %d\n", prefix, loc.Line);
130132
if (extendedDesc)
131133
{
132-
buffer += sprintf(buffer, "\n%s\n", description);
133-
if (arg0)
134+
buffer += sprintf(buffer, "\n%s\n", desc);
135+
if (arg1)
134136
{
135-
buffer += sprintf(buffer, "%s\n", arg0);
136-
if (arg1)
137-
buffer += sprintf(buffer, "%s\n", arg1);
137+
buffer += sprintf(buffer, "%s\n", arg1);
138+
if (arg2)
139+
buffer += sprintf(buffer, "%s\n", arg2);
138140
}
139141
}
140142
else
141143
{
142-
buffer += sprintf(buffer, "%sDescription : %s\n", prefix, description);
143-
if (arg0)
144+
buffer += sprintf(buffer, "%sDescription : %s\n", prefix, desc);
145+
if (arg1)
144146
{
145-
if (arg1)
147+
if (arg2)
146148
{
147-
buffer += sprintf(buffer, "%sArgument 0 : %s\n", prefix, arg0);
148-
buffer += sprintf(buffer, "%sArgument 1 : %s\n", prefix, arg1);
149+
buffer += sprintf(buffer, "%sArgument 0 : %s\n", prefix, arg1);
150+
buffer += sprintf(buffer, "%sArgument 1 : %s\n", prefix, arg2);
149151
}
150152
else
151-
buffer += sprintf(buffer, "%sArguments : %s\n", prefix, arg0);
153+
buffer += sprintf(buffer, "%sArguments : %s\n", prefix, arg1);
152154
}
153155
}
154156
buffer += sprintf(buffer, "\n");
@@ -183,15 +185,23 @@ void xrDebug::GatherInfo(const char *expression, const char *description, const
183185
os_clipboard::copy_to_clipboard(assertionInfo);
184186
}
185187

186-
void xrDebug::DoExit(const std::string &message)
188+
void xrDebug::Fatal(const ErrorLocation &loc, const char *format, ...)
187189
{
188-
FlushLog();
189-
MessageBox(NULL, message.c_str(), "Error", MB_OK|MB_ICONERROR|MB_SYSTEMMODAL);
190-
TerminateProcess(GetCurrentProcess(), 1);
190+
string1024 desc;
191+
va_list args;
192+
va_start(args, format);
193+
vsnprintf(desc, sizeof(desc), format, args);
194+
va_end(args);
195+
bool ignoreAlways = true;
196+
Fail(ignoreAlways, loc, nullptr, "fatal error", desc);
191197
}
192198

193-
void xrDebug::Backend(const char *expression, const char *description, const char *arg0, const char *arg1,
194-
const char *file, int line, const char *function, bool &ignoreAlways)
199+
void xrDebug::Fail(bool &ignoreAlways, const ErrorLocation &loc, const char *expr,
200+
long hresult, const char *arg1, const char *arg2)
201+
{ Fail(ignoreAlways, loc, expr, xrDebug::ErrorToString(hresult), arg1, arg2); }
202+
203+
void xrDebug::Fail(bool &ignoreAlways, const ErrorLocation &loc, const char *expr,
204+
const char *desc, const char *arg1, const char *arg2)
195205
{
196206
#ifdef PROFILE_CRITICAL_SECTIONS
197207
static Lock lock(MUTEX_PROFILE_ID(xrDebug::Backend));
@@ -201,7 +211,7 @@ void xrDebug::Backend(const char *expression, const char *description, const cha
201211
lock.Enter();
202212
ErrorAfterDialog = true;
203213
string4096 assertionInfo;
204-
GatherInfo(expression, description, arg0, arg1, file, line, function, assertionInfo);
214+
GatherInfo(assertionInfo, loc, expr, desc, arg1, arg2);
205215
#ifdef USE_OWN_ERROR_MESSAGE_WINDOW
206216
strcat(assertionInfo,
207217
"\r\n"
@@ -252,6 +262,17 @@ void xrDebug::Backend(const char *expression, const char *description, const cha
252262
lock.Leave();
253263
}
254264

265+
void xrDebug::Fail(bool &ignoreAlways, const ErrorLocation &loc, const char *expr,
266+
const std::string &desc, const char *arg1, const char *arg2)
267+
{ Fail(ignoreAlways, loc, expr, desc.c_str(), arg1, arg2); }
268+
269+
void xrDebug::DoExit(const std::string &message)
270+
{
271+
FlushLog();
272+
MessageBox(NULL, message.c_str(), "Error", MB_OK|MB_ICONERROR|MB_SYSTEMMODAL);
273+
TerminateProcess(GetCurrentProcess(), 1);
274+
}
275+
255276
LPCSTR xrDebug::ErrorToString(long code)
256277
{
257278
const char *result = nullptr;
@@ -265,57 +286,6 @@ LPCSTR xrDebug::ErrorToString(long code)
265286
return result;
266287
}
267288

268-
void xrDebug::Error(long hr, const char *expr, const char *file, int line, const char *function, bool &ignoreAlways)
269-
{
270-
Backend(expr, ErrorToString(hr), nullptr, nullptr, file, line, function, ignoreAlways);
271-
}
272-
273-
void xrDebug::Error(long hr, const char *expr, const char *e2, const char *file, int line, const char *function,
274-
bool &ignoreAlways)
275-
{
276-
Backend(expr, ErrorToString(hr), e2, 0, file, line, function, ignoreAlways);
277-
}
278-
279-
void xrDebug::Fail(const char *e1, const char *file, int line, const char *function, bool &ignoreAlways)
280-
{
281-
Backend(e1, "assertion failed", 0, 0, file, line, function, ignoreAlways);
282-
}
283-
284-
void xrDebug::Fail(const char *e1, const std::string &e2, const char *file, int line, const char *function,
285-
bool &ignoreAlways)
286-
{
287-
Backend(e1, e2.c_str(), 0, 0, file, line, function, ignoreAlways);
288-
}
289-
290-
void xrDebug::Fail(const char *e1, const char *e2, const char *file, int line, const char *function,
291-
bool &ignoreAlways)
292-
{
293-
Backend(e1, e2, 0, 0, file, line, function, ignoreAlways);
294-
}
295-
296-
void xrDebug::Fail(const char *e1, const char *e2, const char *e3, const char *file, int line, const char *function,
297-
bool &ignoreAlways)
298-
{
299-
Backend(e1, e2, e3, 0, file, line, function, ignoreAlways);
300-
}
301-
302-
void xrDebug::Fail(const char *e1, const char *e2, const char *e3, const char *e4, const char *file, int line,
303-
const char *function, bool &ignoreAlways)
304-
{
305-
Backend(e1, e2, e3, e4, file, line, function, ignoreAlways);
306-
}
307-
308-
void xrDebug::Fatal(const char *file, int line, const char *function, const char *format, ...)
309-
{
310-
string1024 buffer;
311-
va_list p;
312-
va_start(p, format);
313-
vsprintf(buffer, format, p);
314-
va_end(p);
315-
bool ignoreAlways = true;
316-
Backend(nullptr, "fatal error", buffer, 0, file, line, function, ignoreAlways);
317-
}
318-
319289
int out_of_memory_handler(size_t size)
320290
{
321291
xrDebug::OutOfMemoryCallbackFunc cb = xrDebug::GetOutOfMemoryCallback();
@@ -513,8 +483,7 @@ void _terminate()
513483
if (strstr(GetCommandLine(), "-silent_error_mode"))
514484
exit(-1);
515485
string4096 assertionInfo;
516-
xrDebug::GatherInfo(nullptr, "Unexpected application termination", nullptr, nullptr,
517-
DEBUG_INFO, assertionInfo);
486+
xrDebug::GatherInfo(assertionInfo, DEBUG_INFO, nullptr, "Unexpected application termination");
518487
strcat(assertionInfo, "Press OK to abort execution\r\n");
519488
MessageBox(GetTopWindow(NULL), assertionInfo, "Fatal Error", MB_OK|MB_ICONERROR|MB_SYSTEMMODAL);
520489
exit(-1);
@@ -524,7 +493,7 @@ void _terminate()
524493
static void handler_base(const char *reason)
525494
{
526495
bool ignoreAlways = false;
527-
xrDebug::Backend(nullptr, reason, nullptr, nullptr, DEBUG_INFO, ignoreAlways);
496+
xrDebug::Fail(ignoreAlways, DEBUG_INFO, nullptr, reason, nullptr, nullptr);
528497
}
529498

530499
static void invalid_parameter_handler(const wchar_t *expression, const wchar_t *function,
@@ -550,7 +519,7 @@ static void invalid_parameter_handler(const wchar_t *expression, const wchar_t *
550519
line = __LINE__;
551520
xr_strcpy(mbFile, __FILE__);
552521
}
553-
xrDebug::Backend(mbExpression, "invalid parameter", nullptr, nullptr, mbFile, line, mbFunction, ignoreAlways);
522+
xrDebug::Fail(ignoreAlways, {mbFile, int(line), mbFunction}, mbExpression, "invalid parameter");
554523
}
555524

556525
static void pure_call_handler()

src/xrCore/xrDebug.h

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@ struct StackTraceInfo
1111
char *operator[](size_t i) { return Frames+i*LineCapacity; }
1212
};
1313

14+
class ErrorLocation
15+
{
16+
public:
17+
const char *File = nullptr;
18+
int Line = -1;
19+
const char *Function = nullptr;
20+
21+
ErrorLocation(const char *file, int line, const char *function)
22+
{
23+
File = file;
24+
Line = line;
25+
Function = function;
26+
}
27+
28+
ErrorLocation &operator=(const ErrorLocation &rhs)
29+
{
30+
File = rhs.File;
31+
Line = rhs.Line;
32+
Function = rhs.Function;
33+
return *this;
34+
}
35+
};
36+
1437
class XRCORE_API xrDebug
1538
{
1639
public:
@@ -25,12 +48,12 @@ class XRCORE_API xrDebug
2548
static CrashHandler OnCrash;
2649
static DialogHandler OnDialog;
2750
static string_path BugReportFile;
28-
static bool ErrorAfterDialog;
51+
static bool ErrorAfterDialog;
2952
static StackTraceInfo StackTrace;
3053

3154
public:
3255
xrDebug() = delete;
33-
static void Initialize(const bool &dedicated);
56+
static void Initialize(const bool &dedicated);
3457
static void Destroy();
3558
static void OnThreadSpawn();
3659
static OutOfMemoryCallbackFunc GetOutOfMemoryCallback() { return OutOfMemoryCallback; }
@@ -43,24 +66,15 @@ class XRCORE_API xrDebug
4366
static void SetBugReportFile(const char *fileName);
4467
static void LogStackTrace(const char *header);
4568
static size_t BuildStackTrace(char *buffer, size_t capacity, size_t lineCapacity);
46-
static void GatherInfo(const char *expression, const char *description, const char *arg0, const char *arg1,
47-
const char *file, int line, const char *function, char *assertionInfo);
48-
static void Fail(const char *e1, const char *file, int line, const char *function, bool &ignoreAlways);
49-
static void Fail(const char *e1, const std::string &e2, const char *file, int line, const char *function,
50-
bool &ignoreAlways);
51-
static void Fail(const char *e1, const char *e2, const char *file, int line, const char *function,
52-
bool &ignoreAlways);
53-
static void Fail(const char *e1, const char *e2, const char *e3, const char *file, int line, const char *function,
54-
bool &ignoreAlways);
55-
static void Fail(const char *e1, const char *e2, const char *e3, const char *e4, const char *file, int line,
56-
const char *function, bool &ignoreAlways);
57-
static void Error(long code, const char *e1, const char *file, int line, const char *function,
58-
bool &ignoreAlways);
59-
static void Error(long code, const char *e1, const char *e2, const char *file, int line, const char *function,
60-
bool &ignoreAlways);
61-
static void Fatal(const char *file, int line, const char *function, const char *format, ...);
62-
static void Backend(const char *reason, const char *expression, const char *arg0, const char *arg1,
63-
const char *file, int line, const char *function, bool &ignoreAlways);
69+
static void GatherInfo(char *assertionInfo, const ErrorLocation &loc, const char *expr,
70+
const char *desc, const char *arg1 = nullptr, const char *arg2 = nullptr);
71+
static void Fatal(const ErrorLocation &loc, const char *format, ...);
72+
static void Fail(bool &ignoreAlways, const ErrorLocation &loc, const char *expr,
73+
long hresult, const char *arg1 = nullptr, const char *arg2 = nullptr);
74+
static void Fail(bool &ignoreAlways, const ErrorLocation &loc, const char *expr,
75+
const char *desc = "assertion failed", const char *arg1 = nullptr, const char *arg2 = nullptr);
76+
static void Fail(bool &ignoreAlways, const ErrorLocation &loc, const char *expr,
77+
const std::string &desc, const char *arg1 = nullptr, const char *arg2 = nullptr);
6478
static void DoExit(const std::string &message);
6579

6680
private:
@@ -79,7 +93,7 @@ inline std::string make_string(const char *format, ...)
7993
va_start(args, format);
8094
string4096 temp;
8195
vsprintf(temp, format, args);
82-
return std::string(temp);
96+
return temp;
8397
}
8498

8599
#include "xrDebug_macros.h"

0 commit comments

Comments
 (0)