/src/qemu/util/qemu-print.c
Line | Count | Source |
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 | 0 | { |
26 | 0 | Monitor *cur_mon = monitor_cur(); |
27 | | |
28 | | /* for all monitors: QMP & HMP */ |
29 | 0 | if (cur_mon) { |
30 | 0 | #ifdef CONFIG_HMP |
31 | | /* don't use monitor_cur_hmp(), to avoid a second lookup */ |
32 | 0 | MonitorHMP *hmp = (MonitorHMP *) |
33 | 0 | object_dynamic_cast(OBJECT(cur_mon), TYPE_MONITOR_HMP); |
34 | 0 | if (!hmp) { |
35 | 0 | return -1; |
36 | 0 | } |
37 | 0 | return monitor_hmp_vprintf(hmp, fmt, ap); |
38 | | #else |
39 | | return -1; |
40 | | #endif |
41 | 0 | } |
42 | 0 | return vprintf(fmt, ap); |
43 | 0 | } |
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 | 0 | { |
52 | 0 | va_list ap; |
53 | 0 | int ret; |
54 | |
|
55 | 0 | va_start(ap, fmt); |
56 | 0 | ret = qemu_vprintf(fmt, ap); |
57 | 0 | va_end(ap); |
58 | 0 | return ret; |
59 | 0 | } |
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 | 0 | { |
70 | 0 | #ifdef CONFIG_HMP |
71 | 0 | if (!stream) { |
72 | 0 | MonitorHMP *hmp = monitor_cur_hmp(); |
73 | 0 | if (!hmp) { |
74 | 0 | return -1; |
75 | 0 | } |
76 | 0 | return monitor_hmp_vprintf(hmp, fmt, ap); |
77 | 0 | } |
78 | 0 | #endif |
79 | 0 | return vfprintf(stream, fmt, ap); |
80 | 0 | } |
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 | 0 | { |
91 | 0 | va_list ap; |
92 | 0 | int ret; |
93 | |
|
94 | 0 | va_start(ap, fmt); |
95 | 0 | ret = qemu_vfprintf(stream, fmt, ap); |
96 | | va_end(ap); |
97 | 0 | return ret; |
98 | 0 | } |