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"
17 * Print to current monitor if we have one, else to stderr.
18 * TODO should return int, so callers can calculate width, but that
19 * requires surgery to monitor_vprintf(). Left for another day.
21 void error_vprintf(const char *fmt
, va_list ap
)
23 if (cur_mon
&& !monitor_cur_is_qmp()) {
24 monitor_vprintf(cur_mon
, fmt
, ap
);
26 vfprintf(stderr
, fmt
, ap
);
31 * Print to current monitor if we have one, else to stderr.
32 * TODO just like error_vprintf()
34 void error_printf(const char *fmt
, ...)
39 error_vprintf(fmt
, ap
);
43 void error_printf_unless_qmp(const char *fmt
, ...)
47 if (!monitor_cur_is_qmp()) {
49 error_vprintf(fmt
, ap
);
54 static Location std_loc
= {
57 static Location
*cur_loc
= &std_loc
;
60 * Push location saved in LOC onto the location stack, return it.
61 * The top of that stack is the current location.
62 * Needs a matching loc_pop().
64 Location
*loc_push_restore(Location
*loc
)
73 * Initialize *LOC to "nowhere", push it onto the location stack.
74 * The top of that stack is the current location.
75 * Needs a matching loc_pop().
78 Location
*loc_push_none(Location
*loc
)
82 return loc_push_restore(loc
);
86 * Pop the location stack.
87 * LOC must be the current location, i.e. the top of the stack.
89 Location
*loc_pop(Location
*loc
)
91 assert(cur_loc
== loc
&& loc
->prev
);
98 * Save the current location in LOC, return LOC.
100 Location
*loc_save(Location
*loc
)
108 * Change the current location to the one saved in LOC.
110 void loc_restore(Location
*loc
)
112 Location
*prev
= cur_loc
->prev
;
115 cur_loc
->prev
= prev
;
119 * Change the current location to "nowhere in particular".
121 void loc_set_none(void)
123 cur_loc
->kind
= LOC_NONE
;
127 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
129 void loc_set_cmdline(char **argv
, int idx
, int cnt
)
131 cur_loc
->kind
= LOC_CMDLINE
;
133 cur_loc
->ptr
= argv
+ idx
;
137 * Change the current location to file FNAME, line LNO.
139 void loc_set_file(const char *fname
, int lno
)
141 assert (fname
|| cur_loc
->kind
== LOC_FILE
);
142 cur_loc
->kind
= LOC_FILE
;
145 cur_loc
->ptr
= fname
;
149 static const char *progname
;
152 * Set the program name for error_print_loc().
154 void error_set_progname(const char *argv0
)
156 const char *p
= strrchr(argv0
, '/');
157 progname
= p
? p
+ 1 : argv0
;
160 const char *error_get_progname(void)
166 * Print current location to current monitor if we have one, else to stderr.
168 static void error_print_loc(void)
170 const char *sep
= "";
172 const char *const *argp
;
174 if (!cur_mon
&& progname
) {
175 fprintf(stderr
, "%s:", progname
);
178 switch (cur_loc
->kind
) {
181 for (i
= 0; i
< cur_loc
->num
; i
++) {
182 error_printf("%s%s", sep
, argp
[i
]);
188 error_printf("%s:", (const char *)cur_loc
->ptr
);
190 error_printf("%d:", cur_loc
->num
);
195 error_printf("%s", sep
);
199 bool enable_timestamp_msg
;
201 * Print an error message to current monitor if we have one, else to stderr.
202 * Format arguments like vsprintf(). The result should not contain
204 * Prepend the current location and append a newline.
205 * It's wrong to call this in a QMP monitor. Use qerror_report() there.
207 void error_vreport(const char *fmt
, va_list ap
)
212 if (enable_timestamp_msg
) {
213 g_get_current_time(&tv
);
214 timestr
= g_time_val_to_iso8601(&tv
);
215 error_printf("%s ", timestr
);
220 error_vprintf(fmt
, ap
);
225 * Print an error message to current monitor if we have one, else to stderr.
226 * Format arguments like sprintf(). The result should not contain
228 * Prepend the current location and append a newline.
229 * It's wrong to call this in a QMP monitor. Use qerror_report() there.
231 void error_report(const char *fmt
, ...)
236 error_vreport(fmt
, ap
);