[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH V2 3/6] libqblock error handling
From: |
Blue Swirl |
Subject: |
Re: [Qemu-devel] [PATCH V2 3/6] libqblock error handling |
Date: |
Tue, 11 Sep 2012 20:32:57 +0000 |
On Mon, Sep 10, 2012 at 8:26 AM, Wenchao Xia <address@hidden> wrote:
> This patch contains error handling APIs, which user could call them to
> get error details.
>
> Signed-off-by: Wenchao Xia <address@hidden>
> ---
> 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 <address@hidden>
> + *
> + * 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 <address@hidden>
> + *
> + * 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
>
>
- [Qemu-devel] [PATCH V2 0/6] libqblock, qemu block layer library, Wenchao Xia, 2012/09/10
- [Qemu-devel] [PATCH V2 4/6] libqblock export some qemu block function, Wenchao Xia, 2012/09/10
- [Qemu-devel] [PATCH V2 5/6] libqblock building system, Wenchao Xia, 2012/09/10
- [Qemu-devel] [PATCH V2 6/6] libqblock test example, Wenchao Xia, 2012/09/10
- [Qemu-devel] [PATCH V2 1/6] libqblock API design, Wenchao Xia, 2012/09/10
- Re: [Qemu-devel] [PATCH V2 1/6] libqblock API design, Blue Swirl, 2012/09/11