]>
Commit | Line | Data |
---|---|---|
9f9daf9a LC |
1 | /* |
2 | * QError: QEMU Error data-type. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <[email protected]> | |
8 | * | |
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. | |
11 | */ | |
12 | #include "qjson.h" | |
13 | #include "qerror.h" | |
14 | #include "qstring.h" | |
15 | #include "sysemu.h" | |
16 | #include "qemu-common.h" | |
17 | ||
18 | static void qerror_destroy_obj(QObject *obj); | |
19 | ||
20 | static const QType qerror_type = { | |
21 | .code = QTYPE_QERROR, | |
22 | .destroy = qerror_destroy_obj, | |
23 | }; | |
24 | ||
25 | /** | |
26 | * The 'desc' parameter is a printf-like string, the format of the format | |
27 | * string is: | |
28 | * | |
29 | * %(KEY) | |
30 | * | |
31 | * Where KEY is a QDict key, which has to be passed to qerror_from_info(). | |
32 | * | |
33 | * Example: | |
34 | * | |
35 | * "foo error on device: %(device) slot: %(slot_nr)" | |
36 | * | |
37 | * A single percent sign can be printed if followed by a second one, | |
38 | * for example: | |
39 | * | |
40 | * "running out of foo: %(foo)%%" | |
41 | */ | |
42 | const QErrorStringTable qerror_table[] = { | |
43 | {} | |
44 | }; | |
45 | ||
46 | /** | |
47 | * qerror_new(): Create a new QError | |
48 | * | |
49 | * Return strong reference. | |
50 | */ | |
51 | QError *qerror_new(void) | |
52 | { | |
53 | QError *qerr; | |
54 | ||
55 | qerr = qemu_mallocz(sizeof(*qerr)); | |
56 | QOBJECT_INIT(qerr, &qerror_type); | |
57 | ||
58 | return qerr; | |
59 | } | |
60 | ||
61 | static void qerror_abort(const QError *qerr, const char *fmt, ...) | |
62 | { | |
63 | va_list ap; | |
64 | ||
65 | fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func); | |
66 | fprintf(stderr, "qerror: -> "); | |
67 | ||
68 | va_start(ap, fmt); | |
69 | vfprintf(stderr, fmt, ap); | |
70 | va_end(ap); | |
71 | ||
72 | fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr); | |
73 | abort(); | |
74 | } | |
75 | ||
76 | static void qerror_set_data(QError *qerr, const char *fmt, va_list *va) | |
77 | { | |
78 | QObject *obj; | |
79 | ||
80 | obj = qobject_from_jsonv(fmt, va); | |
81 | if (!obj) { | |
82 | qerror_abort(qerr, "invalid format '%s'", fmt); | |
83 | } | |
84 | if (qobject_type(obj) != QTYPE_QDICT) { | |
85 | qerror_abort(qerr, "error format is not a QDict '%s'", fmt); | |
86 | } | |
87 | ||
88 | qerr->error = qobject_to_qdict(obj); | |
89 | ||
90 | obj = qdict_get(qerr->error, "class"); | |
91 | if (!obj) { | |
92 | qerror_abort(qerr, "missing 'class' key in '%s'", fmt); | |
93 | } | |
94 | if (qobject_type(obj) != QTYPE_QSTRING) { | |
95 | qerror_abort(qerr, "'class' key value should be a QString"); | |
96 | } | |
97 | ||
98 | obj = qdict_get(qerr->error, "data"); | |
99 | if (!obj) { | |
100 | qerror_abort(qerr, "missing 'data' key in '%s'", fmt); | |
101 | } | |
102 | if (qobject_type(obj) != QTYPE_QDICT) { | |
103 | qerror_abort(qerr, "'data' key value should be a QDICT"); | |
104 | } | |
105 | } | |
106 | ||
107 | static void qerror_set_desc(QError *qerr, const char *fmt) | |
108 | { | |
109 | int i; | |
110 | ||
111 | // FIXME: inefficient loop | |
112 | ||
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]; | |
116 | return; | |
117 | } | |
118 | } | |
119 | ||
120 | qerror_abort(qerr, "error format '%s' not found", fmt); | |
121 | } | |
122 | ||
123 | /** | |
124 | * qerror_from_info(): Create a new QError from error information | |
125 | * | |
126 | * The information consists of: | |
127 | * | |
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 | |
132 | * 'data' | |
133 | * - va va_list of all arguments specified by fmt | |
134 | * | |
135 | * Return strong reference. | |
136 | */ | |
137 | QError *qerror_from_info(const char *file, int linenr, const char *func, | |
138 | const char *fmt, va_list *va) | |
139 | { | |
140 | QError *qerr; | |
141 | ||
142 | qerr = qerror_new(); | |
143 | qerr->linenr = linenr; | |
144 | qerr->file = file; | |
145 | qerr->func = func; | |
146 | ||
147 | if (!fmt) { | |
148 | qerror_abort(qerr, "QDict not specified"); | |
149 | } | |
150 | ||
151 | qerror_set_data(qerr, fmt, va); | |
152 | qerror_set_desc(qerr, fmt); | |
153 | ||
154 | return qerr; | |
155 | } | |
156 | ||
157 | static void parse_error(const QError *qerror, int c) | |
158 | { | |
159 | qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc); | |
160 | } | |
161 | ||
162 | static const char *append_field(QString *outstr, const QError *qerror, | |
163 | const char *start) | |
164 | { | |
165 | QObject *obj; | |
166 | QDict *qdict; | |
167 | QString *key_qs; | |
168 | const char *end, *key; | |
169 | ||
170 | if (*start != '%') | |
171 | parse_error(qerror, '%'); | |
172 | start++; | |
173 | if (*start != '(') | |
174 | parse_error(qerror, '('); | |
175 | start++; | |
176 | ||
177 | end = strchr(start, ')'); | |
178 | if (!end) | |
179 | parse_error(qerror, ')'); | |
180 | ||
181 | key_qs = qstring_from_substr(start, 0, end - start - 1); | |
182 | key = qstring_get_str(key_qs); | |
183 | ||
184 | qdict = qobject_to_qdict(qdict_get(qerror->error, "data")); | |
185 | obj = qdict_get(qdict, key); | |
186 | if (!obj) { | |
187 | qerror_abort(qerror, "key '%s' not found in QDict", key); | |
188 | } | |
189 | ||
190 | switch (qobject_type(obj)) { | |
191 | case QTYPE_QSTRING: | |
192 | qstring_append(outstr, qdict_get_str(qdict, key)); | |
193 | break; | |
194 | case QTYPE_QINT: | |
195 | qstring_append_int(outstr, qdict_get_int(qdict, key)); | |
196 | break; | |
197 | default: | |
198 | qerror_abort(qerror, "invalid type '%c'", qobject_type(obj)); | |
199 | } | |
200 | ||
201 | QDECREF(key_qs); | |
202 | return ++end; | |
203 | } | |
204 | ||
205 | /** | |
206 | * qerror_print(): Print QError data | |
207 | * | |
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). | |
211 | */ | |
212 | void qerror_print(const QError *qerror) | |
213 | { | |
214 | const char *p; | |
215 | QString *qstring; | |
216 | ||
217 | assert(qerror->entry != NULL); | |
218 | ||
219 | qstring = qstring_new(); | |
220 | ||
221 | for (p = qerror->entry->desc; *p != '\0';) { | |
222 | if (*p != '%') { | |
223 | qstring_append_chr(qstring, *p++); | |
224 | } else if (*(p + 1) == '%') { | |
225 | qstring_append_chr(qstring, '%'); | |
226 | p += 2; | |
227 | } else { | |
228 | p = append_field(qstring, qerror, p); | |
229 | } | |
230 | } | |
231 | ||
232 | qemu_error("%s\n", qstring_get_str(qstring)); | |
233 | QDECREF(qstring); | |
234 | } | |
235 | ||
236 | /** | |
237 | * qobject_to_qerror(): Convert a QObject into a QError | |
238 | */ | |
239 | QError *qobject_to_qerror(const QObject *obj) | |
240 | { | |
241 | if (qobject_type(obj) != QTYPE_QERROR) { | |
242 | return NULL; | |
243 | } | |
244 | ||
245 | return container_of(obj, QError, base); | |
246 | } | |
247 | ||
248 | /** | |
249 | * qerror_destroy_obj(): Free all memory allocated by a QError | |
250 | */ | |
251 | static void qerror_destroy_obj(QObject *obj) | |
252 | { | |
253 | QError *qerr; | |
254 | ||
255 | assert(obj != NULL); | |
256 | qerr = qobject_to_qerror(obj); | |
257 | ||
258 | QDECREF(qerr->error); | |
259 | qemu_free(qerr); | |
260 | } |