qemu-iotests: Filter HMP readline escape characters
[qemu/kevin.git] / util / qemu-error.c
blobb331f8f4a4eccbf1ee1bc3fa31bbf351b5ba8f7e
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"
17 void error_printf(const char *fmt, ...)
19 va_list ap;
21 va_start(ap, fmt);
22 error_vprintf(fmt, ap);
23 va_end(ap);
26 void error_printf_unless_qmp(const char *fmt, ...)
28 va_list ap;
30 va_start(ap, fmt);
31 error_vprintf_unless_qmp(fmt, ap);
32 va_end(ap);
35 static Location std_loc = {
36 .kind = LOC_NONE
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)
47 assert(!loc->prev);
48 loc->prev = cur_loc;
49 cur_loc = loc;
50 return 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().
57 * Return LOC.
59 Location *loc_push_none(Location *loc)
61 loc->kind = LOC_NONE;
62 loc->prev = NULL;
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);
73 cur_loc = loc->prev;
74 loc->prev = NULL;
75 return loc;
79 * Save the current location in LOC, return LOC.
81 Location *loc_save(Location *loc)
83 *loc = *cur_loc;
84 loc->prev = NULL;
85 return loc;
89 * Change the current location to the one saved in LOC.
91 void loc_restore(Location *loc)
93 Location *prev = cur_loc->prev;
94 assert(!loc->prev);
95 *cur_loc = *loc;
96 cur_loc->prev = 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;
113 cur_loc->num = cnt;
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;
124 cur_loc->num = lno;
125 if (fname) {
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)
143 return progname;
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 = "";
152 int i;
153 const char *const *argp;
155 if (!cur_mon && progname) {
156 fprintf(stderr, "%s:", progname);
157 sep = " ";
159 switch (cur_loc->kind) {
160 case LOC_CMDLINE:
161 argp = cur_loc->ptr;
162 for (i = 0; i < cur_loc->num; i++) {
163 error_printf("%s%s", sep, argp[i]);
164 sep = " ";
166 error_printf(": ");
167 break;
168 case LOC_FILE:
169 error_printf("%s:", (const char *)cur_loc->ptr);
170 if (cur_loc->num) {
171 error_printf("%d:", cur_loc->num);
173 error_printf(" ");
174 break;
175 default:
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)
190 GTimeVal tv;
191 gchar *timestr;
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);
197 g_free(timestr);
200 error_print_loc();
201 error_vprintf(fmt, ap);
202 error_printf("\n");
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, ...)
214 va_list ap;
216 va_start(ap, fmt);
217 error_vreport(fmt, ap);
218 va_end(ap);