]>
Commit | Line | Data |
---|---|---|
ba0fe87a MA |
1 | /* |
2 | * Error reporting | |
3 | * | |
4 | * Copyright (C) 2010 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Markus Armbruster <[email protected]>, | |
8 | * | |
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. | |
11 | */ | |
12 | ||
aafd7584 | 13 | #include "qemu/osdep.h" |
83c9089e | 14 | #include "monitor/monitor.h" |
d49b6836 | 15 | #include "qemu/error-report.h" |
b4a51f7f | 16 | |
97f40301 AF |
17 | /* |
18 | * @report_type is the type of message: error, warning or | |
19 | * informational. | |
20 | */ | |
21 | typedef enum { | |
22 | REPORT_TYPE_ERROR, | |
23 | REPORT_TYPE_WARNING, | |
24 | REPORT_TYPE_INFO, | |
25 | } report_type; | |
26 | ||
deda497b MA |
27 | /* Prepend timestamp to messages */ |
28 | bool error_with_timestamp; | |
29 | ||
679cb8e1 | 30 | int error_printf(const char *fmt, ...) |
ba0fe87a MA |
31 | { |
32 | va_list ap; | |
679cb8e1 | 33 | int ret; |
ba0fe87a MA |
34 | |
35 | va_start(ap, fmt); | |
679cb8e1 | 36 | ret = error_vprintf(fmt, ap); |
ba0fe87a | 37 | va_end(ap); |
679cb8e1 | 38 | return ret; |
ba0fe87a MA |
39 | } |
40 | ||
679cb8e1 | 41 | int error_printf_unless_qmp(const char *fmt, ...) |
aa924ae7 MA |
42 | { |
43 | va_list ap; | |
679cb8e1 | 44 | int ret; |
aa924ae7 | 45 | |
397d30e9 | 46 | va_start(ap, fmt); |
679cb8e1 | 47 | ret = error_vprintf_unless_qmp(fmt, ap); |
397d30e9 | 48 | va_end(ap); |
679cb8e1 | 49 | return ret; |
aa924ae7 MA |
50 | } |
51 | ||
827b0813 MA |
52 | static Location std_loc = { |
53 | .kind = LOC_NONE | |
54 | }; | |
55 | static Location *cur_loc = &std_loc; | |
56 | ||
57 | /* | |
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(). | |
61 | */ | |
62 | Location *loc_push_restore(Location *loc) | |
63 | { | |
64 | assert(!loc->prev); | |
65 | loc->prev = cur_loc; | |
66 | cur_loc = loc; | |
67 | return loc; | |
68 | } | |
69 | ||
70 | /* | |
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. | |
75 | */ | |
76 | Location *loc_push_none(Location *loc) | |
77 | { | |
78 | loc->kind = LOC_NONE; | |
79 | loc->prev = NULL; | |
80 | return loc_push_restore(loc); | |
81 | } | |
82 | ||
83 | /* | |
84 | * Pop the location stack. | |
85 | * LOC must be the current location, i.e. the top of the stack. | |
86 | */ | |
87 | Location *loc_pop(Location *loc) | |
88 | { | |
89 | assert(cur_loc == loc && loc->prev); | |
90 | cur_loc = loc->prev; | |
91 | loc->prev = NULL; | |
92 | return loc; | |
93 | } | |
94 | ||
95 | /* | |
96 | * Save the current location in LOC, return LOC. | |
97 | */ | |
98 | Location *loc_save(Location *loc) | |
99 | { | |
100 | *loc = *cur_loc; | |
101 | loc->prev = NULL; | |
102 | return loc; | |
103 | } | |
104 | ||
105 | /* | |
106 | * Change the current location to the one saved in LOC. | |
107 | */ | |
108 | void loc_restore(Location *loc) | |
109 | { | |
110 | Location *prev = cur_loc->prev; | |
111 | assert(!loc->prev); | |
112 | *cur_loc = *loc; | |
113 | cur_loc->prev = prev; | |
114 | } | |
115 | ||
116 | /* | |
117 | * Change the current location to "nowhere in particular". | |
118 | */ | |
119 | void loc_set_none(void) | |
120 | { | |
121 | cur_loc->kind = LOC_NONE; | |
122 | } | |
123 | ||
0f0bc3f1 MA |
124 | /* |
125 | * Change the current location to argument ARGV[IDX..IDX+CNT-1]. | |
126 | */ | |
127 | void loc_set_cmdline(char **argv, int idx, int cnt) | |
128 | { | |
129 | cur_loc->kind = LOC_CMDLINE; | |
130 | cur_loc->num = cnt; | |
131 | cur_loc->ptr = argv + idx; | |
132 | } | |
133 | ||
cf5a65aa MA |
134 | /* |
135 | * Change the current location to file FNAME, line LNO. | |
136 | */ | |
137 | void loc_set_file(const char *fname, int lno) | |
138 | { | |
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; | |
144 | } | |
145 | } | |
146 | ||
65abca0a MA |
147 | static const char *progname; |
148 | ||
149 | /* | |
150 | * Set the program name for error_print_loc(). | |
151 | */ | |
f5852efa | 152 | static void error_set_progname(const char *argv0) |
65abca0a MA |
153 | { |
154 | const char *p = strrchr(argv0, '/'); | |
155 | progname = p ? p + 1 : argv0; | |
156 | } | |
157 | ||
7636a470 ME |
158 | const char *error_get_progname(void) |
159 | { | |
160 | return progname; | |
161 | } | |
162 | ||
827b0813 MA |
163 | /* |
164 | * Print current location to current monitor if we have one, else to stderr. | |
165 | */ | |
beeb175c | 166 | static void print_loc(void) |
827b0813 | 167 | { |
65abca0a | 168 | const char *sep = ""; |
0f0bc3f1 MA |
169 | int i; |
170 | const char *const *argp; | |
65abca0a | 171 | |
6627f645 | 172 | if (!cur_mon && progname) { |
65abca0a MA |
173 | fprintf(stderr, "%s:", progname); |
174 | sep = " "; | |
175 | } | |
827b0813 | 176 | switch (cur_loc->kind) { |
0f0bc3f1 MA |
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 = " "; | |
182 | } | |
183 | error_printf(": "); | |
184 | break; | |
cf5a65aa MA |
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); | |
189 | } | |
190 | error_printf(" "); | |
191 | break; | |
65abca0a | 192 | default: |
bb334b12 | 193 | error_printf("%s", sep); |
827b0813 MA |
194 | } |
195 | } | |
196 | ||
1ecda02b | 197 | /* |
97f40301 AF |
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. | |
f4d0064a MA |
200 | * Format arguments like vsprintf(). The resulting message should be |
201 | * a single phrase, with no newline or trailing punctuation. | |
827b0813 | 202 | * Prepend the current location and append a newline. |
1ecda02b | 203 | */ |
97f40301 | 204 | static void vreport(report_type type, const char *fmt, va_list ap) |
ba0fe87a | 205 | { |
5e2ac519 SA |
206 | GTimeVal tv; |
207 | gchar *timestr; | |
208 | ||
deda497b | 209 | if (error_with_timestamp && !cur_mon) { |
70f17a15 MA |
210 | g_get_current_time(&tv); |
211 | timestr = g_time_val_to_iso8601(&tv); | |
212 | error_printf("%s ", timestr); | |
213 | g_free(timestr); | |
214 | } | |
215 | ||
216 | print_loc(); | |
217 | ||
97f40301 AF |
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; | |
227 | } | |
228 | ||
ba0fe87a | 229 | error_vprintf(fmt, ap); |
1ecda02b | 230 | error_printf("\n"); |
b4a51f7f | 231 | } |
5748e4c2 CM |
232 | |
233 | /* | |
234 | * Print an error message to current monitor if we have one, else to stderr. | |
97f40301 AF |
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. | |
239 | */ | |
240 | void error_vreport(const char *fmt, va_list ap) | |
241 | { | |
242 | vreport(REPORT_TYPE_ERROR, fmt, ap); | |
243 | } | |
244 | ||
245 | /* | |
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. | |
97f40301 AF |
250 | */ |
251 | void warn_vreport(const char *fmt, va_list ap) | |
252 | { | |
253 | vreport(REPORT_TYPE_WARNING, fmt, ap); | |
254 | } | |
255 | ||
256 | /* | |
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. | |
97f40301 AF |
262 | */ |
263 | void info_vreport(const char *fmt, va_list ap) | |
264 | { | |
265 | vreport(REPORT_TYPE_INFO, fmt, ap); | |
266 | } | |
267 | ||
268 | /* | |
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. | |
5748e4c2 | 272 | * Prepend the current location and append a newline. |
485febc6 | 273 | * It's wrong to call this in a QMP monitor. Use error_setg() there. |
5748e4c2 CM |
274 | */ |
275 | void error_report(const char *fmt, ...) | |
276 | { | |
277 | va_list ap; | |
278 | ||
279 | va_start(ap, fmt); | |
97f40301 AF |
280 | vreport(REPORT_TYPE_ERROR, fmt, ap); |
281 | va_end(ap); | |
282 | } | |
283 | ||
284 | /* | |
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. | |
97f40301 AF |
289 | */ |
290 | void warn_report(const char *fmt, ...) | |
291 | { | |
292 | va_list ap; | |
293 | ||
294 | va_start(ap, fmt); | |
295 | vreport(REPORT_TYPE_WARNING, fmt, ap); | |
296 | va_end(ap); | |
297 | } | |
298 | ||
299 | /* | |
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. | |
97f40301 AF |
305 | */ |
306 | void info_report(const char *fmt, ...) | |
307 | { | |
308 | va_list ap; | |
309 | ||
310 | va_start(ap, fmt); | |
311 | vreport(REPORT_TYPE_INFO, fmt, ap); | |
5748e4c2 CM |
312 | va_end(ap); |
313 | } | |
c55510b7 CH |
314 | |
315 | /* | |
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. | |
319 | */ | |
320 | bool error_report_once_cond(bool *printed, const char *fmt, ...) | |
321 | { | |
322 | va_list ap; | |
323 | ||
324 | assert(printed); | |
325 | if (*printed) { | |
326 | return false; | |
327 | } | |
328 | *printed = true; | |
329 | va_start(ap, fmt); | |
330 | vreport(REPORT_TYPE_ERROR, fmt, ap); | |
331 | va_end(ap); | |
332 | return true; | |
333 | } | |
334 | ||
335 | /* | |
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. | |
339 | */ | |
340 | bool warn_report_once_cond(bool *printed, const char *fmt, ...) | |
341 | { | |
342 | va_list ap; | |
343 | ||
344 | assert(printed); | |
345 | if (*printed) { | |
346 | return false; | |
347 | } | |
348 | *printed = true; | |
349 | va_start(ap, fmt); | |
350 | vreport(REPORT_TYPE_WARNING, fmt, ap); | |
351 | va_end(ap); | |
352 | return true; | |
353 | } | |
f5852efa CF |
354 | |
355 | static char *qemu_glog_domains; | |
356 | ||
357 | static void qemu_log_func(const gchar *log_domain, | |
358 | GLogLevelFlags log_level, | |
359 | const gchar *message, | |
360 | gpointer user_data) | |
361 | { | |
362 | switch (log_level & G_LOG_LEVEL_MASK) { | |
363 | case G_LOG_LEVEL_DEBUG: | |
364 | case G_LOG_LEVEL_INFO: | |
365 | /* | |
366 | * Use same G_MESSAGES_DEBUG logic as glib to enable/disable debug | |
367 | * messages | |
368 | */ | |
369 | if (qemu_glog_domains == NULL) { | |
370 | break; | |
371 | } | |
372 | if (strcmp(qemu_glog_domains, "all") != 0 && | |
373 | (log_domain == NULL || !strstr(qemu_glog_domains, log_domain))) { | |
374 | break; | |
375 | } | |
376 | /* Fall through */ | |
377 | case G_LOG_LEVEL_MESSAGE: | |
378 | info_report("%s%s%s", | |
379 | log_domain ?: "", log_domain ? ": " : "", message); | |
380 | ||
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; | |
391 | } | |
392 | } | |
393 | ||
394 | void error_init(const char *argv0) | |
395 | { | |
396 | /* Set the program name for error_print_loc(). */ | |
397 | error_set_progname(argv0); | |
398 | ||
399 | /* | |
400 | * This sets up glib logging so libraries using it also print their logs | |
401 | * through error_report(), warn_report(), info_report(). | |
402 | */ | |
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")); | |
406 | } |