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 /* Prepend timestamp to messages */
28 bool error_with_timestamp
;
30 int error_printf(const char *fmt
, ...)
36 ret
= error_vprintf(fmt
, ap
);
41 int error_printf_unless_qmp(const char *fmt
, ...)
47 ret
= error_vprintf_unless_qmp(fmt
, ap
);
52 static Location std_loc
= {
55 static Location
*cur_loc
= &std_loc
;
58 * Push location saved in LOC onto the location stack, return it.
59 * The top of that stack is the current location.
60 * Needs a matching loc_pop().
62 Location
*loc_push_restore(Location
*loc
)
71 * Initialize *LOC to "nowhere", push it onto the location stack.
72 * The top of that stack is the current location.
73 * Needs a matching loc_pop().
76 Location
*loc_push_none(Location
*loc
)
80 return loc_push_restore(loc
);
84 * Pop the location stack.
85 * LOC must be the current location, i.e. the top of the stack.
87 Location
*loc_pop(Location
*loc
)
89 assert(cur_loc
== loc
&& loc
->prev
);
96 * Save the current location in LOC, return LOC.
98 Location
*loc_save(Location
*loc
)
106 * Change the current location to the one saved in LOC.
108 void loc_restore(Location
*loc
)
110 Location
*prev
= cur_loc
->prev
;
113 cur_loc
->prev
= prev
;
117 * Change the current location to "nowhere in particular".
119 void loc_set_none(void)
121 cur_loc
->kind
= LOC_NONE
;
125 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
127 void loc_set_cmdline(char **argv
, int idx
, int cnt
)
129 cur_loc
->kind
= LOC_CMDLINE
;
131 cur_loc
->ptr
= argv
+ idx
;
135 * Change the current location to file FNAME, line LNO.
137 void loc_set_file(const char *fname
, int lno
)
139 assert (fname
|| cur_loc
->kind
== LOC_FILE
);
140 cur_loc
->kind
= LOC_FILE
;
143 cur_loc
->ptr
= fname
;
147 static const char *progname
;
150 * Set the program name for error_print_loc().
152 static void error_set_progname(const char *argv0
)
154 const char *p
= strrchr(argv0
, '/');
155 progname
= p
? p
+ 1 : argv0
;
158 const char *error_get_progname(void)
164 * Print current location to current monitor if we have one, else to stderr.
166 static void print_loc(void)
168 const char *sep
= "";
170 const char *const *argp
;
172 if (!cur_mon
&& progname
) {
173 fprintf(stderr
, "%s:", progname
);
176 switch (cur_loc
->kind
) {
179 for (i
= 0; i
< cur_loc
->num
; i
++) {
180 error_printf("%s%s", sep
, argp
[i
]);
186 error_printf("%s:", (const char *)cur_loc
->ptr
);
188 error_printf("%d:", cur_loc
->num
);
193 error_printf("%s", sep
);
198 * Print a message to current monitor if we have one, else to stderr.
199 * @report_type is the type of message: error, warning or informational.
200 * Format arguments like vsprintf(). The resulting message should be
201 * a single phrase, with no newline or trailing punctuation.
202 * Prepend the current location and append a newline.
204 static void vreport(report_type type
, const char *fmt
, va_list ap
)
209 if (error_with_timestamp
&& !cur_mon
) {
210 g_get_current_time(&tv
);
211 timestr
= g_time_val_to_iso8601(&tv
);
212 error_printf("%s ", timestr
);
219 case REPORT_TYPE_ERROR
:
221 case REPORT_TYPE_WARNING
:
222 error_printf("warning: ");
224 case REPORT_TYPE_INFO
:
225 error_printf("info: ");
229 error_vprintf(fmt
, ap
);
234 * Print an error message to current monitor if we have one, else to stderr.
235 * Format arguments like vsprintf(). The resulting message should be
236 * a single phrase, with no newline or trailing punctuation.
237 * Prepend the current location and append a newline.
238 * It's wrong to call this in a QMP monitor. Use error_setg() there.
240 void error_vreport(const char *fmt
, va_list ap
)
242 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
246 * Print a warning message to current monitor if we have one, else to stderr.
247 * Format arguments like vsprintf(). The resulting message should be
248 * a single phrase, with no newline or trailing punctuation.
249 * Prepend the current location and append a newline.
251 void warn_vreport(const char *fmt
, va_list ap
)
253 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);
257 * Print an information message to current monitor if we have one, else to
259 * Format arguments like vsprintf(). The resulting message should be
260 * a single phrase, with no newline or trailing punctuation.
261 * Prepend the current location and append a newline.
263 void info_vreport(const char *fmt
, va_list ap
)
265 vreport(REPORT_TYPE_INFO
, fmt
, ap
);
269 * Print an error message to current monitor if we have one, else to stderr.
270 * Format arguments like sprintf(). The resulting message should be
271 * a single phrase, with no newline or trailing punctuation.
272 * Prepend the current location and append a newline.
273 * It's wrong to call this in a QMP monitor. Use error_setg() there.
275 void error_report(const char *fmt
, ...)
280 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
285 * Print a warning message to current monitor if we have one, else to stderr.
286 * Format arguments like sprintf(). The resulting message should be a
287 * single phrase, with no newline or trailing punctuation.
288 * Prepend the current location and append a newline.
290 void warn_report(const char *fmt
, ...)
295 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);
300 * Print an information message to current monitor if we have one, else to
302 * Format arguments like sprintf(). The resulting message should be a
303 * single phrase, with no newline or trailing punctuation.
304 * Prepend the current location and append a newline.
306 void info_report(const char *fmt
, ...)
311 vreport(REPORT_TYPE_INFO
, fmt
, ap
);
316 * Like error_report(), except print just once.
317 * If *printed is false, print the message, and flip *printed to true.
318 * Return whether the message was printed.
320 bool error_report_once_cond(bool *printed
, const char *fmt
, ...)
330 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
336 * Like warn_report(), except print just once.
337 * If *printed is false, print the message, and flip *printed to true.
338 * Return whether the message was printed.
340 bool warn_report_once_cond(bool *printed
, const char *fmt
, ...)
350 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);
355 static char *qemu_glog_domains
;
357 static void qemu_log_func(const gchar
*log_domain
,
358 GLogLevelFlags log_level
,
359 const gchar
*message
,
362 switch (log_level
& G_LOG_LEVEL_MASK
) {
363 case G_LOG_LEVEL_DEBUG
:
364 case G_LOG_LEVEL_INFO
:
366 * Use same G_MESSAGES_DEBUG logic as glib to enable/disable debug
369 if (qemu_glog_domains
== NULL
) {
372 if (strcmp(qemu_glog_domains
, "all") != 0 &&
373 (log_domain
== NULL
|| !strstr(qemu_glog_domains
, log_domain
))) {
377 case G_LOG_LEVEL_MESSAGE
:
378 info_report("%s%s%s",
379 log_domain
?: "", log_domain
? ": " : "", message
);
382 case G_LOG_LEVEL_WARNING
:
383 warn_report("%s%s%s",
384 log_domain
?: "", log_domain
? ": " : "", message
);
386 case G_LOG_LEVEL_CRITICAL
:
387 case G_LOG_LEVEL_ERROR
:
388 error_report("%s%s%s",
389 log_domain
?: "", log_domain
? ": " : "", message
);
394 void error_init(const char *argv0
)
396 /* Set the program name for error_print_loc(). */
397 error_set_progname(argv0
);
400 * This sets up glib logging so libraries using it also print their logs
401 * through error_report(), warn_report(), info_report().
403 g_log_set_default_handler(qemu_log_func
, NULL
);
404 g_warn_if_fail(qemu_glog_domains
== NULL
);
405 qemu_glog_domains
= g_strdup(g_getenv("G_MESSAGES_DEBUG"));