2
0

qerror.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * QError Module
  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 "monitor/monitor.h"
  13. #include "qapi/qmp/qjson.h"
  14. #include "qapi/qmp/qerror.h"
  15. #include "qemu-common.h"
  16. static void qerror_destroy_obj(QObject *obj);
  17. static const QType qerror_type = {
  18. .code = QTYPE_QERROR,
  19. .destroy = qerror_destroy_obj,
  20. };
  21. /**
  22. * qerror_new(): Create a new QError
  23. *
  24. * Return strong reference.
  25. */
  26. static QError *qerror_new(void)
  27. {
  28. QError *qerr;
  29. qerr = g_malloc0(sizeof(*qerr));
  30. QOBJECT_INIT(qerr, &qerror_type);
  31. return qerr;
  32. }
  33. /**
  34. * qerror_from_info(): Create a new QError from error information
  35. *
  36. * Return strong reference.
  37. */
  38. static QError *qerror_from_info(ErrorClass err_class, const char *fmt,
  39. va_list *va)
  40. {
  41. QError *qerr;
  42. qerr = qerror_new();
  43. loc_save(&qerr->loc);
  44. qerr->err_msg = g_strdup_vprintf(fmt, *va);
  45. qerr->err_class = err_class;
  46. return qerr;
  47. }
  48. /**
  49. * qerror_human(): Format QError data into human-readable string.
  50. */
  51. QString *qerror_human(const QError *qerror)
  52. {
  53. return qstring_from_str(qerror->err_msg);
  54. }
  55. /**
  56. * qerror_print(): Print QError data
  57. *
  58. * This function will print the member 'desc' of the specified QError object,
  59. * it uses error_report() for this, so that the output is routed to the right
  60. * place (ie. stderr or Monitor's device).
  61. */
  62. static void qerror_print(QError *qerror)
  63. {
  64. QString *qstring = qerror_human(qerror);
  65. loc_push_restore(&qerror->loc);
  66. error_report("%s", qstring_get_str(qstring));
  67. loc_pop(&qerror->loc);
  68. QDECREF(qstring);
  69. }
  70. void qerror_report(ErrorClass eclass, const char *fmt, ...)
  71. {
  72. va_list va;
  73. QError *qerror;
  74. va_start(va, fmt);
  75. qerror = qerror_from_info(eclass, fmt, &va);
  76. va_end(va);
  77. if (monitor_cur_is_qmp()) {
  78. monitor_set_error(cur_mon, qerror);
  79. } else {
  80. qerror_print(qerror);
  81. QDECREF(qerror);
  82. }
  83. }
  84. /* Evil... */
  85. struct Error
  86. {
  87. char *msg;
  88. ErrorClass err_class;
  89. };
  90. void qerror_report_err(Error *err)
  91. {
  92. QError *qerr;
  93. qerr = qerror_new();
  94. loc_save(&qerr->loc);
  95. qerr->err_msg = g_strdup(err->msg);
  96. qerr->err_class = err->err_class;
  97. if (monitor_cur_is_qmp()) {
  98. monitor_set_error(cur_mon, qerr);
  99. } else {
  100. qerror_print(qerr);
  101. QDECREF(qerr);
  102. }
  103. }
  104. void assert_no_error(Error *err)
  105. {
  106. if (err) {
  107. qerror_report_err(err);
  108. abort();
  109. }
  110. }
  111. /**
  112. * qobject_to_qerror(): Convert a QObject into a QError
  113. */
  114. static QError *qobject_to_qerror(const QObject *obj)
  115. {
  116. if (qobject_type(obj) != QTYPE_QERROR) {
  117. return NULL;
  118. }
  119. return container_of(obj, QError, base);
  120. }
  121. /**
  122. * qerror_destroy_obj(): Free all memory allocated by a QError
  123. */
  124. static void qerror_destroy_obj(QObject *obj)
  125. {
  126. QError *qerr;
  127. assert(obj != NULL);
  128. qerr = qobject_to_qerror(obj);
  129. g_free(qerr->err_msg);
  130. g_free(qerr);
  131. }