Skip to content

Commit 5036655

Browse files
Platform debug: added option to control whether to break on error
1 parent e81c3be commit 5036655

File tree

8 files changed

+150
-80
lines changed

8 files changed

+150
-80
lines changed

Platforms/Android/src/AndroidDebug.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -36,11 +36,20 @@ namespace Diligent
3636

3737
void AndroidDebug::AssertionFailed(const Char* Message, const char* Function, const char* File, int Line)
3838
{
39-
auto AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
40-
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
41-
42-
raise(SIGTRAP);
43-
};
39+
String AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
40+
if (DebugMessageCallback)
41+
{
42+
DebugMessageCallback(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
43+
}
44+
else
45+
{
46+
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
47+
if (GetBreakOnError())
48+
{
49+
raise(SIGTRAP);
50+
}
51+
}
52+
}
4453

4554
void AndroidDebug::OutputDebugMessage(DEBUG_MESSAGE_SEVERITY Severity,
4655
const Char* Message,

Platforms/Apple/src/AppleDebug.mm

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,20 @@
3636

3737
void AppleDebug::AssertionFailed(const Char *Message, const char *Function, const char *File, int Line)
3838
{
39-
auto AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
40-
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
41-
42-
raise( SIGTRAP );
43-
};
39+
String AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
40+
if (DebugMessageCallback)
41+
{
42+
DebugMessageCallback(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
43+
}
44+
else
45+
{
46+
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
47+
if (GetBreakOnError())
48+
{
49+
raise(SIGTRAP);
50+
}
51+
}
52+
}
4453

4554
bool AppleDebug::ColoredTextSupported()
4655
{

Platforms/Basic/interface/BasicPlatformDebug.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -75,6 +75,9 @@ struct BasicPlatformDebug
7575
{
7676
return true;
7777
}
78+
79+
static void SetBreakOnError(bool BreakOnError);
80+
static bool GetBreakOnError();
7881
};
7982

8083
// Forward declarations of platform-specific debug functions

Platforms/Basic/src/BasicPlatformDebug.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,6 +29,7 @@
2929
#include "FormatString.hpp"
3030
#include "BasicFileSystem.hpp"
3131
#include <iostream>
32+
#include <atomic>
3233

3334
namespace Diligent
3435
{
@@ -125,4 +126,16 @@ const char* BasicPlatformDebug::TextColorToTextColorCode(DEBUG_MESSAGE_SEVERITY
125126
}
126127
}
127128

129+
static std::atomic_bool g_BreakOnError{true};
130+
131+
void BasicPlatformDebug::SetBreakOnError(bool BreakOnError)
132+
{
133+
g_BreakOnError.store(BreakOnError);
134+
}
135+
136+
bool BasicPlatformDebug::GetBreakOnError()
137+
{
138+
return g_BreakOnError.load();
139+
}
140+
128141
} // namespace Diligent

Platforms/Emscripten/src/EmscriptenDebug.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,11 +35,20 @@ namespace Diligent
3535

3636
void EmscriptenDebug::AssertionFailed(const Char* Message, const char* Function, const char* File, int Line)
3737
{
38-
auto AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
39-
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
40-
41-
raise(SIGTRAP);
42-
};
38+
String AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
39+
if (DebugMessageCallback)
40+
{
41+
DebugMessageCallback(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
42+
}
43+
else
44+
{
45+
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
46+
if (GetBreakOnError())
47+
{
48+
raise(SIGTRAP);
49+
}
50+
}
51+
}
4352

