File:  [Qemu by Fabrice Bellard] / qemu / error.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 19:17:30 2018 UTC (3 years, 1 month ago) by root
Branches: qemu, MAIN
CVS tags: qemu1001, HEAD
qemu 1.0.1

    1: /*
    2:  * QEMU Error Objects
    3:  *
    4:  * Copyright IBM, Corp. 2011
    5:  *
    6:  * Authors:
    7:  *  Anthony Liguori   <aliguori@us.ibm.com>
    8:  *
    9:  * This work is licensed under the terms of the GNU LGPL, version 2.  See
   10:  * the COPYING.LIB file in the top-level directory.
   11:  */
   12: 
   13: #include "qemu-common.h"
   14: #include "error.h"
   15: #include "qjson.h"
   16: #include "qdict.h"
   17: #include "error_int.h"
   18: #include "qerror.h"
   19: 
   20: struct Error
   21: {
   22:     QDict *obj;
   23:     const char *fmt;
   24:     char *msg;
   25: };
   26: 
   27: void error_set(Error **errp, const char *fmt, ...)
   28: {
   29:     Error *err;
   30:     va_list ap;
   31: 
   32:     if (errp == NULL) {
   33:         return;
   34:     }
   35: 
   36:     err = g_malloc0(sizeof(*err));
   37: 
   38:     va_start(ap, fmt);
   39:     err->obj = qobject_to_qdict(qobject_from_jsonv(fmt, &ap));
   40:     va_end(ap);
   41:     err->fmt = fmt;
   42: 
   43:     *errp = err;
   44: }
   45: 
   46: bool error_is_set(Error **errp)
   47: {
   48:     return (errp && *errp);
   49: }
   50: 
   51: const char *error_get_pretty(Error *err)
   52: {
   53:     if (err->msg == NULL) {
   54:         QString *str;
   55:         str = qerror_format(err->fmt, err->obj);
   56:         err->msg = g_strdup(qstring_get_str(str));
   57:         QDECREF(str);
   58:     }
   59: 
   60:     return err->msg;
   61: }
   62: 
   63: const char *error_get_field(Error *err, const char *field)
   64: {
   65:     if (strcmp(field, "class") == 0) {
   66:         return qdict_get_str(err->obj, field);
   67:     } else {
   68:         QDict *dict = qdict_get_qdict(err->obj, "data");
   69:         return qdict_get_str(dict, field);
   70:     }
   71: }
   72: 
   73: QDict *error_get_data(Error *err)
   74: {
   75:     QDict *data = qdict_get_qdict(err->obj, "data");
   76:     QINCREF(data);
   77:     return data;
   78: }
   79: 
   80: void error_set_field(Error *err, const char *field, const char *value)
   81: {
   82:     QDict *dict = qdict_get_qdict(err->obj, "data");
   83:     return qdict_put(dict, field, qstring_from_str(value));
   84: }
   85: 
   86: void error_free(Error *err)
   87: {
   88:     if (err) {
   89:         QDECREF(err->obj);
   90:         g_free(err->msg);
   91:         g_free(err);
   92:     }
   93: }
   94: 
   95: bool error_is_type(Error *err, const char *fmt)
   96: {
   97:     const char *error_class;
   98:     char *ptr;
   99:     char *end;
  100: 
  101:     if (!err) {
  102:         return false;
  103:     }
  104: 
  105:     ptr = strstr(fmt, "'class': '");
  106:     assert(ptr != NULL);
  107:     ptr += strlen("'class': '");
  108: 
  109:     end = strchr(ptr, '\'');
  110:     assert(end != NULL);
  111: 
  112:     error_class = error_get_field(err, "class");
  113:     if (strlen(error_class) != end - ptr) {
  114:         return false;
  115:     }
  116: 
  117:     return strncmp(ptr, error_class, end - ptr) == 0;
  118: }
  119: 
  120: void error_propagate(Error **dst_err, Error *local_err)
  121: {
  122:     if (dst_err) {
  123:         *dst_err = local_err;
  124:     } else if (local_err) {
  125:         error_free(local_err);
  126:     }
  127: }
  128: 
  129: QObject *error_get_qobject(Error *err)
  130: {
  131:     QINCREF(err->obj);
  132:     return QOBJECT(err->obj);
  133: }
  134: 
  135: void error_set_qobject(Error **errp, QObject *obj)
  136: {
  137:     Error *err;
  138:     if (errp == NULL) {
  139:         return;
  140:     }
  141:     err = g_malloc0(sizeof(*err));
  142:     err->obj = qobject_to_qdict(obj);
  143:     qobject_incref(obj);
  144: 
  145:     *errp = err;
  146: }

unix.superglobalmegacorp.com