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.
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
)
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 static Location std_loc
= {
47 static Location
*cur_loc
= &std_loc
;
50 * Push location saved in LOC onto the location stack, return it.
51 * The top of that stack is the current location.
52 * Needs a matching loc_pop().
54 Location
*loc_push_restore(Location
*loc
)
63 * Initialize *LOC to "nowhere", push it onto the location stack.
64 * The top of that stack is the current location.
65 * Needs a matching loc_pop().
68 Location
*loc_push_none(Location
*loc
)
72 return loc_push_restore(loc
);
76 * Pop the location stack.
77 * LOC must be the current location, i.e. the top of the stack.
79 Location
*loc_pop(Location
*loc
)
81 assert(cur_loc
== loc
&& loc
->prev
);
88 * Save the current location in LOC, return LOC.
90 Location
*loc_save(Location
*loc
)
98 * Change the current location to the one saved in LOC.
100 void loc_restore(Location
*loc
)
102 Location
*prev
= cur_loc
->prev
;
105 cur_loc
->prev
= prev
;
109 * Change the current location to "nowhere in particular".
111 void loc_set_none(void)
113 cur_loc
->kind
= LOC_NONE
;
117 * Change the current location to argument ARGV[IDX..IDX+CNT-1].
119 void loc_set_cmdline(char **argv
, int idx
, int cnt
)
121 cur_loc
->kind
= LOC_CMDLINE
;
123 cur_loc
->ptr
= argv
+ idx
;
127 * Change the current location to file FNAME, line LNO.
129 void loc_set_file(const char *fname
, int lno
)
131 assert (fname
|| cur_loc
->kind
== LOC_FILE
);
132 cur_loc
->kind
= LOC_FILE
;
135 cur_loc
->ptr
= fname
;
139 static const char *progname
;
142 * Set the program name for error_print_loc().
144 void error_set_progname(const char *argv0
)
146 const char *p
= strrchr(argv0
, '/');
147 progname
= p
? p
+ 1 : argv0
;
151 * Print current location to current monitor if we have one, else to stderr.
153 void error_print_loc(void)
155 const char *sep
= "";
157 const char *const *argp
;
160 fprintf(stderr
, "%s:", progname
);
163 switch (cur_loc
->kind
) {
166 for (i
= 0; i
< cur_loc
->num
; i
++) {
167 error_printf("%s%s", sep
, argp
[i
]);
173 error_printf("%s:", (const char *)cur_loc
->ptr
);
175 error_printf("%d:", cur_loc
->num
);
185 * Print an error message to current monitor if we have one, else to stderr.
186 * Prepend the current location and append a newline.
187 * It's wrong to call this in a QMP monitor. Use qerror_report() there.
189 void error_report(const char *fmt
, ...)
195 error_vprintf(fmt
, ap
);
200 void qerror_report_internal(const char *file
, int linenr
, const char *func
,
201 const char *fmt
, ...)
207 qerror
= qerror_from_info(file
, linenr
, func
, fmt
, &va
);
210 if (monitor_cur_is_qmp()) {
211 monitor_set_error(cur_mon
, qerror
);
213 qerror_print(qerror
);