From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:42547) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T8Sqz-0003wG-AP for qemu-devel@nongnu.org; Mon, 03 Sep 2012 05:22:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T8Sqt-0007oC-3W for qemu-devel@nongnu.org; Mon, 03 Sep 2012 05:21:57 -0400 Received: from e23smtp02.au.ibm.com ([202.81.31.144]:39068) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T8Sqs-0007o4-HC for qemu-devel@nongnu.org; Mon, 03 Sep 2012 05:21:51 -0400 Received: from /spool/local by e23smtp02.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 3 Sep 2012 19:20:28 +1000 Received: from d23av02.au.ibm.com (d23av02.au.ibm.com [9.190.235.138]) by d23relay05.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q839CdEi12189868 for ; Mon, 3 Sep 2012 19:12:39 +1000 Received: from d23av02.au.ibm.com (loopback [127.0.0.1]) by d23av02.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q839LiNT018331 for ; Mon, 3 Sep 2012 19:21:44 +1000 From: Wenchao Xia Date: Mon, 3 Sep 2012 17:18:43 +0800 Message-Id: <1346663926-20188-4-git-send-email-xiawenc@linux.vnet.ibm.com> In-Reply-To: <1346663926-20188-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1346663926-20188-1-git-send-email-xiawenc@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 3/6] libqblock error handling List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, aliguori@us.ibm.com, stefanha@gmail.com, pbonzini@redhat.com, eblake@redhat.com, Wenchao Xia This patch contains error handling APIs. Signed-off-by: Wenchao Xia --- libqblock/libqblock-error.c | 44 +++++++++++++++++++++++++++++++++++++++++++ libqblock/libqblock-error.h | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 0 deletions(-) create mode 100644 libqblock/libqblock-error.c create mode 100644 libqblock/libqblock-error.h diff --git a/libqblock/libqblock-error.c b/libqblock/libqblock-error.c new file mode 100644 index 0000000..28d1d77 --- /dev/null +++ b/libqblock/libqblock-error.c @@ -0,0 +1,44 @@ +#include "libqblock-error.h" +#include "libqblock-helper.h" + +void qb_error_get_human_str(struct QBroker *broker, + char *buf, int buf_size) +{ + const char *err_ret_str; + switch (broker->err_ret) { + case QB_ERR_MEM_ERR: + err_ret_str = "Not enough memory."; + break; + case QB_ERR_INTERNAL_ERR: + err_ret_str = "Internal error."; + break; + case QB_ERR_INVALID_PARAM: + err_ret_str = "Invalid param."; + break; + default: + err_ret_str = "Unknow error."; + break; + } + if (broker == NULL) { + snprintf(buf, buf_size, "%s", err_ret_str); + return; + } + + if (broker->err_ret == QB_ERR_INTERNAL_ERR) { + snprintf(buf, buf_size, "%s %s errno [%d]. strerror [%s].", + err_ret_str, broker->err_msg, + broker->err_no, strerror(-broker->err_no)); + } else { + snprintf(buf, buf_size, "%s %s", + err_ret_str, broker->err_msg); + } + return; +} + +int qb_error_get_errno(struct QBroker *broker) +{ + if (broker->err_ret == QB_ERR_INTERNAL_ERR) { + return broker->err_no; + } + return 0; +} diff --git a/libqblock/libqblock-error.h b/libqblock/libqblock-error.h new file mode 100644 index 0000000..b11df7c --- /dev/null +++ b/libqblock/libqblock-error.h @@ -0,0 +1,34 @@ +#ifndef LIBQBLOCK_ERROR +#define LIBQBLOCK_ERROR + +#include "libqblock-types.h" + +#define QB_ERR_MEM_ERR (-1) +#define QB_ERR_INTERNAL_ERR (-2) +#define QB_ERR_INVALID_PARAM (-3) +#define QB_ERR_BLOCK_OUT_OF_RANGE (-100) + +/* error handling */ +/** + * qb_error_get_human_str: get human readable erro string. + * + * return a human readable string. + * + * @broker: operation broker, must be valid. + * @buf: buf to receive the string. + * @buf_size: the size of the string buf. + */ +void qb_error_get_human_str(struct QBroker *broker, + char *buf, int buf_size); + +/** + * qb_error_get_errno: get error number, only valid when err_ret is + * QB_ERR_INTERNAL_ERR. + * + * return negative errno or 0 if last error is not QB_ERR_INTERNAL_ERR. + * + * @broker: operation broker. + */ +int qb_error_get_errno(struct QBroker *broker); + +#endif -- 1.7.1