diff --git a/include/bx/error.h b/include/bx/error.h index 59b259d..d00d8af 100644 --- a/include/bx/error.h +++ b/include/bx/error.h @@ -8,22 +8,22 @@ #include "string.h" -#define BX_ERROR_SET(_ptr, _result, _msg) \ - BX_MACRO_BLOCK_BEGIN \ - (_ptr)->setError(_result, "" _msg); \ - BX_MACRO_BLOCK_END +#define BX_ERROR_SET(_ptr, _result, _msg) \ + BX_MACRO_BLOCK_BEGIN \ + (_ptr)->setError(_result, "" _msg); \ + BX_MACRO_BLOCK_END -#define BX_ERROR_USE_TEMP_WHEN_NULL(_ptr) \ - const bx::Error tmpError; /* It should not be used directly! */ \ - _ptr = NULL == _ptr ? const_cast(&tmpError) : _ptr +#define BX_ERROR_USE_TEMP_WHEN_NULL(_ptr) \ + const bx::Error tmpError; /* It should not be used directly! */ \ + _ptr = NULL == _ptr ? const_cast(&tmpError) : _ptr -#define BX_ERROR_SCOPE(_ptr) \ - BX_ERROR_USE_TEMP_WHEN_NULL(_ptr); \ - bx::ErrorScope bxErrorScope(const_cast(&tmpError) ) +#define BX_ERROR_SCOPE(_ptr, ...) \ + BX_ERROR_USE_TEMP_WHEN_NULL(_ptr); \ + bx::ErrorScope bxErrorScope(const_cast(&tmpError), "" __VA_ARGS__) -#define BX_ERROR_RESULT(_err, _code) \ - BX_STATIC_ASSERT(_code != 0, "ErrorCode 0 is reserved!"); \ - static constexpr bx::ErrorResult _err = { _code } +#define BX_ERROR_RESULT(_err, _code) \ + BX_STATIC_ASSERT(_code != 0, "ErrorCode 0 is reserved!"); \ + static constexpr bx::ErrorResult _err = { _code } namespace bx { @@ -81,13 +81,17 @@ namespace bx public: /// - ErrorScope(Error* _err); + ErrorScope(Error* _err, const StringView& _name); /// ~ErrorScope(); + /// + const StringView& getName() const; + private: Error* m_err; + const StringView m_name; }; } // namespace bx diff --git a/include/bx/inline/error.inl b/include/bx/inline/error.inl index 288f65d..382c624 100644 --- a/include/bx/inline/error.inl +++ b/include/bx/inline/error.inl @@ -59,15 +59,38 @@ namespace bx return _rhs.code != m_code; } - inline ErrorScope::ErrorScope(Error* _err) + inline ErrorScope::ErrorScope(Error* _err, const StringView& _name) : m_err(_err) + , m_name(_name) { BX_ASSERT(NULL != _err, "_err can't be NULL"); } inline ErrorScope::~ErrorScope() { - BX_ASSERT(m_err->isOk(), "Error: %d", m_err->get().code); + if (m_name.isEmpty() ) + { + BX_ASSERT(m_err->isOk(), "Error: 0x%08x `%.*s`" + , m_err->get().code + , m_err->getMessage().getLength() + , m_err->getMessage().getPtr() + ); + } + else + { + BX_ASSERT(m_err->isOk(), "Error: %.*s - 0x%08x `%.*s`" + , m_name.getLength() + , m_name.getPtr() + , m_err->get().code + , m_err->getMessage().getLength() + , m_err->getMessage().getPtr() + ); + } + } + + inline const StringView& ErrorScope::getName() const + { + return m_name; } } // namespace bx