Luiz Capitulino | 9f9daf9 | 2009-11-18 23:05:30 -0200 | [diff] [blame] | 1 | /* |
| 2 | * QError: QEMU Error data-type. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
| 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[] = { |
Luiz Capitulino | 357b615 | 2009-11-18 23:05:32 -0200 | [diff] [blame^] | 43 | { |
| 44 | .error_fmt = QERR_DEVICE_NOT_FOUND, |
| 45 | .desc = "The %(device) device has not been found", |
| 46 | }, |
Luiz Capitulino | 9f9daf9 | 2009-11-18 23:05:30 -0200 | [diff] [blame] | 47 | {} |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * qerror_new(): Create a new QError |
| 52 | * |
| 53 | * Return strong reference. |
| 54 | */ |
| 55 | QError *qerror_new(void) |
| 56 | { |
| 57 | QError *qerr; |
| 58 | |
| 59 | qerr = qemu_mallocz(sizeof(*qerr)); |
| 60 | QOBJECT_INIT(qerr, &qerror_type); |
| 61 | |
| 62 | return qerr; |
| 63 | } |
| 64 | |
| 65 | static void qerror_abort(const QError *qerr, const char *fmt, ...) |
| 66 | { |
| 67 | va_list ap; |
| 68 | |
| 69 | fprintf(stderr, "qerror: bad call in function '%s':\n", qerr->func); |
| 70 | fprintf(stderr, "qerror: -> "); |
| 71 | |
| 72 | va_start(ap, fmt); |
| 73 | vfprintf(stderr, fmt, ap); |
| 74 | va_end(ap); |
| 75 | |
| 76 | fprintf(stderr, "\nqerror: call at %s:%d\n", qerr->file, qerr->linenr); |
| 77 | abort(); |
| 78 | } |
| 79 | |
| 80 | static void qerror_set_data(QError *qerr, const char *fmt, va_list *va) |
| 81 | { |
| 82 | QObject *obj; |
| 83 | |
| 84 | obj = qobject_from_jsonv(fmt, va); |
| 85 | if (!obj) { |
| 86 | qerror_abort(qerr, "invalid format '%s'", fmt); |
| 87 | } |
| 88 | if (qobject_type(obj) != QTYPE_QDICT) { |
| 89 | qerror_abort(qerr, "error format is not a QDict '%s'", fmt); |
| 90 | } |
| 91 | |
| 92 | qerr->error = qobject_to_qdict(obj); |
| 93 | |
| 94 | obj = qdict_get(qerr->error, "class"); |
| 95 | if (!obj) { |
| 96 | qerror_abort(qerr, "missing 'class' key in '%s'", fmt); |
| 97 | } |
| 98 | if (qobject_type(obj) != QTYPE_QSTRING) { |
| 99 | qerror_abort(qerr, "'class' key value should be a QString"); |
| 100 | } |
| 101 | |
| 102 | obj = qdict_get(qerr->error, "data"); |
| 103 | if (!obj) { |
| 104 | qerror_abort(qerr, "missing 'data' key in '%s'", fmt); |
| 105 | } |
| 106 | if (qobject_type(obj) != QTYPE_QDICT) { |
| 107 | qerror_abort(qerr, "'data' key value should be a QDICT"); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static void qerror_set_desc(QError *qerr, const char *fmt) |
| 112 | { |
| 113 | int i; |
| 114 | |
| 115 | // FIXME: inefficient loop |
| 116 | |
| 117 | for (i = 0; qerror_table[i].error_fmt; i++) { |
| 118 | if (strcmp(qerror_table[i].error_fmt, fmt) == 0) { |
| 119 | qerr->entry = &qerror_table[i]; |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | qerror_abort(qerr, "error format '%s' not found", fmt); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * qerror_from_info(): Create a new QError from error information |
| 129 | * |
| 130 | * The information consists of: |
| 131 | * |
| 132 | * - file the file name of where the error occurred |
| 133 | * - linenr the line number of where the error occurred |
| 134 | * - func the function name of where the error occurred |
| 135 | * - fmt JSON printf-like dictionary, there must exist keys 'class' and |
| 136 | * 'data' |
| 137 | * - va va_list of all arguments specified by fmt |
| 138 | * |
| 139 | * Return strong reference. |
| 140 | */ |
| 141 | QError *qerror_from_info(const char *file, int linenr, const char *func, |
| 142 | const char *fmt, va_list *va) |
| 143 | { |
| 144 | QError *qerr; |
| 145 | |
| 146 | qerr = qerror_new(); |
| 147 | qerr->linenr = linenr; |
| 148 | qerr->file = file; |
| 149 | qerr->func = func; |
| 150 | |
| 151 | if (!fmt) { |
| 152 | qerror_abort(qerr, "QDict not specified"); |
| 153 | } |
| 154 | |
| 155 | qerror_set_data(qerr, fmt, va); |
| 156 | qerror_set_desc(qerr, fmt); |
| 157 | |
| 158 | return qerr; |
| 159 | } |
| 160 | |
| 161 | static void parse_error(const QError *qerror, int c) |
| 162 | { |
| 163 | qerror_abort(qerror, "expected '%c' in '%s'", c, qerror->entry->desc); |
| 164 | } |
| 165 | |
| 166 | static const char *append_field(QString *outstr, const QError *qerror, |
| 167 | const char *start) |
| 168 | { |
| 169 | QObject *obj; |
| 170 | QDict *qdict; |
| 171 | QString *key_qs; |
| 172 | const char *end, *key; |
| 173 | |
| 174 | if (*start != '%') |
| 175 | parse_error(qerror, '%'); |
| 176 | start++; |
| 177 | if (*start != '(') |
| 178 | parse_error(qerror, '('); |
| 179 | start++; |
| 180 | |
| 181 | end = strchr(start, ')'); |
| 182 | if (!end) |
| 183 | parse_error(qerror, ')'); |
| 184 | |
| 185 | key_qs = qstring_from_substr(start, 0, end - start - 1); |
| 186 | key = qstring_get_str(key_qs); |
| 187 | |
| 188 | qdict = qobject_to_qdict(qdict_get(qerror->error, "data")); |
| 189 | obj = qdict_get(qdict, key); |
| 190 | if (!obj) { |
| 191 | qerror_abort(qerror, "key '%s' not found in QDict", key); |
| 192 | } |
| 193 | |
| 194 | switch (qobject_type(obj)) { |
| 195 | case QTYPE_QSTRING: |
| 196 | qstring_append(outstr, qdict_get_str(qdict, key)); |
| 197 | break; |
| 198 | case QTYPE_QINT: |
| 199 | qstring_append_int(outstr, qdict_get_int(qdict, key)); |
| 200 | break; |
| 201 | default: |
| 202 | qerror_abort(qerror, "invalid type '%c'", qobject_type(obj)); |
| 203 | } |
| 204 | |
| 205 | QDECREF(key_qs); |
| 206 | return ++end; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * qerror_print(): Print QError data |
| 211 | * |
| 212 | * This function will print the member 'desc' of the specified QError object, |
| 213 | * it uses qemu_error() for this, so that the output is routed to the right |
| 214 | * place (ie. stderr or Monitor's device). |
| 215 | */ |
| 216 | void qerror_print(const QError *qerror) |
| 217 | { |
| 218 | const char *p; |
| 219 | QString *qstring; |
| 220 | |
| 221 | assert(qerror->entry != NULL); |
| 222 | |
| 223 | qstring = qstring_new(); |
| 224 | |
| 225 | for (p = qerror->entry->desc; *p != '\0';) { |
| 226 | if (*p != '%') { |
| 227 | qstring_append_chr(qstring, *p++); |
| 228 | } else if (*(p + 1) == '%') { |
| 229 | qstring_append_chr(qstring, '%'); |
| 230 | p += 2; |
| 231 | } else { |
| 232 | p = append_field(qstring, qerror, p); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | qemu_error("%s\n", qstring_get_str(qstring)); |
| 237 | QDECREF(qstring); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * qobject_to_qerror(): Convert a QObject into a QError |
| 242 | */ |
| 243 | QError *qobject_to_qerror(const QObject *obj) |
| 244 | { |
| 245 | if (qobject_type(obj) != QTYPE_QERROR) { |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | return container_of(obj, QError, base); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * qerror_destroy_obj(): Free all memory allocated by a QError |
| 254 | */ |
| 255 | static void qerror_destroy_obj(QObject *obj) |
| 256 | { |
| 257 | QError *qerr; |
| 258 | |
| 259 | assert(obj != NULL); |
| 260 | qerr = qobject_to_qerror(obj); |
| 261 | |
| 262 | QDECREF(qerr->error); |
| 263 | qemu_free(qerr); |
| 264 | } |