| 1 | //===-- Implementation of vprintf -------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/stdio/vprintf.h" |
| 10 | #include "src/__support/OSUtil/io.h" |
| 11 | #include "src/__support/arg_list.h" |
| 12 | #include "src/__support/macros/config.h" |
| 13 | #include "src/stdio/printf_core/core_structs.h" |
| 14 | #include "src/stdio/printf_core/printf_main.h" |
| 15 | #include "src/stdio/printf_core/writer.h" |
| 16 | |
| 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
| 19 | |
| 20 | namespace LIBC_NAMESPACE_DECL { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) { |
| 25 | write_to_stderr(new_str); |
| 26 | return printf_core::WRITE_OK; |
| 27 | } |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | LLVM_LIBC_FUNCTION(int, vprintf, |
| 32 | (const char *__restrict format, va_list vlist)) { |
| 33 | internal::ArgList args(vlist); // This holder class allows for easier copying |
| 34 | // and pointer semantics, as well as handling |
| 35 | // destruction automatically. |
| 36 | constexpr size_t BUFF_SIZE = 1024; |
| 37 | char buffer[BUFF_SIZE]; |
| 38 | |
| 39 | printf_core::WriteBuffer<printf_core::WriteMode::FLUSH_TO_STREAM> wb( |
| 40 | buffer, BUFF_SIZE, &raw_write_hook, nullptr); |
| 41 | printf_core::Writer<printf_core::WriteMode::FLUSH_TO_STREAM> writer(wb); |
| 42 | |
| 43 | int retval = printf_core::printf_main(&writer, format, args); |
| 44 | |
| 45 | int flushval = wb.overflow_write("" ); |
| 46 | if (flushval != printf_core::WRITE_OK) |
| 47 | retval = flushval; |
| 48 | |
| 49 | return retval; |
| 50 | } |
| 51 | |
| 52 | } // namespace LIBC_NAMESPACE_DECL |
| 53 | |