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"
17 void error_printf(const char *fmt, ...)
22 error_vprintf(fmt, ap);
26 void error_printf_unless_qmp(const char *fmt, ...)
31 error_vprintf_unless_qmp(fmt, ap);
35 static Location std_loc = {
38 static Location *cur_loc = &std_loc;
41 * Push location saved in LOC onto the location stack, return it.
42 * The top of that stack is the current location.
43 * Needs a matching loc_pop().
45 Location *loc_push_restore(Location *loc)
54 * Initialize *LOC to "nowhere", push it onto the location stack.
55 * The top of that stack is the current location.
56 * Needs a matching loc_pop().
59 Location *loc_push_none(Location *loc)
63 return loc_push_restore(loc);
67 * Pop the location stack.
68 * LOC must be the current location, i.e. the top of the stack.
70 Location *loc_pop(Location *loc)
72 assert(cur_loc == loc && loc->prev);
79 * Save the current location in LOC, return LOC.
81 Location *loc_save(Location *loc)
89 * Change the current location to the one saved in LOC.
91 void loc_restore(Location *loc)
93 Location *prev = cur_loc->prev;
100 * Change the current location to "nowhere in particular".
102 void loc_set_none(void)
104 cur_loc->kind = LOC_NONE;
108 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
110 void loc_set_cmdline(char **argv, int idx, int cnt)
112 cur_loc->kind = LOC_CMDLINE;
114 cur_loc->ptr = argv + idx;
118 * Change the current location to file FNAME, line LNO.
120 void loc_set_file(const char *fname, int lno)
122 assert (fname || cur_loc->kind == LOC_FILE);
123 cur_loc->kind = LOC_FILE;
126 cur_loc->ptr = fname;
130 static const char *progname;
133 * Set the program name for error_print_loc().
135 void error_set_progname(const char *argv0)
137 const char *p = strrchr(argv0, '/');
138 progname = p ? p + 1 : argv0;
141 const char *error_get_progname(void)
147 * Print current location to current monitor if we have one, else to stderr.
149 static void error_print_loc(void)
151 const char *sep = "";
153 const char *const *argp;
155 if (!cur_mon && progname) {
156 fprintf(stderr, "%s:", progname);
159 switch (cur_loc->kind) {
162 for (i = 0; i < cur_loc->num; i++) {
163 error_printf("%s%s", sep, argp[i]);
169 error_printf("%s:", (const char *)cur_loc->ptr);
171 error_printf("%d:", cur_loc->num);
176 error_printf("%s", sep);
180 bool enable_timestamp_msg;
182 * Print an error message to current monitor if we have one, else to stderr.
183 * Format arguments like vsprintf(). The resulting message should be
184 * a single phrase, with no newline or trailing punctuation.
185 * Prepend the current location and append a newline.
186 * It's wrong to call this in a QMP monitor. Use error_setg() there.
188 void error_vreport(const char *fmt, va_list ap)
193 if (enable_timestamp_msg && !cur_mon) {
194 g_get_current_time(&tv);
195 timestr = g_time_val_to_iso8601(&tv);
196 error_printf("%s ", timestr);
201 error_vprintf(fmt, ap);
206 * Print an error message to current monitor if we have one, else to stderr.
207 * Format arguments like sprintf(). The resulting message should be a
208 * single phrase, with no newline or trailing punctuation.
209 * Prepend the current location and append a newline.
210 * It's wrong to call this in a QMP monitor. Use error_setg() there.
212 void error_report(const char *fmt, ...)
217 error_vreport(fmt, ap);