root / qobject / qerror.c @ feature-archipelago
History | View | Annotate | Download (2.9 kB)
1 | 9f9daf9a | Luiz Capitulino | /*
|
---|---|---|---|
2 | 41836a9f | Luiz Capitulino | * QError Module
|
3 | 9f9daf9a | Luiz Capitulino | *
|
4 | 9f9daf9a | Luiz Capitulino | * Copyright (C) 2009 Red Hat Inc.
|
5 | 9f9daf9a | Luiz Capitulino | *
|
6 | 9f9daf9a | Luiz Capitulino | * Authors:
|
7 | 9f9daf9a | Luiz Capitulino | * Luiz Capitulino <lcapitulino@redhat.com>
|
8 | 9f9daf9a | Luiz Capitulino | *
|
9 | 9f9daf9a | Luiz Capitulino | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
10 | 9f9daf9a | Luiz Capitulino | * See the COPYING.LIB file in the top-level directory.
|
11 | 9f9daf9a | Luiz Capitulino | */
|
12 | 0167f772 | Markus Armbruster | |
13 | 83c9089e | Paolo Bonzini | #include "monitor/monitor.h" |
14 | 7b1b5d19 | Paolo Bonzini | #include "qapi/qmp/qjson.h" |
15 | 7b1b5d19 | Paolo Bonzini | #include "qapi/qmp/qerror.h" |
16 | 9f9daf9a | Luiz Capitulino | #include "qemu-common.h" |
17 | 9f9daf9a | Luiz Capitulino | |
18 | 9f9daf9a | Luiz Capitulino | static void qerror_destroy_obj(QObject *obj); |
19 | 9f9daf9a | Luiz Capitulino | |
20 | 9f9daf9a | Luiz Capitulino | static const QType qerror_type = { |
21 | 9f9daf9a | Luiz Capitulino | .code = QTYPE_QERROR, |
22 | 9f9daf9a | Luiz Capitulino | .destroy = qerror_destroy_obj, |
23 | 9f9daf9a | Luiz Capitulino | }; |
24 | 9f9daf9a | Luiz Capitulino | |
25 | 9f9daf9a | Luiz Capitulino | /**
|
26 | 9f9daf9a | Luiz Capitulino | * qerror_new(): Create a new QError
|
27 | 9f9daf9a | Luiz Capitulino | *
|
28 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
29 | 9f9daf9a | Luiz Capitulino | */
|
30 | 2a744405 | Luiz Capitulino | static QError *qerror_new(void) |
31 | 9f9daf9a | Luiz Capitulino | { |
32 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
33 | 9f9daf9a | Luiz Capitulino | |
34 | 7267c094 | Anthony Liguori | qerr = g_malloc0(sizeof(*qerr));
|
35 | 9f9daf9a | Luiz Capitulino | QOBJECT_INIT(qerr, &qerror_type); |
36 | 9f9daf9a | Luiz Capitulino | |
37 | 9f9daf9a | Luiz Capitulino | return qerr;
|
38 | 9f9daf9a | Luiz Capitulino | } |
39 | 9f9daf9a | Luiz Capitulino | |
40 | 9f9daf9a | Luiz Capitulino | /**
|
41 | 9f9daf9a | Luiz Capitulino | * qerror_from_info(): Create a new QError from error information
|
42 | 9f9daf9a | Luiz Capitulino | *
|
43 | 9f9daf9a | Luiz Capitulino | * Return strong reference.
|
44 | 9f9daf9a | Luiz Capitulino | */
|
45 | 47908a0f | Stefan Weil | static QError * GCC_FMT_ATTR(2, 0) |
46 | 47908a0f | Stefan Weil | qerror_from_info(ErrorClass err_class, const char *fmt, va_list *va) |
47 | 9f9daf9a | Luiz Capitulino | { |
48 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
49 | 9f9daf9a | Luiz Capitulino | |
50 | 9f9daf9a | Luiz Capitulino | qerr = qerror_new(); |
51 | 827b0813 | Markus Armbruster | loc_save(&qerr->loc); |
52 | 9f9daf9a | Luiz Capitulino | |
53 | df1e608a | Luiz Capitulino | qerr->err_msg = g_strdup_vprintf(fmt, *va); |
54 | 13f59ae8 | Luiz Capitulino | qerr->err_class = err_class; |
55 | 9f9daf9a | Luiz Capitulino | |
56 | 9f9daf9a | Luiz Capitulino | return qerr;
|
57 | 9f9daf9a | Luiz Capitulino | } |
58 | 9f9daf9a | Luiz Capitulino | |
59 | 77e595e7 | Markus Armbruster | /**
|
60 | a12eeaaa | Luiz Capitulino | * qerror_human(): Format QError data into human-readable string.
|
61 | a12eeaaa | Luiz Capitulino | */
|
62 | a12eeaaa | Luiz Capitulino | QString *qerror_human(const QError *qerror)
|
63 | a12eeaaa | Luiz Capitulino | { |
64 | 18da7c0f | Luiz Capitulino | return qstring_from_str(qerror->err_msg);
|
65 | a12eeaaa | Luiz Capitulino | } |
66 | a12eeaaa | Luiz Capitulino | |
67 | a12eeaaa | Luiz Capitulino | /**
|
68 | 77e595e7 | Markus Armbruster | * qerror_print(): Print QError data
|
69 | 77e595e7 | Markus Armbruster | *
|
70 | 77e595e7 | Markus Armbruster | * This function will print the member 'desc' of the specified QError object,
|
71 | 1ecda02b | Markus Armbruster | * it uses error_report() for this, so that the output is routed to the right
|
72 | 77e595e7 | Markus Armbruster | * place (ie. stderr or Monitor's device).
|
73 | 77e595e7 | Markus Armbruster | */
|
74 | 2a744405 | Luiz Capitulino | static void qerror_print(QError *qerror) |
75 | 77e595e7 | Markus Armbruster | { |
76 | 77e595e7 | Markus Armbruster | QString *qstring = qerror_human(qerror); |
77 | 827b0813 | Markus Armbruster | loc_push_restore(&qerror->loc); |
78 | 1ecda02b | Markus Armbruster | error_report("%s", qstring_get_str(qstring));
|
79 | 827b0813 | Markus Armbruster | loc_pop(&qerror->loc); |
80 | 9f9daf9a | Luiz Capitulino | QDECREF(qstring); |
81 | 9f9daf9a | Luiz Capitulino | } |
82 | 9f9daf9a | Luiz Capitulino | |
83 | 13f59ae8 | Luiz Capitulino | void qerror_report(ErrorClass eclass, const char *fmt, ...) |
84 | 0167f772 | Markus Armbruster | { |
85 | 0167f772 | Markus Armbruster | va_list va; |
86 | 0167f772 | Markus Armbruster | QError *qerror; |
87 | 0167f772 | Markus Armbruster | |
88 | 0167f772 | Markus Armbruster | va_start(va, fmt); |
89 | 13f59ae8 | Luiz Capitulino | qerror = qerror_from_info(eclass, fmt, &va); |
90 | 0167f772 | Markus Armbruster | va_end(va); |
91 | 0167f772 | Markus Armbruster | |
92 | 0167f772 | Markus Armbruster | if (monitor_cur_is_qmp()) {
|
93 | 0167f772 | Markus Armbruster | monitor_set_error(cur_mon, qerror); |
94 | 0167f772 | Markus Armbruster | } else {
|
95 | 0167f772 | Markus Armbruster | qerror_print(qerror); |
96 | 0167f772 | Markus Armbruster | QDECREF(qerror); |
97 | 0167f772 | Markus Armbruster | } |
98 | 0167f772 | Markus Armbruster | } |
99 | 0167f772 | Markus Armbruster | |
100 | 2a82d936 | Anthony Liguori | /* Evil... */
|
101 | 2a82d936 | Anthony Liguori | struct Error
|
102 | 2a82d936 | Anthony Liguori | { |
103 | 2a82d936 | Anthony Liguori | char *msg;
|
104 | 13f59ae8 | Luiz Capitulino | ErrorClass err_class; |
105 | 2a82d936 | Anthony Liguori | }; |
106 | 2a82d936 | Anthony Liguori | |
107 | 2a82d936 | Anthony Liguori | void qerror_report_err(Error *err)
|
108 | 2a82d936 | Anthony Liguori | { |
109 | 2a82d936 | Anthony Liguori | QError *qerr; |
110 | 2a82d936 | Anthony Liguori | |
111 | 2a82d936 | Anthony Liguori | qerr = qerror_new(); |
112 | 2a82d936 | Anthony Liguori | loc_save(&qerr->loc); |
113 | dd7520f0 | Luiz Capitulino | qerr->err_msg = g_strdup(err->msg); |
114 | 13f59ae8 | Luiz Capitulino | qerr->err_class = err->err_class; |
115 | 2a82d936 | Anthony Liguori | |
116 | 2a82d936 | Anthony Liguori | if (monitor_cur_is_qmp()) {
|
117 | 2a82d936 | Anthony Liguori | monitor_set_error(cur_mon, qerr); |
118 | 2a82d936 | Anthony Liguori | } else {
|
119 | 2a82d936 | Anthony Liguori | qerror_print(qerr); |
120 | 2a82d936 | Anthony Liguori | QDECREF(qerr); |
121 | 2a82d936 | Anthony Liguori | } |
122 | 2a82d936 | Anthony Liguori | } |
123 | 2a82d936 | Anthony Liguori | |
124 | 9f9daf9a | Luiz Capitulino | /**
|
125 | 9f9daf9a | Luiz Capitulino | * qobject_to_qerror(): Convert a QObject into a QError
|
126 | 9f9daf9a | Luiz Capitulino | */
|
127 | 2a744405 | Luiz Capitulino | static QError *qobject_to_qerror(const QObject *obj) |
128 | 9f9daf9a | Luiz Capitulino | { |
129 | 9f9daf9a | Luiz Capitulino | if (qobject_type(obj) != QTYPE_QERROR) {
|
130 | 9f9daf9a | Luiz Capitulino | return NULL; |
131 | 9f9daf9a | Luiz Capitulino | } |
132 | 9f9daf9a | Luiz Capitulino | |
133 | 9f9daf9a | Luiz Capitulino | return container_of(obj, QError, base);
|
134 | 9f9daf9a | Luiz Capitulino | } |
135 | 9f9daf9a | Luiz Capitulino | |
136 | 9f9daf9a | Luiz Capitulino | /**
|
137 | 9f9daf9a | Luiz Capitulino | * qerror_destroy_obj(): Free all memory allocated by a QError
|
138 | 9f9daf9a | Luiz Capitulino | */
|
139 | 9f9daf9a | Luiz Capitulino | static void qerror_destroy_obj(QObject *obj) |
140 | 9f9daf9a | Luiz Capitulino | { |
141 | 9f9daf9a | Luiz Capitulino | QError *qerr; |
142 | 9f9daf9a | Luiz Capitulino | |
143 | 9f9daf9a | Luiz Capitulino | assert(obj != NULL);
|
144 | 9f9daf9a | Luiz Capitulino | qerr = qobject_to_qerror(obj); |
145 | 9f9daf9a | Luiz Capitulino | |
146 | 18da7c0f | Luiz Capitulino | g_free(qerr->err_msg); |
147 | 7267c094 | Anthony Liguori | g_free(qerr); |
148 | 9f9daf9a | Luiz Capitulino | } |