build: move TARGET_GPROF to config-host.mak
[qemu.git] / util / qemu-error.c
blobdac7c7dc502544678312badb985af6c960a56f4e
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 /* Prepend timestamp to messages */
28 bool error_with_timestamp;
30 int error_printf(const char *fmt, ...)
32 va_list ap;
33 int ret;
35 va_start(ap, fmt);
36 ret = error_vprintf(fmt, ap);
37 va_end(ap);
38 return ret;
41 int error_printf_unless_qmp(const char *fmt, ...)
43 va_list ap;
44 int ret;
46 va_start(ap, fmt);
47 ret = error_vprintf_unless_qmp(fmt, ap);
48 va_end(ap);
49 return ret;
52 static Location std_loc = {
53 .kind = LOC_NONE
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)
64 assert(!loc->prev);
65 loc->prev = cur_loc;
66 cur_loc = loc;
67 return 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().
74 * Return LOC.
76 Location *loc_push_none(Location *loc)
78 loc->kind = LOC_NONE;
79 loc->prev = NULL;
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);
90 cur_loc = loc->prev;
91 loc->prev = NULL;
92 return loc;
96 * Save the current location in LOC, return LOC.
98 Location *loc_save(Location *loc)
100 *loc = *cur_loc;
101 loc->prev = NULL;
102 return loc;
106 * Change the current location to the one saved in LOC.
108 void loc_restore(Location *loc)
110 Location *prev = cur_loc->prev;
111 assert(!loc->prev);
112 *cur_loc = *loc;
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;
130 cur_loc->num = cnt;
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;
141 cur_loc->num = lno;
142 if (fname) {
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)
160 return progname;
164 * Print current location to current monitor if we have one, else to stderr.
166 static void print_loc(void)
168 const char *sep = "";
169 int i;
170 const char *const *argp;
172 if (!cur_mon && progname) {
173 fprintf(stderr, "%s:", progname);
174 sep = " ";
176 switch (cur_loc->kind) {
177 case LOC_CMDLINE:
178 argp = cur_loc->ptr;
179 for (i = 0; i < cur_loc->num; i++) {
180 error_printf("%s%s", sep, argp[i]);
181 sep = " ";
183 error_printf(": ");
184 break;
185 case LOC_FILE:
186 error_printf("%s:", (const char *)cur_loc->ptr);
187 if (cur_loc->num) {
188 error_printf("%d:", cur_loc->num);
190 error_printf(" ");
191 break;
192 default:
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)
206 GTimeVal tv;
207 gchar *timestr;
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);
213 g_free(timestr);
216 print_loc();
218 switch (type) {
219 case REPORT_TYPE_ERROR:
220 break;
221 case REPORT_TYPE_WARNING:
222 error_printf("warning: ");
223 break;
224 case REPORT_TYPE_INFO:
225 error_printf("info: ");
226 break;
229 error_vprintf(fmt, ap);
230 error_printf("\n");
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
258 * stderr.
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, ...)
277 va_list ap;
279 va_start(ap, fmt);
280 vreport(REPORT_TYPE_ERROR, fmt, ap);
281 va_end(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, ...)
292 va_list ap;
294 va_start(ap, fmt);
295 vreport(REPORT_TYPE_WARNING, fmt, ap);
296 va_end(ap);
300 * Print an information message to current monitor if we have one, else to
301 * stderr.
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, ...)
308 va_list ap;
310 va_start(ap, fmt);
311 vreport(REPORT_TYPE_INFO, fmt, ap);
312 va_end(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, ...)
322 va_list ap;
324 assert(printed);
325 if (*printed) {
326 return false;
328 *printed = true;
329 va_start(ap, fmt);
330 vreport(REPORT_TYPE_ERROR, fmt, ap);
331 va_end(ap);
332 return true;
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, ...)
342 va_list ap;
344 assert(printed);
345 if (*printed) {
346 return false;
348 *printed = true;
349 va_start(ap, fmt);
350 vreport(REPORT_TYPE_WARNING, fmt, ap);
351 va_end(ap);
352 return true;
355 static char *qemu_glog_domains;
357 static void qemu_log_func(const gchar *log_domain,
358 GLogLevelFlags log_level,
359 const gchar *message,
360 gpointer user_data)
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
367 * messages
369 if (qemu_glog_domains == NULL) {
370 break;
372 if (strcmp(qemu_glog_domains, "all") != 0 &&
373 (log_domain == NULL || !strstr(qemu_glog_domains, log_domain))) {
374 break;
376 /* Fall through */
377 case G_LOG_LEVEL_MESSAGE:
378 info_report("%s%s%s",
379 log_domain ?: "", log_domain ? ": " : "", message);
381 break;
382 case G_LOG_LEVEL_WARNING:
383 warn_report("%s%s%s",
384 log_domain ?: "", log_domain ? ": " : "", message);
385 break;
386 case G_LOG_LEVEL_CRITICAL:
387 case G_LOG_LEVEL_ERROR:
388 error_report("%s%s%s",
389 log_domain ?: "", log_domain ? ": " : "", message);
390 break;
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"));