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
[] = {
44 .error_fmt
= QERR_DEVICE_NOT_FOUND
,
45 .desc
= "The %(device) device has not been found",
48 .error_fmt
= QERR_DEVICE_NOT_ACTIVE
,
49 .desc
= "The %(device) device has not been activated by the guest",
55 * qerror_new(): Create a new QError
57 * Return strong reference.
59 QError
*qerror_new(void)
63 qerr
= qemu_mallocz(sizeof(*qerr
));
64 QOBJECT_INIT(qerr
, &qerror_type
);
69 static void qerror_abort(const QError
*qerr
, const char *fmt
, ...)
73 fprintf(stderr
, "qerror: bad call in function '%s':\n", qerr
->func
);
74 fprintf(stderr
, "qerror: -> ");
77 vfprintf(stderr
, fmt
, ap
);
80 fprintf(stderr
, "\nqerror: call at %s:%d\n", qerr
->file
, qerr
->linenr
);
84 static void qerror_set_data(QError
*qerr
, const char *fmt
, va_list *va
)
88 obj
= qobject_from_jsonv(fmt
, va
);
90 qerror_abort(qerr
, "invalid format '%s'", fmt
);
92 if (qobject_type(obj
) != QTYPE_QDICT
) {
93 qerror_abort(qerr
, "error format is not a QDict '%s'", fmt
);
96 qerr
->error
= qobject_to_qdict(obj
);
98 obj
= qdict_get(qerr
->error
, "class");
100 qerror_abort(qerr
, "missing 'class' key in '%s'", fmt
);
102 if (qobject_type(obj
) != QTYPE_QSTRING
) {
103 qerror_abort(qerr
, "'class' key value should be a QString");
106 obj
= qdict_get(qerr
->error
, "data");
108 qerror_abort(qerr
, "missing 'data' key in '%s'", fmt
);
110 if (qobject_type(obj
) != QTYPE_QDICT
) {
111 qerror_abort(qerr
, "'data' key value should be a QDICT");
115 static void qerror_set_desc(QError
*qerr
, const char *fmt
)
119 // FIXME: inefficient loop
121 for (i
= 0; qerror_table
[i
].error_fmt
; i
++) {
122 if (strcmp(qerror_table
[i
].error_fmt
, fmt
) == 0) {
123 qerr
->entry
= &qerror_table
[i
];
128 qerror_abort(qerr
, "error format '%s' not found", fmt
);
132 * qerror_from_info(): Create a new QError from error information
134 * The information consists of:
136 * - file the file name of where the error occurred
137 * - linenr the line number of where the error occurred
138 * - func the function name of where the error occurred
139 * - fmt JSON printf-like dictionary, there must exist keys 'class' and
141 * - va va_list of all arguments specified by fmt
143 * Return strong reference.
145 QError
*qerror_from_info(const char *file
, int linenr
, const char *func
,
146 const char *fmt
, va_list *va
)
151 qerr
->linenr
= linenr
;
156 qerror_abort(qerr
, "QDict not specified");
159 qerror_set_data(qerr
, fmt
, va
);
160 qerror_set_desc(qerr
, fmt
);
165 static void parse_error(const QError
*qerror
, int c
)
167 qerror_abort(qerror
, "expected '%c' in '%s'", c
, qerror
->entry
->desc
);
170 static const char *append_field(QString
*outstr
, const QError
*qerror
,
176 const char *end
, *key
;
179 parse_error(qerror
, '%');
182 parse_error(qerror
, '(');
185 end
= strchr(start
, ')');
187 parse_error(qerror
, ')');
189 key_qs
= qstring_from_substr(start
, 0, end
- start
- 1);
190 key
= qstring_get_str(key_qs
);
192 qdict
= qobject_to_qdict(qdict_get(qerror
->error
, "data"));
193 obj
= qdict_get(qdict
, key
);
195 qerror_abort(qerror
, "key '%s' not found in QDict", key
);
198 switch (qobject_type(obj
)) {
200 qstring_append(outstr
, qdict_get_str(qdict
, key
));
203 qstring_append_int(outstr
, qdict_get_int(qdict
, key
));
206 qerror_abort(qerror
, "invalid type '%c'", qobject_type(obj
));
214 * qerror_print(): Print QError data
216 * This function will print the member 'desc' of the specified QError object,
217 * it uses qemu_error() for this, so that the output is routed to the right
218 * place (ie. stderr or Monitor's device).
220 void qerror_print(const QError
*qerror
)
225 assert(qerror
->entry
!= NULL
);
227 qstring
= qstring_new();
229 for (p
= qerror
->entry
->desc
; *p
!= '\0';) {
231 qstring_append_chr(qstring
, *p
++);
232 } else if (*(p
+ 1) == '%') {
233 qstring_append_chr(qstring
, '%');
236 p
= append_field(qstring
, qerror
, p
);
240 qemu_error("%s\n", qstring_get_str(qstring
));
245 * qobject_to_qerror(): Convert a QObject into a QError
247 QError
*qobject_to_qerror(const QObject
*obj
)
249 if (qobject_type(obj
) != QTYPE_QERROR
) {
253 return container_of(obj
, QError
, base
);
257 * qerror_destroy_obj(): Free all memory allocated by a QError
259 static void qerror_destroy_obj(QObject
*obj
)
264 qerr
= qobject_to_qerror(obj
);
266 QDECREF(qerr
->error
);