Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / util / qemu-error.c
blob50484a8fbb755ddc73c66dbd0dc4a5db6ceb2e57
1 /*
2 * Error reporting
4 * Copyright (C) 2010 Red Hat Inc.
6 * Authors:
7 * Markus Armbruster <armbru@redhat.com>,
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "monitor/monitor.h"
15 #include "qemu/error-report.h"
18 * @report_type is the type of message: error, warning or
19 * informational.
21 typedef enum {
22 REPORT_TYPE_ERROR,
23 REPORT_TYPE_WARNING,
24 REPORT_TYPE_INFO,
25 } report_type;
27 void error_printf(const char *fmt, ...)
29 va_list ap;
31 va_start(ap, fmt);
32 error_vprintf(fmt, ap);
33 va_end(ap);
36 void error_printf_unless_qmp(const char *fmt, ...)
38 va_list ap;
40 va_start(ap, fmt);
41 error_vprintf_unless_qmp(fmt, ap);
42 va_end(ap);
45 static Location std_loc = {
46 .kind = LOC_NONE
48 static Location *cur_loc = &std_loc;
51 * Push location saved in LOC onto the location stack, return it.
52 * The top of that stack is the current location.
53 * Needs a matching loc_pop().
55 Location *loc_push_restore(Location *loc)
57 assert(!loc->prev);
58 loc->prev = cur_loc;
59 cur_loc = loc;
60 return loc;
64 * Initialize *LOC to "nowhere", push it onto the location stack.
65 * The top of that stack is the current location.
66 * Needs a matching loc_pop().
67 * Return LOC.
69 Location *loc_push_none(Location *loc)
71 loc->kind = LOC_NONE;
72 loc->prev = NULL;
73 return loc_push_restore(loc);
77 * Pop the location stack.
78 * LOC must be the current location, i.e. the top of the stack.
80 Location *loc_pop(Location *loc)
82 assert(cur_loc == loc && loc->prev);
83 cur_loc = loc->prev;
84 loc->prev = NULL;
85 return loc;
89 * Save the current location in LOC, return LOC.
91 Location *loc_save(Location *loc)
93 *loc = *cur_loc;
94 loc->prev = NULL;
95 return loc;
99 * Change the current location to the one saved in LOC.
101 void loc_restore(Location *loc)
103 Location *prev = cur_loc->prev;
104 assert(!loc->prev);
105 *cur_loc = *loc;
106 cur_loc->prev = prev;
110 * Change the current location to "nowhere in particular".
112 void loc_set_none(void)
114 cur_loc->kind = LOC_NONE;
118 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
120 void loc_set_cmdline(char **argv, int idx, int cnt)
122 cur_loc->kind = LOC_CMDLINE;
123 cur_loc->num = cnt;
124 cur_loc->ptr = argv + idx;
128 * Change the current location to file FNAME, line LNO.
130 void loc_set_file(const char *fname, int lno)
132 assert (fname || cur_loc->kind == LOC_FILE);
133 cur_loc->kind = LOC_FILE;
134 cur_loc->num = lno;
135 if (fname) {
136 cur_loc->ptr = fname;
140 static const char *progname;
143 * Set the program name for error_print_loc().
145 void error_set_progname(const char *argv0)
147 const char *p = strrchr(argv0, '/');
148 progname = p ? p + 1 : argv0;
151 const char *error_get_progname(void)
153 return progname;
157 * Print current location to current monitor if we have one, else to stderr.
159 static void print_loc(void)
161 const char *sep = "";
162 int i;
163 const char *const *argp;
165 if (!cur_mon && progname) {
166 fprintf(stderr, "%s:", progname);
167 sep = " ";
169 switch (cur_loc->kind) {
170 case LOC_CMDLINE:
171 argp = cur_loc->ptr;
172 for (i = 0; i < cur_loc->num; i++) {
173 error_printf("%s%s", sep, argp[i]);
174 sep = " ";
176 error_printf(": ");
177 break;
178 case LOC_FILE:
179 error_printf("%s:", (const char *)cur_loc->ptr);
180 if (cur_loc->num) {
181 error_printf("%d:", cur_loc->num);
183 error_printf(" ");
184 break;
185 default:
186 error_printf("%s", sep);
190 bool enable_timestamp_msg;
192 * Print a message to current monitor if we have one, else to stderr.
193 * @report_type is the type of message: error, warning or informational.
194 * Format arguments like vsprintf(). The resulting message should be
195 * a single phrase, with no newline or trailing punctuation.
196 * Prepend the current location and append a newline.
197 * It's wrong to call this in a QMP monitor. Use error_setg() there.
199 static void GCC_FMT_ATTR(2, 0)
200 vreport(report_type type, const char *fmt, va_list ap)
202 GTimeVal tv;
203 gchar *timestr;
205 if (enable_timestamp_msg && !cur_mon) {
206 g_get_current_time(&tv);
207 timestr = g_time_val_to_iso8601(&tv);
208 error_printf("%s ", timestr);
209 g_free(timestr);
212 print_loc();
214 switch (type) {
215 case REPORT_TYPE_ERROR:
216 break;
217 case REPORT_TYPE_WARNING:
218 error_printf("warning: ");
219 break;
220 case REPORT_TYPE_INFO:
221 error_printf("info: ");
222 break;
225 error_vprintf(fmt, ap);
226 error_printf("\n");
230 * Print an error message to current monitor if we have one, else to stderr.
231 * Format arguments like vsprintf(). The resulting message should be
232 * a single phrase, with no newline or trailing punctuation.
233 * Prepend the current location and append a newline.
234 * It's wrong to call this in a QMP monitor. Use error_setg() there.
236 void error_vreport(const char *fmt, va_list ap)
238 vreport(REPORT_TYPE_ERROR, fmt, ap);
242 * Print a warning message to current monitor if we have one, else to stderr.
243 * Format arguments like vsprintf(). The resulting message should be
244 * a single phrase, with no newline or trailing punctuation.
245 * Prepend the current location and append a newline.
246 * It's wrong to call this in a QMP monitor. Use error_setg() there.
248 void warn_vreport(const char *fmt, va_list ap)
250 vreport(REPORT_TYPE_WARNING, fmt, ap);
254 * Print an information message to current monitor if we have one, else to
255 * stderr.
256 * Format arguments like vsprintf(). The resulting message should be
257 * a single phrase, with no newline or trailing punctuation.
258 * Prepend the current location and append a newline.
259 * It's wrong to call this in a QMP monitor. Use error_setg() there.
261 void info_vreport(const char *fmt, va_list ap)
263 vreport(REPORT_TYPE_INFO, fmt, ap);
267 * Print an error message to current monitor if we have one, else to stderr.
268 * Format arguments like sprintf(). The resulting message should be
269 * a single phrase, with no newline or trailing punctuation.
270 * Prepend the current location and append a newline.
271 * It's wrong to call this in a QMP monitor. Use error_setg() there.
273 void error_report(const char *fmt, ...)
275 va_list ap;
277 va_start(ap, fmt);
278 vreport(REPORT_TYPE_ERROR, fmt, ap);
279 va_end(ap);
283 * Print a warning message to current monitor if we have one, else to stderr.
284 * Format arguments like sprintf(). The resulting message should be a
285 * single phrase, with no newline or trailing punctuation.
286 * Prepend the current location and append a newline.
287 * It's wrong to call this in a QMP monitor. Use error_setg() there.
289 void warn_report(const char *fmt, ...)
291 va_list ap;
293 va_start(ap, fmt);
294 vreport(REPORT_TYPE_WARNING, fmt, ap);
295 va_end(ap);
299 * Print an information message to current monitor if we have one, else to
300 * stderr.
301 * Format arguments like sprintf(). The resulting message should be a
302 * single phrase, with no newline or trailing punctuation.
303 * Prepend the current location and append a newline.
304 * It's wrong to call this in a QMP monitor. Use error_setg() there.
306 void info_report(const char *fmt, ...)
308 va_list ap;
310 va_start(ap, fmt);
311 vreport(REPORT_TYPE_INFO, fmt, ap);
312 va_end(ap);