Skip to content

Commit 51f25ba

Browse files
committed
Added ErrorScope name.
1 parent 2f4aacf commit 51f25ba

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

include/bx/error.h

+18-14
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88

99
#include "string.h"
1010

11-
#define BX_ERROR_SET(_ptr, _result, _msg) \
12-
BX_MACRO_BLOCK_BEGIN \
13-
(_ptr)->setError(_result, "" _msg); \
14-
BX_MACRO_BLOCK_END
11+
#define BX_ERROR_SET(_ptr, _result, _msg) \
12+
BX_MACRO_BLOCK_BEGIN \
13+
(_ptr)->setError(_result, "" _msg); \
14+
BX_MACRO_BLOCK_END
1515

16-
#define BX_ERROR_USE_TEMP_WHEN_NULL(_ptr) \
17-
const bx::Error tmpError; /* It should not be used directly! */ \
18-
_ptr = NULL == _ptr ? const_cast<bx::Error*>(&tmpError) : _ptr
16+
#define BX_ERROR_USE_TEMP_WHEN_NULL(_ptr) \
17+
const bx::Error tmpError; /* It should not be used directly! */ \
18+
_ptr = NULL == _ptr ? const_cast<bx::Error*>(&tmpError) : _ptr
1919

20-
#define BX_ERROR_SCOPE(_ptr) \
21-
BX_ERROR_USE_TEMP_WHEN_NULL(_ptr); \
22-
bx::ErrorScope bxErrorScope(const_cast<bx::Error*>(&tmpError) )
20+
#define BX_ERROR_SCOPE(_ptr, ...) \
21+
BX_ERROR_USE_TEMP_WHEN_NULL(_ptr); \
22+
bx::ErrorScope bxErrorScope(const_cast<bx::Error*>(&tmpError), "" __VA_ARGS__)
2323

24-
#define BX_ERROR_RESULT(_err, _code) \
25-
BX_STATIC_ASSERT(_code != 0, "ErrorCode 0 is reserved!"); \
26-
static constexpr bx::ErrorResult _err = { _code }
24+
#define BX_ERROR_RESULT(_err, _code) \
25+
BX_STATIC_ASSERT(_code != 0, "ErrorCode 0 is reserved!"); \
26+
static constexpr bx::ErrorResult _err = { _code }
2727

2828
namespace bx
2929
{
@@ -81,13 +81,17 @@ namespace bx
8181

8282
public:
8383
///
84-
ErrorScope(Error* _err);
84+
ErrorScope(Error* _err, const StringView& _name);
8585

8686
///
8787
~ErrorScope();
8888

89+
///
90+
const StringView& getName() const;
91+
8992
private:
9093
Error* m_err;
94+
const StringView m_name;
9195
};
9296

9397
} // namespace bx

include/bx/inline/error.inl

+25-2
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,38 @@ namespace bx
5959
return _rhs.code != m_code;
6060
}
6161

62-
inline ErrorScope::ErrorScope(Error* _err)
62+
inline ErrorScope::ErrorScope(Error* _err, const StringView& _name)
6363
: m_err(_err)
64+
, m_name(_name)
6465
{
6566
BX_ASSERT(NULL != _err, "_err can't be NULL");
6667
}
6768

6869
inline ErrorScope::~ErrorScope()
6970
{
70-
BX_ASSERT(m_err->isOk(), "Error: %d", m_err->get().code);
71+
if (m_name.isEmpty() )
72+
{
73+
BX_ASSERT(m_err->isOk(), "Error: 0x%08x `%.*s`"
74+
, m_err->get().code
75+
, m_err->getMessage().getLength()
76+
, m_err->getMessage().getPtr()
77+
);
78+
}
79+
else
80+
{
81+
BX_ASSERT(m_err->isOk(), "Error: %.*s - 0x%08x `%.*s`"
82+
, m_name.getLength()
83+
, m_name.getPtr()
84+
, m_err->get().code
85+
, m_err->getMessage().getLength()
86+
, m_err->getMessage().getPtr()
87+
);
88+
}
89+
}
90+
91+
inline const StringView& ErrorScope::getName() const
92+
{
93+
return m_name;
7194
}
7295

7396
} // namespace bx

0 commit comments

Comments
 (0)