From c55510b72218a2d0b263eda006fd7aac1496b694 Mon Sep 17 00:00:00 2001 From: Cornelia Huck Date: Thu, 30 Aug 2018 16:59:01 +0200 Subject: qemu-error: add {error, warn}_report_once_cond Add two functions to print an error/warning report once depending on a passed-in condition variable and flip it if printed. This is useful if you want to print a message not once-globally, but e.g. once-per-device. Inspired by warn_once() in hw/vfio/ccw.c, which has been replaced with warn_report_once_cond(). Signed-off-by: Cornelia Huck Message-Id: <20180830145902.27376-2-cohuck@redhat.com> Reviewed-by: Markus Armbruster [Function comments reworded] Signed-off-by: Markus Armbruster --- util/qemu-error.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'util') diff --git a/util/qemu-error.c b/util/qemu-error.c index a25d3b94c6..4ab428f7e4 100644 --- a/util/qemu-error.c +++ b/util/qemu-error.c @@ -310,3 +310,43 @@ void info_report(const char *fmt, ...) vreport(REPORT_TYPE_INFO, fmt, ap); va_end(ap); } + +/* + * Like error_report(), except print just once. + * If *printed is false, print the message, and flip *printed to true. + * Return whether the message was printed. + */ +bool error_report_once_cond(bool *printed, const char *fmt, ...) +{ + va_list ap; + + assert(printed); + if (*printed) { + return false; + } + *printed = true; + va_start(ap, fmt); + vreport(REPORT_TYPE_ERROR, fmt, ap); + va_end(ap); + return true; +} + +/* + * Like warn_report(), except print just once. + * If *printed is false, print the message, and flip *printed to true. + * Return whether the message was printed. + */ +bool warn_report_once_cond(bool *printed, const char *fmt, ...) +{ + va_list ap; + + assert(printed); + if (*printed) { + return false; + } + *printed = true; + va_start(ap, fmt); + vreport(REPORT_TYPE_WARNING, fmt, ap); + va_end(ap); + return true; +} -- cgit v1.2.3-55-g7522