Ot ten Thije | 2ff39a3 | 2010-10-06 17:48:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "outputchannel.h" |
| 19 | #include "qemu-common.h" |
| 20 | |
| 21 | struct OutputChannel { |
| 22 | void* opaque; /* caller-specific information */ |
| 23 | OutputChannelPrintf printf; /* callback function to do the printing */ |
| 24 | unsigned int written; /* number of bytes written to the channel */ |
| 25 | }; |
| 26 | |
| 27 | OutputChannel* output_channel_alloc(void* opaque, OutputChannelPrintf cb) |
| 28 | { |
David 'Digit' Turner | e771abe | 2011-01-06 16:57:08 +0100 | [diff] [blame] | 29 | OutputChannel* oc = qemu_mallocz(sizeof(*oc)); |
Ot ten Thije | 2ff39a3 | 2010-10-06 17:48:15 +0100 | [diff] [blame] | 30 | oc->printf = cb; |
| 31 | oc->opaque = opaque; |
| 32 | oc->written = 0; |
| 33 | |
| 34 | return oc; |
| 35 | } |
| 36 | |
| 37 | int output_channel_printf(OutputChannel* oc, const char* fmt, ...) |
| 38 | { |
| 39 | int ret; |
| 40 | va_list ap; |
| 41 | va_start(ap, fmt); |
| 42 | ret = oc->printf(oc->opaque, fmt, ap); |
| 43 | va_end(ap); |
| 44 | |
| 45 | /* Don't count errors and no-ops towards number of bytes written */ |
| 46 | if (ret > 0) { |
| 47 | oc->written += ret; |
| 48 | } |
| 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | void output_channel_free(OutputChannel* oc) |
| 54 | { |
| 55 | free(oc); |
| 56 | } |
| 57 | |
| 58 | unsigned int output_channel_written(OutputChannel* oc) |
| 59 | { |
| 60 | return oc->written; |
| 61 | } |