4 * Copyright (C) 2010 Red Hat Inc.
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 int error_printf(const char *fmt, ...)
33 ret = error_vprintf(fmt, ap);
38 int error_printf_unless_qmp(const char *fmt, ...)
44 ret = error_vprintf_unless_qmp(fmt, ap);
49 static Location std_loc = {
52 static Location *cur_loc = &std_loc;
55 * Push location saved in LOC onto the location stack, return it.
56 * The top of that stack is the current location.
57 * Needs a matching loc_pop().
59 Location *loc_push_restore(Location *loc)
68 * Initialize *LOC to "nowhere", push it onto the location stack.
69 * The top of that stack is the current location.
70 * Needs a matching loc_pop().
73 Location *loc_push_none(Location *loc)
77 return loc_push_restore(loc);
81 * Pop the location stack.
82 * LOC must be the current location, i.e. the top of the stack.
84 Location *loc_pop(Location *loc)
86 assert(cur_loc == loc && loc->prev);
93 * Save the current location in LOC, return LOC.
95 Location *loc_save(Location *loc)
103 * Change the current location to the one saved in LOC.
105 void loc_restore(Location *loc)
107 Location *prev = cur_loc->prev;
110 cur_loc->prev = prev;
114 * Change the current location to "nowhere in particular".
116 void loc_set_none(void)
118 cur_loc->kind = LOC_NONE;
122 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
124 void loc_set_cmdline(char **argv, int idx, int cnt)
126 cur_loc->kind = LOC_CMDLINE;
128 cur_loc->ptr = argv + idx;
132 * Change the current location to file FNAME, line LNO.
134 void loc_set_file(const char *fname, int lno)
136 assert (fname || cur_loc->kind == LOC_FILE);
137 cur_loc->kind = LOC_FILE;
140 cur_loc->ptr = fname;
144 static const char *progname;
147 * Set the program name for error_print_loc().
149 static void error_set_progname(const char *argv0)
151 const char *p = strrchr(argv0, '/');
152 progname = p ? p + 1 : argv0;
155 const char *error_get_progname(void)
161 * Print current location to current monitor if we have one, else to stderr.
163 static void print_loc(void)
165 const char *sep = "";
167 const char *const *argp;
169 if (!cur_mon && progname) {
170 fprintf(stderr, "%s:", progname);
173 switch (cur_loc->kind) {
176 for (i = 0; i < cur_loc->num; i++) {
177 error_printf("%s%s", sep, argp[i]);
183 error_printf("%s:", (const char *)cur_loc->ptr);
185 error_printf("%d:", cur_loc->num);
190 error_printf("%s", sep);
194 bool enable_timestamp_msg;
196 * Print a message to current monitor if we have one, else to stderr.
197 * @report_type is the type of message: error, warning or informational.
198 * Format arguments like vsprintf(). The resulting message should be
199 * a single phrase, with no newline or trailing punctuation.
200 * Prepend the current location and append a newline.
202 static void vreport(report_type type, const char *fmt, va_list ap)
207 if (enable_timestamp_msg && !cur_mon) {
208 g_get_current_time(&tv);
209 timestr = g_time_val_to_iso8601(&tv);
210 error_printf("%s ", timestr);
217 case REPORT_TYPE_ERROR:
219 case REPORT_TYPE_WARNING:
220 error_printf("warning: ");
222 case REPORT_TYPE_INFO:
223 error_printf("info: ");
227 error_vprintf(fmt, ap);
232 * Print an error message to current monitor if we have one, else to stderr.
233 * Format arguments like vsprintf(). The resulting message should be
234 * a single phrase, with no newline or trailing punctuation.
235 * Prepend the current location and append a newline.
236 * It's wrong to call this in a QMP monitor. Use error_setg() there.
238 void error_vreport(const char *fmt, va_list ap)
240 vreport(REPORT_TYPE_ERROR, fmt, ap);
244 * Print a warning message to current monitor if we have one, else to stderr.
245 * Format arguments like vsprintf(). The resulting message should be
246 * a single phrase, with no newline or trailing punctuation.
247 * Prepend the current location and append a newline.
249 void warn_vreport(const char *fmt, va_list ap)
251 vreport(REPORT_TYPE_WARNING, fmt, ap);
255 * Print an information message to current monitor if we have one, else to
257 * Format arguments like vsprintf(). The resulting message should be
258 * a single phrase, with no newline or trailing punctuation.
259 * Prepend the current location and append a newline.
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, ...)
278 vreport(REPORT_TYPE_ERROR, fmt, 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.
288 void warn_report(const char *fmt, ...)
293 vreport(REPORT_TYPE_WARNING, fmt, ap);
298 * Print an information message to current monitor if we have one, else to
300 * Format arguments like sprintf(). The resulting message should be a
301 * single phrase, with no newline or trailing punctuation.
302 * Prepend the current location and append a newline.
304 void info_report(const char *fmt, ...)
309 vreport(REPORT_TYPE_INFO, fmt, ap);
314 * Like error_report(), except print just once.
315 * If *printed is false, print the message, and flip *printed to true.
316 * Return whether the message was printed.
318 bool error_report_once_cond(bool *printed, const char *fmt, ...)
328 vreport(REPORT_TYPE_ERROR, fmt, ap);
334 * Like warn_report(), except print just once.
335 * If *printed is false, print the message, and flip *printed to true.
336 * Return whether the message was printed.
338 bool warn_report_once_cond(bool *printed, const char *fmt, ...)
348 vreport(REPORT_TYPE_WARNING, fmt, ap);
353 static char *qemu_glog_domains;
355 static void qemu_log_func(const gchar *log_domain,
356 GLogLevelFlags log_level,
357 const gchar *message,
360 switch (log_level & G_LOG_LEVEL_MASK) {
361 case G_LOG_LEVEL_DEBUG:
362 case G_LOG_LEVEL_INFO:
364 * Use same G_MESSAGES_DEBUG logic as glib to enable/disable debug
367 if (qemu_glog_domains == NULL) {
370 if (strcmp(qemu_glog_domains, "all") != 0 &&
371 (log_domain == NULL || !strstr(qemu_glog_domains, log_domain))) {
375 case G_LOG_LEVEL_MESSAGE:
376 info_report("%s%s%s",
377 log_domain ?: "", log_domain ? ": " : "", message);
380 case G_LOG_LEVEL_WARNING:
381 warn_report("%s%s%s",
382 log_domain ?: "", log_domain ? ": " : "", message);
384 case G_LOG_LEVEL_CRITICAL:
385 case G_LOG_LEVEL_ERROR:
386 error_report("%s%s%s",
387 log_domain ?: "", log_domain ? ": " : "", message);
392 void error_init(const char *argv0)
394 /* Set the program name for error_print_loc(). */
395 error_set_progname(argv0);
398 * This sets up glib logging so libraries using it also print their logs
399 * through error_report(), warn_report(), info_report().
401 g_log_set_default_handler(qemu_log_func, NULL);
402 g_warn_if_fail(qemu_glog_domains == NULL);
403 qemu_glog_domains = g_strdup(g_getenv("G_MESSAGES_DEBUG"));