diff --git a/lite/include/lite/common_enum_c.h b/lite/include/lite/common_enum_c.h index 97bcd70e..c37fc462 100644 --- a/lite/include/lite/common_enum_c.h +++ b/lite/include/lite/common_enum_c.h @@ -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; diff --git a/lite/lite-c/include/lite-c/global_c.h b/lite/lite-c/include/lite-c/global_c.h index 76bbad61..acb2f445 100644 --- a/lite/lite-c/include/lite-c/global_c.h +++ b/lite/lite-c/include/lite-c/global_c.h @@ -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 */ diff --git a/lite/lite-c/include/lite-c/network_c.h b/lite/lite-c/include/lite-c/network_c.h index 7305842f..f8887d90 100644 --- a/lite/lite-c/include/lite-c/network_c.h +++ b/lite/lite-c/include/lite-c/network_c.h @@ -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 diff --git a/lite/lite-c/src/global.cpp b/lite/lite-c/src/global.cpp index 74638220..16878a06 100644 --- a/lite/lite-c/src/global.cpp +++ b/lite/lite-c/src/global.cpp @@ -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(); diff --git a/lite/test/test_tensor_c.cpp b/lite/test/test_tensor_c.cpp index 725dd592..5b1950e7 100644 --- a/lite/test/test_tensor_c.cpp +++ b/lite/test/test_tensor_c.cpp @@ -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);