From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:42469) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TBdBb-0001tF-9l for qemu-devel@nongnu.org; Tue, 11 Sep 2012 23:00:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TBdBZ-0001Vz-2i for qemu-devel@nongnu.org; Tue, 11 Sep 2012 23:00:19 -0400 Received: from e28smtp05.in.ibm.com ([122.248.162.5]:36530) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TBdBY-0001SI-FQ for qemu-devel@nongnu.org; Tue, 11 Sep 2012 23:00:17 -0400 Received: from /spool/local by e28smtp05.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 12 Sep 2012 08:30:11 +0530 Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q8C307JF8388928 for ; Wed, 12 Sep 2012 08:30:07 +0530 Received: from d28av03.in.ibm.com (loopback [127.0.0.1]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q8C306gt020544 for ; Wed, 12 Sep 2012 13:00:06 +1000 Message-ID: <504FFA68.3040105@linux.vnet.ibm.com> Date: Wed, 12 Sep 2012 10:58:48 +0800 From: Wenchao Xia MIME-Version: 1.0 References: <1347265586-17698-1-git-send-email-xiawenc@linux.vnet.ibm.com> <1347265586-17698-4-git-send-email-xiawenc@linux.vnet.ibm.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [PATCH V2 3/6] libqblock error handling List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Blue Swirl Cc: kwolf@redhat.com, aliguori@us.ibm.com, stefanha@gmail.com, qemu-devel@nongnu.org, pbonzini@redhat.com, eblake@redhat.com 于 2012-9-12 4:32, Blue Swirl 写道: > On Mon, Sep 10, 2012 at 8:26 AM, Wenchao Xia wrote: >> This patch contains error handling APIs, which user could call them to >> get error details. >> >> Signed-off-by: Wenchao Xia >> --- >> libqblock/libqblock-error.c | 60 +++++++++++++++++++++++++++++++++++++++++++ >> libqblock/libqblock-error.h | 50 +++++++++++++++++++++++++++++++++++ >> 2 files changed, 110 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..d5ebabf >> --- /dev/null >> +++ b/libqblock/libqblock-error.c >> @@ -0,0 +1,60 @@ >> +/* >> + * QEMU block layer library >> + * >> + * Copyright IBM, Corp. 2012 >> + * >> + * Authors: >> + * Wenchao Xia >> + * >> + * This work is licensed under the terms of the GNU LGPL, version 2 or later. >> + * See the COPYING.LIB file in the top-level directory. >> + * >> + */ >> + >> +#include "libqblock-error.h" >> +#include "libqblock-internal.h" >> + >> +void qb_error_get_human_str(struct QBroker *broker, >> + char *buf, int buf_size) > > size_t buf_size > OK. >> +{ >> + 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; >> + case QB_ERR_BLOCK_OUT_OF_RANGE: >> + err_ret_str = "request is out of image's range."; >> + 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); >> + } > > Please NUL terminate the string. > snprintf will add "\0" automatically, could u explain more about this? >> + 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..0690cfb >> --- /dev/null >> +++ b/libqblock/libqblock-error.h >> @@ -0,0 +1,50 @@ >> +/* >> + * QEMU block layer library >> + * >> + * Copyright IBM, Corp. 2012 >> + * >> + * Authors: >> + * Wenchao Xia >> + * >> + * This work is licensed under the terms of the GNU LGPL, version 2 or later. >> + * See the COPYING.LIB file in the top-level directory. >> + * >> + */ >> + >> +#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 error string. >> + * >> + * return a human readable string, it would be truncated if buf is not big >> + * enough. >> + * >> + * @broker: operation broker, must be valid. >> + * @buf: buf to receive the string. >> + * @buf_size: the size of the string buf. >> + */ >> +DLL_PUBLIC >> +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. >> + */ >> +DLL_PUBLIC >> +int qb_error_get_errno(struct QBroker *broker); >> + >> +#endif >> -- >> 1.7.1 >> >> > -- Best Regards Wenchao Xia