提交 597efed4 编写于 作者: M Megvii Engine Team

feat(lite): add get last error code interface in lite c

GitOrigin-RevId: 280cc88092a8357565e5b3f2037529891ed750f3
上级 90c8a58c
......@@ -22,6 +22,15 @@ typedef enum {
ERROR = 3, /*!< Print only errors */
} LiteLogLevel;
/*!
* \brief The Error Code
*/
typedef enum {
OK = 0,
LITE_INTERNAL_ERROR = 1,
LITE_UNKNOWN_ERROR = 2,
} ErrorCode;
typedef enum {
LITE_DEFAULT = 0, //! default backend is mge
} LiteBackend;
......
......@@ -23,6 +23,15 @@ extern "C" {
*/
LITE_API int LITE_get_version(int* major, int* minor, int* patch);
/*! \brief Get the last error code.
* \return the current error code
*/
LITE_API ErrorCode LITE_get_last_error_code();
/*! \brief Clear the last error code and error message.
*/
LITE_API void LITE_clear_last_error();
/*! \brief Get the last error message.
* \return the message pointer
*/
......
......@@ -75,7 +75,7 @@ extern "C" {
* mask 0b10: async if there are multiple comp nodes with
* mask 0b100: always async
*/
typedef struct Options {
typedef struct {
int weight_preprocess;
int fuse_preprocess;
int fake_next_exec;
......@@ -128,7 +128,7 @@ LITE_API LiteConfig* default_config();
* \brief config the network input and output item
*
*/
typedef struct LiteIO {
typedef struct {
//! the tensor name in the graph corresponding to the IO
const char* name;
......@@ -157,7 +157,7 @@ extern LITE_API const LiteIO default_io;
* \brief the input and output information when load the network
* the NetworkIO will remain in the network until the network is destroyed
*/
typedef struct LiteNetworkIO {
typedef struct {
LiteIO* inputs;
LiteIO* outputs;
size_t input_size; //! the number IO in inputs
......
......@@ -17,10 +17,20 @@ namespace {
class ErrorMsg {
public:
std::string& get_error_msg() { return error_msg; }
void set_error_msg(const std::string& msg) { error_msg = msg; }
ErrorCode get_error_code() { return error_code; }
void set_error_msg(const std::string& msg, ErrorCode code) {
error_msg = msg + ", Error Code: " + std::to_string(code);
error_code = code;
}
void clear_error() {
error_code = ErrorCode::OK;
error_msg.clear();
}
private:
std::string error_msg;
ErrorCode error_code;
};
static LITE_MUTEX mtx_error;
......@@ -32,10 +42,20 @@ ErrorMsg& get_global_error() {
int LiteHandleException(const std::exception& e) {
LITE_LOCK_GUARD(mtx_error);
get_global_error().set_error_msg(e.what());
get_global_error().set_error_msg(e.what(), ErrorCode::LITE_INTERNAL_ERROR);
return -1;
}
ErrorCode LITE_get_last_error_code() {
LITE_LOCK_GUARD(mtx_error);
return get_global_error().get_error_code();
}
void LITE_clear_last_error() {
LITE_LOCK_GUARD(mtx_error);
get_global_error().clear_error();
}
const char* LITE_get_last_error() {
LITE_LOCK_GUARD(mtx_error);
return get_global_error().get_error_msg().c_str();
......
......@@ -56,7 +56,11 @@ TEST(TestCapiTensor, Basic) {
//! test error
ASSERT_EQ(LITE_is_pinned_host(c_tensor0, nullptr), -1);
ASSERT_NE(strlen(LITE_get_last_error()), 0);
ASSERT_EQ(LITE_get_last_error_code(), ErrorCode::LITE_INTERNAL_ERROR);
printf("The last error is: %s\n", LITE_get_last_error());
LITE_clear_last_error();
ASSERT_EQ(strlen(LITE_get_last_error()), 0);
ASSERT_EQ(LITE_get_last_error_code(), ErrorCode::OK);
LITE_destroy_tensor(c_tensor0);
LITE_destroy_tensor(c_tensor1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册