Markus Armbruster | 637de4d | 2019-04-17 21:06:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Print to stream or current monitor |
| 3 | * |
| 4 | * Copyright (C) 2019 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Markus Armbruster <armbru@redhat.com>, |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "monitor/monitor.h" |
| 15 | #include "qemu/qemu-print.h" |
| 16 | |
| 17 | /* |
| 18 | * Print like vprintf(). |
| 19 | * Print to current monitor if we have one, else to stdout. |
| 20 | */ |
| 21 | int qemu_vprintf(const char *fmt, va_list ap) |
| 22 | { |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 23 | Monitor *cur_mon = monitor_cur(); |
Markus Armbruster | 637de4d | 2019-04-17 21:06:37 +0200 | [diff] [blame] | 24 | if (cur_mon) { |
| 25 | return monitor_vprintf(cur_mon, fmt, ap); |
| 26 | } |
| 27 | return vprintf(fmt, ap); |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | * Print like printf(). |
| 32 | * Print to current monitor if we have one, else to stdout. |
| 33 | */ |
| 34 | int qemu_printf(const char *fmt, ...) |
| 35 | { |
| 36 | va_list ap; |
| 37 | int ret; |
| 38 | |
| 39 | va_start(ap, fmt); |
| 40 | ret = qemu_vprintf(fmt, ap); |
| 41 | va_end(ap); |
| 42 | return ret; |
| 43 | } |
Markus Armbruster | 19aaa4c | 2019-04-17 21:18:01 +0200 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Print like vfprintf() |
| 47 | * Print to @stream if non-null, else to current monitor. |
| 48 | */ |
| 49 | int qemu_vfprintf(FILE *stream, const char *fmt, va_list ap) |
| 50 | { |
| 51 | if (!stream) { |
Kevin Wolf | 947e474 | 2020-10-05 17:58:44 +0200 | [diff] [blame] | 52 | return monitor_vprintf(monitor_cur(), fmt, ap); |
Markus Armbruster | 19aaa4c | 2019-04-17 21:18:01 +0200 | [diff] [blame] | 53 | } |
| 54 | return vfprintf(stream, fmt, ap); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Print like fprintf(). |
| 59 | * Print to @stream if non-null, else to current monitor. |
| 60 | */ |
| 61 | int qemu_fprintf(FILE *stream, const char *fmt, ...) |
| 62 | { |
| 63 | va_list ap; |
| 64 | int ret; |
| 65 | |
| 66 | va_start(ap, fmt); |
| 67 | ret = qemu_vfprintf(stream, fmt, ap); |
| 68 | va_end(ap); |
| 69 | return ret; |
| 70 | } |