monitor: refactor error_vprintf()
The monitor_vprintf() code will return -1 if either the monitor is NULL, or the monitor is QMP. The error_vprintf() code can take advantage of this to avoid having to duplicate the same checks, and instead simply look at the return value. Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Daniel P. Berrangé committed
Sep 24, 2025 at 16:12 UTC
cd670accb5987e68d4e5967922bc79efc10a5e02
1 file changed
+10
-5
monitor/monitor.c
+10
-5
@@ -269,16 +269,21 @@ void monitor_printc(Monitor *mon, int c)
269
}
270
271
/*
272
- * Print to current monitor if we have one, else to stderr.
272
+ * Print to the current human monitor if we have one, else to stderr.
273
*/
274
int error_vprintf(const char *fmt, va_list ap)
275
{
276
Monitor *cur_mon = monitor_cur();
277
-
278
- if (cur_mon && !monitor_cur_is_qmp()) {
279
- return monitor_vprintf(cur_mon, fmt, ap);
277
+ /*
278
+ * This will return -1 if 'cur_mon' is NULL, or is QMP.
279
+ * IOW this will only print if in HMP, otherwise we
280
+ * fallback to stderr for QMP / no-monitor scenarios.
281
+ */
282
+ int ret = monitor_vprintf(cur_mon, fmt, ap);
283
+ if (ret == -1) {
284
+ ret = vfprintf(stderr, fmt, ap);
285
}
281
- return vfprintf(stderr, fmt, ap);
286
+ return ret;
287
}
288
289
static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {