monitor: move error_vprintf back to error-report.c

The current unit tests rely on monitor.o not being linked, such
that the monitor stubs get linked instead. Since error_vprintf
is in monitor.o this allows a stub error_vprintf impl to be used
that calls g_test_message.

This takes a different approach, with error_vprintf moving
back to error-report.c such that it is always linked into the
tests. The monitor_vprintf() stub is then changed to use
g_test_message if QTEST_SILENT_ERRORS is set, otherwise it will
return -1 and trigger error_vprintf to call vfprintf.

The end result is functionally equivalent for the purposes of
the unit tests.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé
2025-09-24 16:25:45 +01:00
parent cd670accb5
commit a582a5784e
5 changed files with 31 additions and 38 deletions

View File

@@ -268,24 +268,6 @@ void monitor_printc(Monitor *mon, int c)
monitor_printf(mon, "'");
}
/*
* Print to the current human monitor if we have one, else to stderr.
*/
int error_vprintf(const char *fmt, va_list ap)
{
Monitor *cur_mon = monitor_cur();
/*
* This will return -1 if 'cur_mon' is NULL, or is QMP.
* IOW this will only print if in HMP, otherwise we
* fallback to stderr for QMP / no-monitor scenarios.
*/
int ret = monitor_vprintf(cur_mon, fmt, ap);
if (ret == -1) {
ret = vfprintf(stderr, fmt, ap);
}
return ret;
}
static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
/* Limit guest-triggerable events to 1 per second */
[QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },

View File

@@ -1,18 +0,0 @@
#include "qemu/osdep.h"
#include "qemu/error-report.h"
#include "monitor/monitor.h"
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);
return ret;
}
return vfprintf(stderr, fmt, ap);
}

View File

@@ -3,7 +3,6 @@
# below, so that it is clear who needs the stubbed functionality.
stub_ss.add(files('cpu-get-clock.c'))
stub_ss.add(files('error-printf.c'))
stub_ss.add(files('fdset.c'))
stub_ss.add(files('iothread-lock.c'))
stub_ss.add(files('is-daemonized.c'))

View File

@@ -18,5 +18,17 @@ void qapi_event_emit(QAPIEvent event, QDict *qdict)
int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
{
abort();
/*
* Pretend 'g_test_message' is our monitor console to
* stop the caller sending messages to stderr
*/
if (g_test_initialized() && !g_test_subprocess() &&
getenv("QTEST_SILENT_ERRORS")) {
char *msg = g_strdup_vprintf(fmt, ap);
g_test_message("%s", msg);
size_t ret = strlen(msg);
g_free(msg);
return ret;
}
return -1;
}

View File

@@ -29,6 +29,24 @@ bool message_with_timestamp;
bool error_with_guestname;
const char *error_guest_name;
/*
* Print to the current human monitor if we have one, else to stderr.
*/
int error_vprintf(const char *fmt, va_list ap)
{
Monitor *cur_mon = monitor_cur();
/*
* This will return -1 if 'cur_mon' is NULL, or is QMP.
* IOW this will only print if in HMP, otherwise we
* fallback to stderr for QMP / no-monitor scenarios.
*/
int ret = monitor_vprintf(cur_mon, fmt, ap);
if (ret == -1) {
ret = vfprintf(stderr, fmt, ap);
}
return ret;
}
int error_printf(const char *fmt, ...)
{
va_list ap;