master
c 98 lines 2.27 KB
Raw
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 "monitor/hmp.h"
16 #include "qom/object.h"
17 #include "qemu/qemu-print.h"
18
19 /*
20 * Print like vprintf().
21 * Print to current monitor if we have one, else to stdout.
22 * (if the monitor is QMP, fail without printing anything)
23 */
24 int qemu_vprintf(const char *fmt, va_list ap)
25 {
26 Monitor *cur_mon = monitor_cur();
27
28 /* for all monitors: QMP & HMP */
29 if (cur_mon) {
30 #ifdef CONFIG_HMP
31 /* don't use monitor_cur_hmp(), to avoid a second lookup */
32 MonitorHMP *hmp = (MonitorHMP *)
33 object_dynamic_cast(OBJECT(cur_mon), TYPE_MONITOR_HMP);
34 if (!hmp) {
35 return -1;
36 }
37 return monitor_hmp_vprintf(hmp, fmt, ap);
38 #else
39 return -1;
40 #endif
41 }
42 return vprintf(fmt, ap);
43 }
44
45 /*
46 * Print like printf().
47 * Print to current monitor if we have one, else to stdout.
48 * (if the monitor is QMP, fail without printing anything)
49 */
50 int qemu_printf(const char *fmt, ...)
51 {
52 va_list ap;
53 int ret;
54
55 va_start(ap, fmt);
56 ret = qemu_vprintf(fmt, ap);
57 va_end(ap);
58 return ret;
59 }
60
61 /*
62 * Print like vfprintf()
63 * Print to @stream if non-null, else to current HMP monitor if we
64 * have one, else fail without printing anything.
65 * Return number of characters printed on success, negative value on
66 * error.
67 */
68 int qemu_vfprintf(FILE *stream, const char *fmt, va_list ap)
69 {
70 #ifdef CONFIG_HMP
71 if (!stream) {
72 MonitorHMP *hmp = monitor_cur_hmp();
73 if (!hmp) {
74 return -1;
75 }
76 return monitor_hmp_vprintf(hmp, fmt, ap);
77 }
78 #endif
79 return vfprintf(stream, fmt, ap);
80 }
81
82 /*
83 * Print like fprintf().
84 * Print to @stream if non-null, else to current HMP monitor if we
85 * have one, else fail without printing anything.
86 * Return number of characters printed on success, negative value on
87 * error.
88 */
89 int qemu_fprintf(FILE *stream, const char *fmt, ...)
90 {
91 va_list ap;
92 int ret;
93
94 va_start(ap, fmt);
95 ret = qemu_vfprintf(stream, fmt, ap);
96 va_end(ap);
97 return ret;
98 }