monitor error: Make printf()-like functions return a value

printf() & friends return the number of characters written on success,
negative value on error.

monitor_printf(), monitor_vfprintf(), monitor_vprintf(),
error_printf(), error_printf_unless_qmp(), error_vprintf(), and
error_vprintf_unless_qmp() return void.  Some of them carry a TODO
comment asking for int instead.

Improve them to return int like printf() does.

This makes our use of monitor_printf() as fprintf_function slightly
less dirty: the function cast no longer adds a return value that isn't
there.  It still changes a parameter's pointer type.  That will be
addressed in a future commit.

monitor_vfprintf() always returns zero.  Improve it to return the
proper value.

Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Message-Id: <20190417190641.26814-11-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2019-04-17 21:06:36 +02:00
parent 26eaf2cd0d
commit 679cb8e1a1
5 changed files with 57 additions and 45 deletions

View File

@ -28,9 +28,9 @@ void monitor_resume(Monitor *mon);
int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp);
int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp);
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
GCC_FMT_ATTR(2, 0);
void monitor_printf(Monitor *mon, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
int monitor_printf(Monitor *mon, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
int monitor_fprintf(FILE *stream, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
void monitor_flush(Monitor *mon);
int monitor_set_cpu(int cpu_index);
@ -48,7 +48,7 @@ int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd);
void monitor_fdset_dup_fd_remove(int dup_fd);
int monitor_fdset_dup_fd_find(int dup_fd);
void monitor_vfprintf(FILE *stream,
const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
int monitor_vfprintf(FILE *stream,
const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
#endif /* MONITOR_H */

View File

@ -30,10 +30,10 @@ void loc_set_none(void);
void loc_set_cmdline(char **argv, int idx, int cnt);
void loc_set_file(const char *fname, int lno);
void error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
void error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
void error_vprintf_unless_qmp(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
void error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
int error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
int error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
int error_vprintf_unless_qmp(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
int error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
void error_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
void warn_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);

View File

@ -430,15 +430,14 @@ void monitor_flush(Monitor *mon)
}
/* flush at every end of line */
static void monitor_puts(Monitor *mon, const char *str)
static int monitor_puts(Monitor *mon, const char *str)
{
int i;
char c;
qemu_mutex_lock(&mon->mon_lock);
for(;;) {
c = *str++;
if (c == '\0')
break;
for (i = 0; str[i]; i++) {
c = str[i];
if (c == '\n') {
qstring_append_chr(mon->outbuf, '\r');
}
@ -448,39 +447,48 @@ static void monitor_puts(Monitor *mon, const char *str)
}
}
qemu_mutex_unlock(&mon->mon_lock);
return i;
}
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
{
char *buf;
int n;
if (!mon)
return;
return -1;
if (monitor_is_qmp(mon)) {
return;
return -1;
}
buf = g_strdup_vprintf(fmt, ap);
monitor_puts(mon, buf);
n = monitor_puts(mon, buf);
g_free(buf);
return n;
}
void monitor_printf(Monitor *mon, const char *fmt, ...)
int monitor_printf(Monitor *mon, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
monitor_vprintf(mon, fmt, ap);
ret = monitor_vprintf(mon, fmt, ap);
va_end(ap);
return ret;
}
int monitor_fprintf(FILE *stream, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
monitor_vprintf((Monitor *)stream, fmt, ap);
ret = monitor_vprintf((Monitor *)stream, fmt, ap);
va_end(ap);
return 0;
return ret;
}
static void qmp_send_response(Monitor *mon, const QDict *rsp)
@ -4535,35 +4543,32 @@ static void monitor_readline_flush(void *opaque)
/*
* Print to current monitor if we have one, else to stream.
* TODO should return int, so callers can calculate width, but that
* requires surgery to monitor_vprintf(). Left for another day.
*/
void monitor_vfprintf(FILE *stream, const char *fmt, va_list ap)
int monitor_vfprintf(FILE *stream, const char *fmt, va_list ap)
{
if (cur_mon && !monitor_cur_is_qmp()) {
monitor_vprintf(cur_mon, fmt, ap);
} else {
vfprintf(stream, fmt, ap);
return monitor_vprintf(cur_mon, fmt, ap);
}
return vfprintf(stream, fmt, ap);
}
/*
* Print to current monitor if we have one, else to stderr.
* TODO should return int, so callers can calculate width, but that
* requires surgery to monitor_vprintf(). Left for another day.
*/
void error_vprintf(const char *fmt, va_list ap)
int error_vprintf(const char *fmt, va_list ap)
{
monitor_vfprintf(stderr, fmt, ap);
return monitor_vfprintf(stderr, fmt, ap);
}
void error_vprintf_unless_qmp(const char *fmt, va_list ap)
int error_vprintf_unless_qmp(const char *fmt, va_list ap)
{
if (cur_mon && !monitor_cur_is_qmp()) {
monitor_vprintf(cur_mon, fmt, ap);
} else if (!cur_mon) {
vfprintf(stderr, fmt, ap);
if (!cur_mon) {
return vfprintf(stderr, fmt, ap);
}
if (!monitor_cur_is_qmp()) {
return monitor_vprintf(cur_mon, fmt, ap);
}
return -1;
}
static void monitor_list_append(Monitor *mon)

View File

@ -2,19 +2,22 @@
#include "qemu-common.h"
#include "qemu/error-report.h"
void error_vprintf(const char *fmt, va_list ap)
int error_vprintf(const char *fmt, va_list ap)
{
int ret;
if (g_test_initialized() && !g_test_subprocess() &&
getenv("QTEST_SILENT_ERRORS")) {
char *msg = g_strdup_vprintf(fmt, ap);
g_test_message("%s", msg);
ret = strlen(msg);
g_free(msg);
} else {
vfprintf(stderr, fmt, ap);
return ret;
}
return vfprintf(stderr, fmt, ap);
}
void error_vprintf_unless_qmp(const char *fmt, va_list ap)
int error_vprintf_unless_qmp(const char *fmt, va_list ap)
{
error_vprintf(fmt, ap);
return error_vprintf(fmt, ap);
}

View File

@ -24,22 +24,26 @@ typedef enum {
REPORT_TYPE_INFO,
} report_type;
void error_printf(const char *fmt, ...)
int error_printf(const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
error_vprintf(fmt, ap);
ret = error_vprintf(fmt, ap);
va_end(ap);
return ret;
}
void error_printf_unless_qmp(const char *fmt, ...)
int error_printf_unless_qmp(const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
error_vprintf_unless_qmp(fmt, ap);
ret = error_vprintf_unless_qmp(fmt, ap);
va_end(ap);
return ret;
}
static Location std_loc = {