4453
void EmscriptenDebug::OutputDebugMessage(DEBUG_MESSAGE_SEVERITY Severity,
4554
const Char* Message,

Platforms/Linux/src/LinuxDebug.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -36,10 +36,19 @@ namespace Diligent
3636

3737
void LinuxDebug::AssertionFailed(const Char* Message, const char* Function, const char* File, int Line)
3838
{
39-
auto AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
40-
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
41-
42-
raise(SIGTRAP);
39+
String AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
40+
if (DebugMessageCallback)
41+
{
42+
DebugMessageCallback(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
43+
}
44+
else
45+
{
46+
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
47+
if (GetBreakOnError())
48+
{
49+
raise(SIGTRAP);
50+
}
51+
}
4352
}
4453

4554

Platforms/UWP/src/UWPDebug.cpp

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -38,38 +38,47 @@ namespace Diligent
3838

3939
void WindowsStoreDebug::AssertionFailed(const Char* Message, const char* Function, const char* File, int Line)
4040
{
41-
auto AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
42-
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
41+
String AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
42+
if (DebugMessageCallback)
43+
{
44+
DebugMessageCallback(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
45+
}
46+
else
47+
{
48+
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
49+
if (GetBreakOnError())
50+
{
51+
__debugbreak();
52+
//int nCode = MessageBoxA(NULL,
53+
// FullMsg.c_str(),
54+
// "Runtime assertion failed",
55+
// MB_TASKMODAL|MB_ICONHAND|MB_ABORTRETRYIGNORE|MB_SETFOREGROUND);
4356

44-
__debugbreak();
45-
//int nCode = MessageBoxA(NULL,
46-
// FullMsg.c_str(),
47-
// "Runtime assertion failed",
48-
// MB_TASKMODAL|MB_ICONHAND|MB_ABORTRETRYIGNORE|MB_SETFOREGROUND);
57+
//// Abort: abort the program
58+
//if (nCode == IDABORT)
59+
//{
60+
// // raise abort signal
61+
// raise(SIGABRT);
4962

50-
//// Abort: abort the program
51-
//if (nCode == IDABORT)
52-
//{
53-
// // raise abort signal
54-
// raise(SIGABRT);
63+
// // We usually won't get here, but it's possible that
64+
// // SIGABRT was ignored. So exit the program anyway.
65+
// exit(3);
66+
//}
5567

56-
// // We usually won't get here, but it's possible that
57-
// // SIGABRT was ignored. So exit the program anyway.
58-
// exit(3);
59-
//}
68+
//// Retry: call the debugger
69+
//if (nCode == IDRETRY)
70+
//{
71+
// DebugBreak();
72+
// /* return to user code */
73+
// return;
74+
//}
6075

61-
//// Retry: call the debugger
62-
//if (nCode == IDRETRY)
63-
//{
64-
// DebugBreak();
65-
// /* return to user code */
66-
// return;
67-
//}
68-
69-
//// Ignore: continue execution
70-
//if (nCode == IDIGNORE)
71-
// return;
72-
};
76+
//// Ignore: continue execution
77+
//if (nCode == IDIGNORE)
78+
// return;
79+
}
80+
}
81+
}
7382

7483
void WindowsStoreDebug::OutputDebugMessage(DEBUG_MESSAGE_SEVERITY Severity,
7584
const Char* Message,

Platforms/Win32/src/Win32Debug.cpp

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -71,37 +71,46 @@ class ConsoleSetUpHelper
7171

7272
void WindowsDebug::AssertionFailed(const Char* Message, const char* Function, const char* File, int Line)
7373
{
74-
auto AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
75-
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
74+
String AssertionFailedMessage = FormatAssertionFailedMessage(Message, Function, File, Line);
75+
if (DebugMessageCallback)
76+
{
77+
DebugMessageCallback(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
78+
}
79+
else
80+
{
81+
OutputDebugMessage(DEBUG_MESSAGE_SEVERITY_ERROR, AssertionFailedMessage.c_str(), nullptr, nullptr, 0);
82+
if (GetBreakOnError())
83+
{
84+
int nCode = MessageBoxA(NULL,
85+
AssertionFailedMessage.c_str(),
86+
"Runtime assertion failed",
87+
MB_TASKMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SETFOREGROUND);
7688

77-
int nCode = MessageBoxA(NULL,
78-
AssertionFailedMessage.c_str(),
79-
"Runtime assertion failed",
80-
MB_TASKMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SETFOREGROUND);
89+
// Abort: abort the program
90+
if (nCode == IDABORT)
91+
{
92+
// raise abort signal
93+
raise(SIGABRT);
8194

82-
// Abort: abort the program
83-
if (nCode == IDABORT)
84-
{
85-
// raise abort signal
86-
raise(SIGABRT);
95+
// We usually won't get here, but it's possible that
96+
// SIGABRT was ignored. So exit the program anyway.
97+
exit(3);
98+
}
8799

88-
// We usually won't get here, but it's possible that
89-
// SIGABRT was ignored. So exit the program anyway.
90-
exit(3);
91-
}
100+
// Retry: call the debugger
101+
if (nCode == IDRETRY)
102+
{
103+
DebugBreak();
104+
// return to user code
105+
return;
106+
}
92107

93-
// Retry: call the debugger
94-
if (nCode == IDRETRY)
95-
{
96-
DebugBreak();
97-
// return to user code
98-
return;
108+
// Ignore: continue execution
109+
if (nCode == IDIGNORE)
110+
return;
111+
}
99112
}
100-
101-
// Ignore: continue execution
102-
if (nCode == IDIGNORE)
103-
return;
104-
};
113+
}
105114

106115
void WindowsDebug::OutputDebugMessage(DEBUG_MESSAGE_SEVERITY Severity,
107116
const Char* Message,

0 commit comments

Comments
 (0)