4 * Copyright (C) 2010 Red Hat Inc.
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
27 void error_printf(const char *fmt
, ...)
32 error_vprintf(fmt
, ap
);
36 void error_printf_unless_qmp(const char *fmt
, ...)
41 error_vprintf_unless_qmp(fmt
, ap
);
45 static Location std_loc
= {
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
)
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().
69 Location
*loc_push_none(Location
*loc
)
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
);
89 * Save the current location in LOC, return LOC.
91 Location
*loc_save(Location
*loc
)
99 * Change the current location to the one saved in LOC.
101 void loc_restore(Location
*loc
)
103 Location
*prev
= cur_loc
->prev
;
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
;
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
;
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)
157 * Print current location to current monitor if we have one, else to stderr.
159 static void print_loc(void)
161 const char *sep
= "";
163 const char *const *argp
;
165 if (!cur_mon
&& progname
) {
166 fprintf(stderr
, "%s:", progname
);
169 switch (cur_loc
->kind
) {
172 for (i
= 0; i
< cur_loc
->num
; i
++) {
173 error_printf("%s%s", sep
, argp
[i
]);
179 error_printf("%s:", (const char *)cur_loc
->ptr
);
181 error_printf("%d:", cur_loc
->num
);
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.
198 static void vreport(report_type type
, const char *fmt
, va_list ap
)
203 if (enable_timestamp_msg
&& !cur_mon
) {
204 g_get_current_time(&tv
);
205 timestr
= g_time_val_to_iso8601(&tv
);
206 error_printf("%s ", timestr
);
213 case REPORT_TYPE_ERROR
:
215 case REPORT_TYPE_WARNING
:
216 error_printf("warning: ");
218 case REPORT_TYPE_INFO
:
219 error_printf("info: ");
223 error_vprintf(fmt
, ap
);
228 * Print an error message to current monitor if we have one, else to stderr.
229 * Format arguments like vsprintf(). The resulting message should be
230 * a single phrase, with no newline or trailing punctuation.
231 * Prepend the current location and append a newline.
232 * It's wrong to call this in a QMP monitor. Use error_setg() there.
234 void error_vreport(const char *fmt
, va_list ap
)
236 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
240 * Print a warning message to current monitor if we have one, else to stderr.
241 * Format arguments like vsprintf(). The resulting message should be
242 * a single phrase, with no newline or trailing punctuation.
243 * Prepend the current location and append a newline.
245 void warn_vreport(const char *fmt
, va_list ap
)
247 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);
251 * Print an information message to current monitor if we have one, else to
253 * Format arguments like vsprintf(). The resulting message should be
254 * a single phrase, with no newline or trailing punctuation.
255 * Prepend the current location and append a newline.
257 void info_vreport(const char *fmt
, va_list ap
)
259 vreport(REPORT_TYPE_INFO
, fmt
, ap
);
263 * Print an error message to current monitor if we have one, else to stderr.
264 * Format arguments like sprintf(). The resulting message should be
265 * a single phrase, with no newline or trailing punctuation.
266 * Prepend the current location and append a newline.
267 * It's wrong to call this in a QMP monitor. Use error_setg() there.
269 void error_report(const char *fmt
, ...)
274 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
279 * Print a warning message to current monitor if we have one, else to stderr.
280 * Format arguments like sprintf(). The resulting message should be a
281 * single phrase, with no newline or trailing punctuation.
282 * Prepend the current location and append a newline.
284 void warn_report(const char *fmt
, ...)
289 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);
294 * Print an information message to current monitor if we have one, else to
296 * Format arguments like sprintf(). The resulting message should be a
297 * single phrase, with no newline or trailing punctuation.
298 * Prepend the current location and append a newline.
300 void info_report(const char *fmt
, ...)
305 vreport(REPORT_TYPE_INFO
, fmt
, ap
);
310 * Like error_report(), except print just once.
311 * If *printed is false, print the message, and flip *printed to true.
312 * Return whether the message was printed.
314 bool error_report_once_cond(bool *printed
, const char *fmt
, ...)
324 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
330 * Like warn_report(), except print just once.
331 * If *printed is false, print the message, and flip *printed to true.
332 * Return whether the message was printed.
334 bool warn_report_once_cond(bool *printed
, const char *fmt
, ...)
344 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);