translate-all: use glib for all page descriptor allocations
[qemu/ar7.git] / util / error.c
blob14f43518792e3debe3381bf73ab035663fded4e2
1 /*
2 * QEMU Error Objects
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
13 #include "qemu-common.h"
14 #include "qapi/error.h"
15 #include "qemu/error-report.h"
17 struct Error
19 char *msg;
20 ErrorClass err_class;
23 Error *error_abort;
25 void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
27 Error *err;
28 va_list ap;
29 int saved_errno = errno;
31 if (errp == NULL) {
32 return;
34 assert(*errp == NULL);
36 err = g_malloc0(sizeof(*err));
38 va_start(ap, fmt);
39 err->msg = g_strdup_vprintf(fmt, ap);
40 va_end(ap);
41 err->err_class = err_class;
43 if (errp == &error_abort) {
44 error_report_err(err);
45 abort();
48 *errp = err;
50 errno = saved_errno;
53 void error_set_errno(Error **errp, int os_errno, ErrorClass err_class,
54 const char *fmt, ...)
56 Error *err;
57 char *msg1;
58 va_list ap;
59 int saved_errno = errno;
61 if (errp == NULL) {
62 return;
64 assert(*errp == NULL);
66 err = g_malloc0(sizeof(*err));
68 va_start(ap, fmt);
69 msg1 = g_strdup_vprintf(fmt, ap);
70 if (os_errno != 0) {
71 err->msg = g_strdup_printf("%s: %s", msg1, strerror(os_errno));
72 g_free(msg1);
73 } else {
74 err->msg = msg1;
76 va_end(ap);
77 err->err_class = err_class;
79 if (errp == &error_abort) {
80 error_report_err(err);
81 abort();
84 *errp = err;
86 errno = saved_errno;
89 void error_setg_file_open(Error **errp, int os_errno, const char *filename)
91 error_setg_errno(errp, os_errno, "Could not open '%s'", filename);
94 #ifdef _WIN32
96 void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
97 const char *fmt, ...)
99 Error *err;
100 char *msg1;
101 va_list ap;
103 if (errp == NULL) {
104 return;
106 assert(*errp == NULL);
108 err = g_malloc0(sizeof(*err));
110 va_start(ap, fmt);
111 msg1 = g_strdup_vprintf(fmt, ap);
112 if (win32_err != 0) {
113 char *msg2 = g_win32_error_message(win32_err);
114 err->msg = g_strdup_printf("%s: %s (error: %x)", msg1, msg2,
115 (unsigned)win32_err);
116 g_free(msg2);
117 g_free(msg1);
118 } else {
119 err->msg = msg1;
121 va_end(ap);
122 err->err_class = err_class;
124 if (errp == &error_abort) {
125 error_report_err(err);
126 abort();
129 *errp = err;
132 #endif
134 Error *error_copy(const Error *err)
136 Error *err_new;
138 err_new = g_malloc0(sizeof(*err));
139 err_new->msg = g_strdup(err->msg);
140 err_new->err_class = err->err_class;
142 return err_new;
145 ErrorClass error_get_class(const Error *err)
147 return err->err_class;
150 const char *error_get_pretty(Error *err)
152 return err->msg;
155 void error_report_err(Error *err)
157 error_report("%s", error_get_pretty(err));
158 error_free(err);
161 void error_free(Error *err)
163 if (err) {
164 g_free(err->msg);
165 g_free(err);
169 void error_propagate(Error **dst_errp, Error *local_err)
171 if (local_err && dst_errp == &error_abort) {
172 error_report_err(local_err);
173 abort();
174 } else if (dst_errp && !*dst_errp) {
175 *dst_errp = local_err;
176 } else if (local_err) {
177 error_free(local_err);