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.
14 #include "monitor/monitor.h"
15 #include "qemu/error-report.h"
18 * Print to current monitor if we have one, else to stderr.
19 * TODO should return int, so callers can calculate width, but that
20 * requires surgery to monitor_vprintf(). Left for another day.
22 void error_vprintf(const char *fmt, va_list ap)
24 if (cur_mon && !monitor_cur_is_qmp()) {
25 monitor_vprintf(cur_mon, fmt, ap);
27 vfprintf(stderr, fmt, ap);
32 * Print to current monitor if we have one, else to stderr.
33 * TODO just like error_vprintf()
35 void error_printf(const char *fmt, ...)
40 error_vprintf(fmt, ap);
44 void error_printf_unless_qmp(const char *fmt, ...)
48 if (!monitor_cur_is_qmp()) {
50 error_vprintf(fmt, ap);
55 static Location std_loc = {
58 static Location *cur_loc = &std_loc;
61 * Push location saved in LOC onto the location stack, return it.
62 * The top of that stack is the current location.
63 * Needs a matching loc_pop().
65 Location *loc_push_restore(Location *loc)
74 * Initialize *LOC to "nowhere", push it onto the location stack.
75 * The top of that stack is the current location.
76 * Needs a matching loc_pop().
79 Location *loc_push_none(Location *loc)
83 return loc_push_restore(loc);
87 * Pop the location stack.
88 * LOC must be the current location, i.e. the top of the stack.
90 Location *loc_pop(Location *loc)
92 assert(cur_loc == loc && loc->prev);
99 * Save the current location in LOC, return LOC.
101 Location *loc_save(Location *loc)
109 * Change the current location to the one saved in LOC.
111 void loc_restore(Location *loc)
113 Location *prev = cur_loc->prev;
116 cur_loc->prev = prev;
120 * Change the current location to "nowhere in particular".
122 void loc_set_none(void)
124 cur_loc->kind = LOC_NONE;
128 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
130 void loc_set_cmdline(char **argv, int idx, int cnt)
132 cur_loc->kind = LOC_CMDLINE;
134 cur_loc->ptr = argv + idx;
138 * Change the current location to file FNAME, line LNO.
140 void loc_set_file(const char *fname, int lno)
142 assert (fname || cur_loc->kind == LOC_FILE);
143 cur_loc->kind = LOC_FILE;
146 cur_loc->ptr = fname;
150 static const char *progname;
153 * Set the program name for error_print_loc().
155 void error_set_progname(const char *argv0)
157 const char *p = strrchr(argv0, '/');
158 progname = p ? p + 1 : argv0;
161 const char *error_get_progname(void)
167 * Print current location to current monitor if we have one, else to stderr.
169 static void error_print_loc(void)
171 const char *sep = "";
173 const char *const *argp;
175 if (!cur_mon && progname) {
176 fprintf(stderr, "%s:", progname);
179 switch (cur_loc->kind) {
182 for (i = 0; i < cur_loc->num; i++) {
183 error_printf("%s%s", sep, argp[i]);
189 error_printf("%s:", (const char *)cur_loc->ptr);
191 error_printf("%d:", cur_loc->num);
196 error_printf("%s", sep);
200 bool enable_timestamp_msg;
202 * Print an error message to current monitor if we have one, else to stderr.
203 * Format arguments like vsprintf(). The result should not contain
205 * Prepend the current location and append a newline.
206 * It's wrong to call this in a QMP monitor. Use error_setg() there.
208 void error_vreport(const char *fmt, va_list ap)
213 if (enable_timestamp_msg) {
214 g_get_current_time(&tv);
215 timestr = g_time_val_to_iso8601(&tv);
216 error_printf("%s ", timestr);
221 error_vprintf(fmt, ap);
226 * Print an error message to current monitor if we have one, else to stderr.
227 * Format arguments like sprintf(). The result should not contain
229 * Prepend the current location and append a newline.
230 * It's wrong to call this in a QMP monitor. Use error_setg() there.
232 void error_report(const char *fmt, ...)
237 error_vreport(fmt, ap);