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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/zencore.h>
#if ZEN_PLATFORM_WINDOWS
# include <zencore/windows.h>
#endif
#include <zencore/assertfmt.h>
#include <zencore/blake3.h>
#include <zencore/callstack.h>
#include <zencore/compactbinary.h>
#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinarypackage.h>
#include <zencore/compositebuffer.h>
#include <zencore/compress.h>
#include <zencore/crypto.h>
#include <zencore/filesystem.h>
#include <zencore/intmath.h>
#include <zencore/iobuffer.h>
#include <zencore/jobqueue.h>
#include <zencore/logging.h>
#include <zencore/memory.h>
#include <zencore/mpscqueue.h>
#include <zencore/process.h>
#include <zencore/sha1.h>
#include <zencore/stats.h>
#include <zencore/stream.h>
#include <zencore/string.h>
#include <zencore/thread.h>
#include <zencore/timer.h>
#include <zencore/uid.h>
#include <zencore/workthreadpool.h>
#include <fmt/format.h>
#include <atomic>
namespace zen::assert {
void
ExecAssertFmt(const char* Filename, int LineNumber, const char* FunctionName, std::string_view Format, fmt::format_args Args)
{
fmt::basic_memory_buffer<char, 1024> Message;
fmt::vformat_to(fmt::appender(Message), Format, Args);
Message.push_back('\0');
AssertImpl::ExecAssert(Filename, LineNumber, FunctionName, Message.data());
}
} // namespace zen::assert
namespace zen {
AssertException::AssertException(const AssertException& Rhs) noexcept : _Mybase(Rhs), _Callstack(CloneCallstack(Rhs._Callstack))
{
}
AssertException::AssertException(AssertException&& Rhs) noexcept : _Mybase(Rhs), _Callstack(Rhs._Callstack)
{
Rhs._Callstack = nullptr;
}
AssertException::~AssertException() noexcept
{
FreeCallstack(_Callstack);
}
AssertException&
AssertException::operator=(const AssertException& Rhs) noexcept
{
_Mybase::operator=(Rhs);
CallstackFrames* Callstack = CloneCallstack(Rhs._Callstack);
std::swap(_Callstack, Callstack);
FreeCallstack(Callstack);
return *this;
}
std::string
AssertException::FullDescription() const noexcept
{
if (_Callstack)
{
return fmt::format("'{}'\n{}", what(), CallstackToString(_Callstack));
}
return what();
}
void
AssertImpl::ThrowAssertException(const char* Filename, int LineNumber, const char* FunctionName, const char* Msg)
{
ZEN_UNUSED(FunctionName);
fmt::basic_memory_buffer<char, 2048> Message;
auto Appender = fmt::appender(Message);
fmt::format_to(Appender, "{}({}): {}", Filename, LineNumber, Msg);
Message.push_back('\0');
void* Frames[8];
uint32_t FrameCount = GetCallstack(3, 8, Frames);
throw AssertException(Message.data(), CreateCallstack(FrameCount, Frames));
}
void refcount_forcelink();
AssertImpl* AssertImpl::CurrentAssertImpl = nullptr;
AssertImpl AssertImpl::DefaultAssertImpl;
//////////////////////////////////////////////////////////////////////////
bool
IsDebuggerPresent()
{
#if ZEN_PLATFORM_WINDOWS
return ::IsDebuggerPresent();
#else
return false;
#endif
}
std::optional<bool> InteractiveSessionFlag;
void
SetIsInteractiveSession(bool Value)
{
InteractiveSessionFlag = Value;
}
bool
IsInteractiveSession()
{
if (!InteractiveSessionFlag.has_value())
{
#if ZEN_PLATFORM_WINDOWS
DWORD dwSessionId = 0;
if (ProcessIdToSessionId(GetCurrentProcessId(), &dwSessionId))
{
InteractiveSessionFlag = (dwSessionId != 0);
}
else
{
InteractiveSessionFlag = false;
}
#else
// TODO: figure out what actually makes sense here
InteractiveSessionFlag = true;
#endif
}
return InteractiveSessionFlag.value();
}
//////////////////////////////////////////////////////////////////////////
static std::atomic_int s_ApplicationExitCode{0};
static std::atomic_bool s_ApplicationExitRequested{false};
bool
IsApplicationExitRequested()
{
return s_ApplicationExitRequested;
}
bool
RequestApplicationExit(int ExitCode)
{
bool Expected = false;
if (s_ApplicationExitRequested.compare_exchange_weak(Expected, true))
{
s_ApplicationExitCode = ExitCode;
return true;
}
return false;
}
int
ApplicationExitCode()
{
return s_ApplicationExitCode;
}
#if ZEN_WITH_TESTS
void
zencore_forcelinktests()
{
zen::blake3_forcelink();
zen::callstack_forcelink();
zen::compositebuffer_forcelink();
zen::compress_forcelink();
zen::crypto_forcelink();
zen::filesystem_forcelink();
zen::jobqueue_forcelink();
zen::intmath_forcelink();
zen::iobuffer_forcelink();
zen::logging_forcelink();
zen::memory_forcelink();
zen::mpscqueue_forcelink();
zen::process_forcelink();
zen::refcount_forcelink();
zen::sha1_forcelink();
zen::stats_forcelink();
zen::stream_forcelink();
zen::string_forcelink();
zen::thread_forcelink();
zen::timer_forcelink();
zen::uid_forcelink();
zen::uson_forcelink();
zen::usonbuilder_forcelink();
zen::usonpackage_forcelink();
zen::cbjson_forcelink();
zen::cbyaml_forcelink();
zen::workthreadpool_forcelink();
}
} // namespace zen
# include <zencore/testing.h>
namespace zen {
TEST_SUITE_BEGIN("core.assert");
TEST_CASE("Assert.Default")
{
bool A = true;
bool B = false;
std::string Expected = fmt::format("{}({}): {}", __FILE__, __LINE__ + 1, "A == B");
CHECK_THROWS_WITH(ZEN_ASSERT(A == B), Expected.c_str());
}
TEST_CASE("Assert.Format")
{
bool A = true;
bool B = false;
std::string Expected = fmt::format("{}({}): {}", __FILE__, __LINE__ + 1, "assert(A == B) failed: true == false");
CHECK_THROWS_WITH(ZEN_ASSERT_FORMAT(A == B, "{} == {}", A, B), Expected.c_str());
}
TEST_CASE("Assert.Custom")
{
struct MyAssertImpl : AssertImpl
{
virtual void ZEN_FORCENOINLINE ZEN_DEBUG_SECTION OnAssert(const char* Filename,
int LineNumber,
const char* FunctionName,
const char* Msg)
{
AssertFileName = Filename;
Line = LineNumber;
FuncName = FunctionName;
Message = Msg;
}
AssertImpl* PrevAssertImpl = nullptr;
const char* AssertFileName = nullptr;
int Line = -1;
const char* FuncName = nullptr;
const char* Message = nullptr;
};
MyAssertImpl MyAssert;
bool A = true;
bool B = false;
CHECK_THROWS_WITH(ZEN_ASSERT(A == B), std::string(fmt::format("{}({}): {}", __FILE__, __LINE__, "A == B")).c_str());
CHECK(MyAssert.AssertFileName != nullptr);
CHECK(MyAssert.Line != -1);
CHECK(MyAssert.FuncName != nullptr);
CHECK(strcmp(MyAssert.Message, "A == B") == 0);
}
TEST_CASE("Assert.Callstack")
{
try
{
ZEN_ASSERT(false);
}
catch (const AssertException& Assert)
{
ZEN_INFO("Assert failed: {}", Assert.what());
CHECK(Assert._Callstack->FrameCount > 0);
CHECK(Assert._Callstack->Frames != nullptr);
ZEN_INFO("Callstack:\n{}", CallstackToString(Assert._Callstack));
}
WorkerThreadPool Pool(1);
auto Task = Pool.EnqueueTask(std::packaged_task<int()>{[] {
ZEN_ASSERT(false);
return 1;
}});
try
{
(void)Task.get();
CHECK(false);
}
catch (const AssertException& Assert)
{
ZEN_INFO("Assert in future: {}", Assert.what());
CHECK(Assert._Callstack->FrameCount > 0);
CHECK(Assert._Callstack->Frames != nullptr);
ZEN_INFO("Callstack:\n{}", CallstackToString(Assert._Callstack));
}
}
TEST_SUITE_END();
#endif
} // namespace zen
|