Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions Sources/OvDebug/include/OvDebug/Assertion.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@

#pragma once

#include <source_location>
#include <string>


#define OVASSERT(condition, message) OvDebug::Assertion::Assert(condition, message)
#ifdef NDEBUG
#define OVASSERT(condition, message) static_cast<void>(0)
#else
#define OVASSERT(condition, message) \
static_cast<bool>(condition) ? \
static_cast<void>(0) : \
OvDebug::_Assert(#condition, message, std::source_location::current());
#endif

namespace OvDebug
{
/**
* Wrapper for C++ assert
*/
class Assertion
{
public:

/**
* Disabled constructor
*/
Assertion() = delete;

/**
* C++ assertion wrapped call
* @param p_condition
* @param p_message
*/
static void Assert(bool p_condition, const std::string& p_message = "");
};
void _Assert(
const char* p_expression,
const std::string_view p_message,
const std::source_location& p_location
);
}
25 changes: 21 additions & 4 deletions Sources/OvDebug/src/OvDebug/Assertion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@
* @licence: MIT
*/

#include "OvDebug/Assertion.h"
#include <OvDebug/Assertion.h>
#include <OvDebug/Logger.h>

#include <cassert>
#include <format>

void OvDebug::Assertion::Assert(bool p_condition, const std::string& p_message)
#if defined(_MSC_VER)
# define OV_DEBUG_BREAK() __debugbreak()
#elif defined(__clang__) || defined(__GNUC__)
# define OV_DEBUG_BREAK() __builtin_trap()
#else
# define OV_DEBUG_BREAK() std::abort()
#endif

void OvDebug::_Assert(const char* p_expression, const std::string_view p_message, const std::source_location& p_location)
{
assert(p_condition && p_message.c_str());
}
OVLOG_ERROR(std::format(
"Assertion failed: {}\n"
" Expression: {}\n"
" Function: {}\n"
" Location: {}:{}",
p_message, p_expression, p_location.function_name(),
p_location.file_name(), p_location.line()));
OV_DEBUG_BREAK();
}