Skip to content

Commit 12fc181

Browse files
authored
Updated assert to be more descriptive (#629)
1 parent 331c441 commit 12fc181

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

Sources/OvDebug/include/OvDebug/Assertion.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,23 @@
66

77
#pragma once
88

9+
#include <source_location>
910
#include <string>
1011

11-
12-
#define OVASSERT(condition, message) OvDebug::Assertion::Assert(condition, message)
12+
#ifdef NDEBUG
13+
#define OVASSERT(condition, message) static_cast<void>(0)
14+
#else
15+
#define OVASSERT(condition, message) \
16+
static_cast<bool>(condition) ? \
17+
static_cast<void>(0) : \
18+
OvDebug::_Assert(#condition, message, std::source_location::current());
19+
#endif
1320

1421
namespace OvDebug
1522
{
16-
/**
17-
* Wrapper for C++ assert
18-
*/
19-
class Assertion
20-
{
21-
public:
22-
23-
/**
24-
* Disabled constructor
25-
*/
26-
Assertion() = delete;
27-
28-
/**
29-
* C++ assertion wrapped call
30-
* @param p_condition
31-
* @param p_message
32-
*/
33-
static void Assert(bool p_condition, const std::string& p_message = "");
34-
};
23+
void _Assert(
24+
const char* p_expression,
25+
const std::string_view p_message,
26+
const std::source_location& p_location
27+
);
3528
}

Sources/OvDebug/src/OvDebug/Assertion.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,28 @@
44
* @licence: MIT
55
*/
66

7-
#include "OvDebug/Assertion.h"
7+
#include <OvDebug/Assertion.h>
8+
#include <OvDebug/Logger.h>
89

910
#include <cassert>
11+
#include <format>
1012

11-
void OvDebug::Assertion::Assert(bool p_condition, const std::string& p_message)
13+
#if defined(_MSC_VER)
14+
# define OV_DEBUG_BREAK() __debugbreak()
15+
#elif defined(__clang__) || defined(__GNUC__)
16+
# define OV_DEBUG_BREAK() __builtin_trap()
17+
#else
18+
# define OV_DEBUG_BREAK() std::abort()
19+
#endif
20+
21+
void OvDebug::_Assert(const char* p_expression, const std::string_view p_message, const std::source_location& p_location)
1222
{
13-
assert(p_condition && p_message.c_str());
14-
}
23+
OVLOG_ERROR(std::format(
24+
"Assertion failed: {}\n"
25+
" Expression: {}\n"
26+
" Function: {}\n"
27+
" Location: {}:{}",
28+
p_message, p_expression, p_location.function_name(),
29+
p_location.file_name(), p_location.line()));
30+
OV_DEBUG_BREAK();
31+
}

0 commit comments

Comments
 (0)