2 * QError: QEMU Error data-type.
4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
16 #include "qemu-common.h"
18 static void qerror_destroy_obj(QObject
*obj
);
20 static const QType qerror_type
= {
22 .destroy
= qerror_destroy_obj
,
26 * The 'desc' parameter is a printf-like string, the format of the format
31 * Where KEY is a QDict key, which has to be passed to qerror_from_info().
35 * "foo error on device: %(device) slot: %(slot_nr)"
37 * A single percent sign can be printed if followed by a second one,
40 * "running out of foo: %(foo)%%"
42 const QErrorStringTable qerror_table
[] = {
47 * qerror_new(): Create a new QError
49 * Return strong reference.
51 QError
*qerror_new(void)
55 qerr
= qemu_mallocz(sizeof(*qerr
));
56 QOBJECT_INIT(qerr
, &qerror_type
);
61 static void qerror_abort(const QError
*qerr
, const char *fmt
, ...)
65 fprintf(stderr
, "qerror: bad call in function '%s':\n", qerr
->func
);
66 fprintf(stderr
, "qerror: -> ");
69 vfprintf(stderr
, fmt
, ap
);
72 fprintf(stderr
, "\nqerror: call at %s:%d\n", qerr
->file
, qerr
->linenr
);
76 static void qerror_set_data(QError
*qerr
, const char *fmt
, va_list *va
)
80 obj
= qobject_from_jsonv(fmt
, va
);
82 qerror_abort(qerr
, "invalid format '%s'", fmt
);
84 if (qobject_type(obj
) != QTYPE_QDICT
) {
85 qerror_abort(qerr
, "error format is not a QDict '%s'", fmt
);
88 qerr
->error
= qobject_to_qdict(obj
);
90 obj
= qdict_get(qerr
->error
, "class");
92 qerror_abort(qerr
, "missing 'class' key in '%s'", fmt
);
94 if (qobject_type(obj
) != QTYPE_QSTRING
) {
95 qerror_abort(qerr
, "'class' key value should be a QString");
98 obj
= qdict_get(qerr
->error
, "data");
100 qerror_abort(qerr
, "missing 'data' key in '%s'", fmt
);
102 if (qobject_type(obj
) != QTYPE_QDICT
) {
103 qerror_abort(qerr
, "'data' key value should be a QDICT");
107 static void qerror_set_desc(QError
*qerr
, const char *fmt
)
111 // FIXME: inefficient loop
113 for (i
= 0; qerror_table
[i
].error_fmt
; i
++) {
114 if (strcmp(qerror_table
[i
].error_fmt
, fmt
) == 0) {
115 qerr
->entry
= &qerror_table
[i
];
120 qerror_abort(qerr
, "error format '%s' not found", fmt
);
124 * qerror_from_info(): Create a new QError from error information
126 * The information consists of:
128 * - file the file name of where the error occurred
129 * - linenr the line number of where the error occurred
130 * - func the function name of where the error occurred
131 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
133 * - va va_list of all arguments specified by fmt
135 * Return strong reference.
137 QError
*qerror_from_info(const char *file
, int linenr
, const char *func
,
138 const char *fmt
, va_list *va
)
143 qerr
->linenr
= linenr
;
148 qerror_abort(qerr
, "QDict not specified");
151 qerror_set_data(qerr
, fmt
, va
);
152 qerror_set_desc(qerr
, fmt
);
157 static void parse_error(const QError
*qerror
, int c
)
159 qerror_abort(qerror
, "expected '%c' in '%s'", c
, qerror
->entry
->desc
);
162 static const char *append_field(QString
*outstr
, const QError
*qerror
,
168 const char *end
, *key
;
171 parse_error(qerror
, '%');
174 parse_error(qerror
, '(');
177 end
= strchr(start
, ')');
179 parse_error(qerror
, ')');
181 key_qs
= qstring_from_substr(start
, 0, end
- start
- 1);
182 key
= qstring_get_str(key_qs
);
184 qdict
= qobject_to_qdict(qdict_get(qerror
->error
, "data"));
185 obj
= qdict_get(qdict
, key
);
187 qerror_abort(qerror
, "key '%s' not found in QDict", key
);
190 switch (qobject_type(obj
)) {
192 qstring_append(outstr
, qdict_get_str(qdict
, key
));
195 qstring_append_int(outstr
, qdict_get_int(qdict
, key
));
198 qerror_abort(qerror
, "invalid type '%c'", qobject_type(obj
));
206 * qerror_print(): Print QError data
208 * This function will print the member 'desc' of the specified QError object,
209 * it uses qemu_error() for this, so that the output is routed to the right
210 * place (ie. stderr or Monitor's device).
212 void qerror_print(const QError
*qerror
)
217 assert(qerror
->entry
!= NULL
);
219 qstring
= qstring_new();
221 for (p
= qerror
->entry
->desc
; *p
!= '\0';) {
223 qstring_append_chr(qstring
, *p
++);
224 } else if (*(p
+ 1) == '%') {
225 qstring_append_chr(qstring
, '%');
228 p
= append_field(qstring
, qerror
, p
);
232 qemu_error("%s\n", qstring_get_str(qstring
));
237 * qobject_to_qerror(): Convert a QObject into a QError
239 QError
*qobject_to_qerror(const QObject
*obj
)
241 if (qobject_type(obj
) != QTYPE_QERROR
) {
245 return container_of(obj
, QError
, base
);
249 * qerror_destroy_obj(): Free all memory allocated by a QError
251 static void qerror_destroy_obj(QObject
*obj
)
256 qerr
= qobject_to_qerror(obj
);
258 QDECREF(qerr
->error
);