root / error.c @ df1e608a
History | View | Annotate | Download (1.5 kB)
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 "qapi-types.h" |
18 |
#include "qerror.h" |
19 |
|
20 |
struct Error
|
21 |
{ |
22 |
QDict *obj; |
23 |
char *msg;
|
24 |
ErrorClass err_class; |
25 |
}; |
26 |
|
27 |
void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...) |
28 |
{ |
29 |
Error *err; |
30 |
va_list ap; |
31 |
|
32 |
if (errp == NULL) { |
33 |
return;
|
34 |
} |
35 |
assert(*errp == NULL);
|
36 |
|
37 |
err = g_malloc0(sizeof(*err));
|
38 |
|
39 |
va_start(ap, fmt); |
40 |
err->msg = g_strdup_vprintf(fmt, ap); |
41 |
va_end(ap); |
42 |
err->err_class = err_class; |
43 |
|
44 |
*errp = err; |
45 |
} |
46 |
|
47 |
Error *error_copy(const Error *err)
|
48 |
{ |
49 |
Error *err_new; |
50 |
|
51 |
err_new = g_malloc0(sizeof(*err));
|
52 |
err_new->msg = g_strdup(err->msg); |
53 |
err_new->err_class = err->err_class; |
54 |
err_new->obj = err->obj; |
55 |
QINCREF(err_new->obj); |
56 |
|
57 |
return err_new;
|
58 |
} |
59 |
|
60 |
bool error_is_set(Error **errp)
|
61 |
{ |
62 |
return (errp && *errp);
|
63 |
} |
64 |
|
65 |
ErrorClass error_get_class(const Error *err)
|
66 |
{ |
67 |
return err->err_class;
|
68 |
} |
69 |
|
70 |
const char *error_get_pretty(Error *err) |
71 |
{ |
72 |
return err->msg;
|
73 |
} |
74 |
|
75 |
void error_free(Error *err)
|
76 |
{ |
77 |
if (err) {
|
78 |
QDECREF(err->obj); |
79 |
g_free(err->msg); |
80 |
g_free(err); |
81 |
} |
82 |
} |
83 |
|
84 |
void error_propagate(Error **dst_err, Error *local_err)
|
85 |
{ |
86 |
if (dst_err && !*dst_err) {
|
87 |
*dst_err = local_err; |
88 |
} else if (local_err) { |
89 |
error_free(local_err); |
90 |
} |
91 |
} |