From patchwork Mon Mar 7 01:22:46 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 85627 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1F358B70FF for ; Mon, 7 Mar 2011 12:40:36 +1100 (EST) Received: from localhost ([127.0.0.1]:56484 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PwPR0-000709-To for incoming@patchwork.ozlabs.org; Sun, 06 Mar 2011 20:40:31 -0500 Received: from [140.186.70.92] (port=47635 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PwPAd-0007c2-Np for qemu-devel@nongnu.org; Sun, 06 Mar 2011 20:23:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PwPAP-0000Nf-TT for qemu-devel@nongnu.org; Sun, 06 Mar 2011 20:23:35 -0500 Received: from e2.ny.us.ibm.com ([32.97.182.142]:46639) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PwPAP-0000MU-Hx for qemu-devel@nongnu.org; Sun, 06 Mar 2011 20:23:21 -0500 Received: from d01dlp01.pok.ibm.com (d01dlp01.pok.ibm.com [9.56.224.56]) by e2.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id p2714q4w027463 for ; Sun, 6 Mar 2011 20:04:52 -0500 Received: from d01relay01.pok.ibm.com (d01relay01.pok.ibm.com [9.56.227.233]) by d01dlp01.pok.ibm.com (Postfix) with ESMTP id 9F88538C8038 for ; Sun, 6 Mar 2011 20:23:12 -0500 (EST) Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by d01relay01.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p271NEuR398112 for ; Sun, 6 Mar 2011 20:23:14 -0500 Received: from d01av01.pok.ibm.com (loopback [127.0.0.1]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p271NEtP020530 for ; Sun, 6 Mar 2011 20:23:14 -0500 Received: from localhost.localdomain (sig-9-65-217-198.mts.ibm.com [9.65.217.198]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p271N9ML020370; Sun, 6 Mar 2011 20:23:13 -0500 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Sun, 6 Mar 2011 19:22:46 -0600 Message-Id: <1299460984-15849-5-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1299460984-15849-1-git-send-email-aliguori@us.ibm.com> References: <1299460984-15849-1-git-send-email-aliguori@us.ibm.com> X-Content-Scanned: Fidelis XPS MAILER X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 32.97.182.142 Cc: Adam Litke , Anthony Liguori , Markus Armbruster , Luiz Capitulino Subject: [Qemu-devel] [PATCH 04/22] qerror: split out the reporting bits of QError X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org These make it very hard to compile QError outside of QEMU. Signed-off-by: Anthony Liguori diff --git a/Makefile.objs b/Makefile.objs index da31530..69f0383 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -2,7 +2,7 @@ # QObject qobject-obj-y = qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o qobject-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o -qobject-obj-y += qerror.o +qobject-obj-y += qerror.o qerror-report.o ####################################################################### # oslib-obj-y is code depending on the OS (win32 vs posix) diff --git a/qerror-report.c b/qerror-report.c new file mode 100644 index 0000000..1ebb111 --- /dev/null +++ b/qerror-report.c @@ -0,0 +1,131 @@ +/* + * QError Module + * + * Copyright (C) 2009 Red Hat Inc. + * + * Authors: + * Luiz Capitulino + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + */ + +#include "qemu-common.h" +#include "qerror.h" +#include "monitor.h" +#include "qjson.h" + +static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr, + const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func); + fprintf(stderr, "qerror: -> "); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr); + abort(); +} + +static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr, + const char *fmt, va_list *va) +{ + QObject *obj; + + obj = qobject_from_jsonv(fmt, va); + if (!obj) { + qerror_abort(qerr, "invalid format '%s'", fmt); + } + if (qobject_type(obj) != QTYPE_QDICT) { + qerror_abort(qerr, "error format is not a QDict '%s'", fmt); + } + + qerr->error = qobject_to_qdict(obj); + + obj = qdict_get(qerr->error, "class"); + if (!obj) { + qerror_abort(qerr, "missing 'class' key in '%s'", fmt); + } + if (qobject_type(obj) != QTYPE_QSTRING) { + qerror_abort(qerr, "'class' key value should be a QString"); + } + + obj = qdict_get(qerr->error, "data"); + if (!obj) { + qerror_abort(qerr, "missing 'data' key in '%s'", fmt); + } + if (qobject_type(obj) != QTYPE_QDICT) { + qerror_abort(qerr, "'data' key value should be a QDICT"); + } +} + +/** + * qerror_from_info(): Create a new QError from error information + * + * The information consists of: + * + * - file the file name of where the error occurred + * - linenr the line number of where the error occurred + * - func the function name of where the error occurred + * - fmt JSON printf-like dictionary, there must exist keys 'class' and + * 'data' + * - va va_list of all arguments specified by fmt + * + * Return strong reference. + */ +QError *qerror_from_info(const char *file, int linenr, const char *func, + const char *fmt, va_list *va) +{ + QError *qerr; + + qerr = qerror_new(); + loc_save(&qerr->loc); + qerr->linenr = linenr; + qerr->file = file; + qerr->func = func; + + if (!fmt) { + qerror_abort(qerr, "QDict not specified"); + } + + qerror_set_data(qerr, fmt, va); + qerror_set_desc(qerr, fmt); + + return qerr; +} + +/** + * qerror_print(): Print QError data + * + * This function will print the member 'desc' of the specified QError object, + * it uses error_report() for this, so that the output is routed to the right + * place (ie. stderr or Monitor's device). + */ +void qerror_print(QError *qerror) +{ + QString *qstring = qerror_human(qerror); + loc_push_restore(&qerror->loc); + error_report("%s", qstring_get_str(qstring)); + loc_pop(&qerror->loc); + QDECREF(qstring); +} + +void qerror_report_internal(const char *file, int linenr, const char *func, + const char *fmt, ...) +{ + va_list va; + QError *qerror; + + va_start(va, fmt); + qerror = qerror_from_info(file, linenr, func, fmt, &va); + va_end(va); + + qerror_print(qerror); + QDECREF(qerror); +} + + diff --git a/qerror.c b/qerror.c index 13d53c9..78d3884 100644 --- a/qerror.c +++ b/qerror.c @@ -243,39 +243,7 @@ static void GCC_FMT_ATTR(2, 3) qerror_abort(const QError *qerr, abort(); } -static void GCC_FMT_ATTR(2, 0) qerror_set_data(QError *qerr, - const char *fmt, va_list *va) -{ - QObject *obj; - - obj = qobject_from_jsonv(fmt, va); - if (!obj) { - qerror_abort(qerr, "invalid format '%s'", fmt); - } - if (qobject_type(obj) != QTYPE_QDICT) { - qerror_abort(qerr, "error format is not a QDict '%s'", fmt); - } - - qerr->error = qobject_to_qdict(obj); - - obj = qdict_get(qerr->error, "class"); - if (!obj) { - qerror_abort(qerr, "missing 'class' key in '%s'", fmt); - } - if (qobject_type(obj) != QTYPE_QSTRING) { - qerror_abort(qerr, "'class' key value should be a QString"); - } - - obj = qdict_get(qerr->error, "data"); - if (!obj) { - qerror_abort(qerr, "missing 'data' key in '%s'", fmt); - } - if (qobject_type(obj) != QTYPE_QDICT) { - qerror_abort(qerr, "'data' key value should be a QDICT"); - } -} - -static void qerror_set_desc(QError *qerr, const char *fmt) +void qerror_set_desc(QError *qerr, const char *fmt) { int i; @@ -291,41 +259,6 @@ static void qerror_set_desc(QError *qerr, const char *fmt) qerror_abort(qerr, "error format '%s' not found", fmt); } -/** - * qerror_from_info(): Create a new QError from error information - * - * The information consists of: - * - * - file the file name of where the error occurred - * - linenr the line number of where the error occurred - * - func the function name of where the error occurred - * - fmt JSON printf-like dictionary, there must exist keys 'class' and - * 'data' - * - va va_list of all arguments specified by fmt - * - * Return strong reference. - */ -QError *qerror_from_info(const char *file, int linenr, const char *func, - const char *fmt, va_list *va) -{ - QError *qerr; - - qerr = qerror_new(); - loc_save(&qerr->loc); - qerr->linenr = linenr; - qerr->file = file; - qerr->func = func; - - if (!fmt) { - qerror_abort(qerr, "QDict not specified"); - } - - qerror_set_data(qerr, fmt, va); - qerror_set_desc(qerr, fmt); - - return qerr; -} - static void parse_error(const QErrorStringTable *entry, int c) { #if 0 @@ -441,40 +374,6 @@ QString *qerror_human(const QError *qerror) } /** - * qerror_print(): Print QError data - * - * This function will print the member 'desc' of the specified QError object, - * it uses error_report() for this, so that the output is routed to the right - * place (ie. stderr or Monitor's device). - */ -void qerror_print(QError *qerror) -{ - QString *qstring = qerror_human(qerror); - loc_push_restore(&qerror->loc); - error_report("%s", qstring_get_str(qstring)); - loc_pop(&qerror->loc); - QDECREF(qstring); -} - -void qerror_report_internal(const char *file, int linenr, const char *func, - const char *fmt, ...) -{ - va_list va; - QError *qerror; - - va_start(va, fmt); - qerror = qerror_from_info(file, linenr, func, fmt, &va); - va_end(va); - - if (monitor_cur_is_qmp()) { - monitor_set_error(cur_mon, qerror); - } else { - qerror_print(qerror); - QDECREF(qerror); - } -} - -/** * qobject_to_qerror(): Convert a QObject into a QError */ QError *qobject_to_qerror(const QObject *obj) diff --git a/qerror.h b/qerror.h index fd63ee9..495d0a4 100644 --- a/qerror.h +++ b/qerror.h @@ -43,6 +43,7 @@ void qerror_report_internal(const char *file, int linenr, const char *func, qerror_report_internal(__FILE__, __LINE__, __func__, fmt, ## __VA_ARGS__) QError *qobject_to_qerror(const QObject *obj); QString *qerror_format(const char *fmt, QDict *error); +void qerror_set_desc(QError *qerr, const char *fmt); /* * QError class list