Message ID | 20190411152520.10061-14-armbru@redhat.com |
---|---|
State | New |
Headers | show |
Series | Clean up and simplify around fprintf_function | expand |
* Markus Armbruster (armbru@redhat.com) wrote: > Code that doesn't want to know about current monitor vs. stdout > vs. stderr takes an fprintf_function callback and a FILE * argument to > pass to it. Actual arguments are either fprintf() and stdout or > stderr, or monitor_fprintf() and the current monitor cast to FILE *. > monitor_fprintf() casts it right back, and is otherwise identical to > monitor_printf(). The type-punning is ugly. > > New qemu_fprintf() and qemu_vprintf() address this need without type > punning: they are like fprintf() and vfprintf(), except they print to > the current monitor when passed a null FILE *. The next commits will > put them to use. > > Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com> > --- > include/qemu/qemu-print.h | 4 ++++ > util/qemu-print.c | 27 +++++++++++++++++++++++++++ > 2 files changed, 31 insertions(+) > > diff --git a/include/qemu/qemu-print.h b/include/qemu/qemu-print.h > index 8fed32bf42..40b596262f 100644 > --- a/include/qemu/qemu-print.h > +++ b/include/qemu/qemu-print.h > @@ -16,4 +16,8 @@ > int qemu_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0); > int qemu_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2); > > +int qemu_vfprintf(FILE *stream, const char *fmt, va_list ap) > + GCC_FMT_ATTR(2, 0); > +int qemu_fprintf(FILE *stream, const char *fmt, ...) GCC_FMT_ATTR(2, 3); > + > #endif > diff --git a/util/qemu-print.c b/util/qemu-print.c > index 86f9417af8..e79d6b8396 100644 > --- a/util/qemu-print.c > +++ b/util/qemu-print.c > @@ -40,3 +40,30 @@ int qemu_printf(const char *fmt, ...) > va_end(ap); > return ret; > } > + > +/* > + * Print like vfprintf() > + * Print to @stream if non-null, else to current monitor. > + */ > +int qemu_vfprintf(FILE *stream, const char *fmt, va_list ap) > +{ > + if (!stream) { > + return monitor_vprintf(cur_mon, fmt, ap); > + } > + return vfprintf(stream, fmt, ap); > +} > + > +/* > + * Print like fprintf(). > + * Print to @stream if non-null, else to current monitor. > + */ > +int qemu_fprintf(FILE *stream, const char *fmt, ...) > +{ > + va_list ap; > + int ret; > + > + va_start(ap, fmt); > + ret = qemu_vfprintf(stream, fmt, ap); > + va_end(ap); > + return ret; > +} > -- > 2.17.2 > -- Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
diff --git a/include/qemu/qemu-print.h b/include/qemu/qemu-print.h index 8fed32bf42..40b596262f 100644 --- a/include/qemu/qemu-print.h +++ b/include/qemu/qemu-print.h @@ -16,4 +16,8 @@ int qemu_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0); int qemu_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2); +int qemu_vfprintf(FILE *stream, const char *fmt, va_list ap) + GCC_FMT_ATTR(2, 0); +int qemu_fprintf(FILE *stream, const char *fmt, ...) GCC_FMT_ATTR(2, 3); + #endif diff --git a/util/qemu-print.c b/util/qemu-print.c index 86f9417af8..e79d6b8396 100644 --- a/util/qemu-print.c +++ b/util/qemu-print.c @@ -40,3 +40,30 @@ int qemu_printf(const char *fmt, ...) va_end(ap); return ret; } + +/* + * Print like vfprintf() + * Print to @stream if non-null, else to current monitor. + */ +int qemu_vfprintf(FILE *stream, const char *fmt, va_list ap) +{ + if (!stream) { + return monitor_vprintf(cur_mon, fmt, ap); + } + return vfprintf(stream, fmt, ap); +} + +/* + * Print like fprintf(). + * Print to @stream if non-null, else to current monitor. + */ +int qemu_fprintf(FILE *stream, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = qemu_vfprintf(stream, fmt, ap); + va_end(ap); + return ret; +}
Code that doesn't want to know about current monitor vs. stdout vs. stderr takes an fprintf_function callback and a FILE * argument to pass to it. Actual arguments are either fprintf() and stdout or stderr, or monitor_fprintf() and the current monitor cast to FILE *. monitor_fprintf() casts it right back, and is otherwise identical to monitor_printf(). The type-punning is ugly. New qemu_fprintf() and qemu_vprintf() address this need without type punning: they are like fprintf() and vfprintf(), except they print to the current monitor when passed a null FILE *. The next commits will put them to use. Signed-off-by: Markus Armbruster <armbru@redhat.com> --- include/qemu/qemu-print.h | 4 ++++ util/qemu-print.c | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)