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 message_with_timestamp
;
29 bool error_with_guestname
;
30 const char *error_guest_name
;
32 int error_printf(const char *fmt
, ...)
38 ret
= error_vprintf(fmt
, ap
);
43 static Location std_loc
= {
46 static Location
*cur_loc
= &std_loc
;
49 * Push location saved in LOC onto the location stack, return it.
50 * The top of that stack is the current location.
51 * Needs a matching loc_pop().
53 Location
*loc_push_restore(Location
*loc
)
62 * Initialize *LOC to "nowhere", push it onto the location stack.
63 * The top of that stack is the current location.
64 * Needs a matching loc_pop().
67 Location
*loc_push_none(Location
*loc
)
71 return loc_push_restore(loc
);
75 * Pop the location stack.
76 * LOC must be the current location, i.e. the top of the stack.
78 Location
*loc_pop(Location
*loc
)
80 assert(cur_loc
== loc
&& loc
->prev
);
87 * Save the current location in LOC, return LOC.
89 Location
*loc_save(Location
*loc
)
97 * Change the current location to the one saved in LOC.
99 void loc_restore(Location
*loc
)
101 Location
*prev
= cur_loc
->prev
;
104 cur_loc
->prev
= prev
;
108 * Change the current location to "nowhere in particular".
110 void loc_set_none(void)
112 cur_loc
->kind
= LOC_NONE
;
116 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
118 void loc_set_cmdline(char **argv
, int idx
, int cnt
)
120 cur_loc
->kind
= LOC_CMDLINE
;
122 cur_loc
->ptr
= argv
+ idx
;
126 * Change the current location to file FNAME, line LNO.
128 void loc_set_file(const char *fname
, int lno
)
130 assert (fname
|| cur_loc
->kind
== LOC_FILE
);
131 cur_loc
->kind
= LOC_FILE
;
134 cur_loc
->ptr
= fname
;
139 * Print current location to current monitor if we have one, else to stderr.
141 static void print_loc(void)
143 const char *sep
= "";
145 const char *const *argp
;
147 if (!monitor_cur() && g_get_prgname()) {
148 error_printf("%s:", g_get_prgname());
151 switch (cur_loc
->kind
) {
154 for (i
= 0; i
< cur_loc
->num
; i
++) {
155 error_printf("%s%s", sep
, argp
[i
]);
161 error_printf("%s:", (const char *)cur_loc
->ptr
);
163 error_printf("%d:", cur_loc
->num
);
168 error_printf("%s", sep
);
173 real_time_iso8601(void)
175 g_autoptr(GDateTime
) dt
= g_date_time_new_now_utc();
176 return g_date_time_format_iso8601(dt
);
180 * Print a message to current monitor if we have one, else to stderr.
181 * @report_type is the type of message: error, warning or informational.
182 * Format arguments like vsprintf(). The resulting message should be
183 * a single phrase, with no newline or trailing punctuation.
184 * Prepend the current location and append a newline.
187 static void vreport(report_type type
, const char *fmt
, va_list ap
)
191 if (message_with_timestamp
&& !monitor_cur()) {
192 timestr
= real_time_iso8601();
193 error_printf("%s ", timestr
);
197 /* Only prepend guest name if -msg guest-name and -name guest=... are set */
198 if (error_with_guestname
&& error_guest_name
&& !monitor_cur()) {
199 error_printf("%s ", error_guest_name
);
205 case REPORT_TYPE_ERROR
:
207 case REPORT_TYPE_WARNING
:
208 error_printf("warning: ");
210 case REPORT_TYPE_INFO
:
211 error_printf("info: ");
215 error_vprintf(fmt
, ap
);
220 * Print an error message to current monitor if we have one, else to stderr.
221 * Format arguments like vsprintf(). The resulting message should be
222 * a single phrase, with no newline or trailing punctuation.
223 * Prepend the current location and append a newline.
224 * It's wrong to call this in a QMP monitor. Use error_setg() there.
226 void error_vreport(const char *fmt
, va_list ap
)
228 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
232 * Print a warning 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.
237 void warn_vreport(const char *fmt
, va_list ap
)
239 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);
243 * Print an information message to current monitor if we have one, else to
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 info_vreport(const char *fmt
, va_list ap
)
251 vreport(REPORT_TYPE_INFO
, fmt
, ap
);
255 * Print an error message to current monitor if we have one, else to stderr.
256 * Format arguments like sprintf(). 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 error_report(const char *fmt
, ...)
266 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
271 * Print a warning message to current monitor if we have one, else to stderr.
272 * Format arguments like sprintf(). The resulting message should be a
273 * single phrase, with no newline or trailing punctuation.
274 * Prepend the current location and append a newline.
276 void warn_report(const char *fmt
, ...)
281 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);
286 * Print an information message to current monitor if we have one, else to
288 * Format arguments like sprintf(). The resulting message should be a
289 * single phrase, with no newline or trailing punctuation.
290 * Prepend the current location and append a newline.
292 void info_report(const char *fmt
, ...)
297 vreport(REPORT_TYPE_INFO
, fmt
, ap
);
302 * Like error_report(), except print just once.
303 * If *printed is false, print the message, and flip *printed to true.
304 * Return whether the message was printed.
306 bool error_report_once_cond(bool *printed
, const char *fmt
, ...)
316 vreport(REPORT_TYPE_ERROR
, fmt
, ap
);
322 * Like warn_report(), except print just once.
323 * If *printed is false, print the message, and flip *printed to true.
324 * Return whether the message was printed.
326 bool warn_report_once_cond(bool *printed
, const char *fmt
, ...)
336 vreport(REPORT_TYPE_WARNING
, fmt
, ap
);
341 static char *qemu_glog_domains
;
343 static void qemu_log_func(const gchar
*log_domain
,
344 GLogLevelFlags log_level
,
345 const gchar
*message
,
348 switch (log_level
& G_LOG_LEVEL_MASK
) {
349 case G_LOG_LEVEL_DEBUG
:
350 case G_LOG_LEVEL_INFO
:
352 * Use same G_MESSAGES_DEBUG logic as glib to enable/disable debug
355 if (qemu_glog_domains
== NULL
) {
358 if (strcmp(qemu_glog_domains
, "all") != 0 &&
359 (log_domain
== NULL
|| !strstr(qemu_glog_domains
, log_domain
))) {
363 case G_LOG_LEVEL_MESSAGE
:
364 info_report("%s%s%s",
365 log_domain
?: "", log_domain
? ": " : "", message
);
368 case G_LOG_LEVEL_WARNING
:
369 warn_report("%s%s%s",
370 log_domain
?: "", log_domain
? ": " : "", message
);
372 case G_LOG_LEVEL_CRITICAL
:
373 case G_LOG_LEVEL_ERROR
:
374 error_report("%s%s%s",
375 log_domain
?: "", log_domain
? ": " : "", message
);
380 void error_init(const char *argv0
)
382 const char *p
= strrchr(argv0
, '/');
384 /* Set the program name for error_print_loc(). */
385 g_set_prgname(p
? p
+ 1 : argv0
);
388 * This sets up glib logging so libraries using it also print their logs
389 * through error_report(), warn_report(), info_report().
391 g_log_set_default_handler(qemu_log_func
, NULL
);
392 g_warn_if_fail(qemu_glog_domains
== NULL
);
393 qemu_glog_domains
= g_strdup(g_getenv("G_MESSAGES_DEBUG"));