On Mon, Sep 10, 2012 at 8:26 AM, Wenchao Xia <xiaw...@linux.vnet.ibm.com> wrote: > This patch contains error handling APIs, which user could call them to > get error details. > > Signed-off-by: Wenchao Xia <xiaw...@linux.vnet.ibm.com> > --- > 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 <xiaw...@linux.vnet.ibm.com> > + * > + * 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 > +{ > + 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. > + 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 <xiaw...@linux.vnet.ibm.com> > + * > + * 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 > >