2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/io/Cursor.h>
19 #include <folly/ScopeGuard.h>
21 namespace folly { namespace io {
23 void Appender::printf(const char* fmt, ...) {
30 void Appender::vprintf(const char* fmt, va_list ap) {
31 // Make a copy of ap in case we need to retry.
32 // We use ap on the first attempt, so it always gets advanced
33 // passed the used arguments. We'll only use apCopy if we need to retry.
40 // First try writing into our available data space.
41 int ret = vsnprintf(reinterpret_cast<char*>(writableData()), length(),
44 throw std::runtime_error("error formatting printf() data");
46 // vsnprintf() returns the number of characters that would be printed,
47 // not including the terminating nul.
48 if (size_t(ret) < length()) {
49 // All of the data was successfully written.
54 // There wasn't enough room for the data.
55 // Allocate more room, and then retry.
57 ret = vsnprintf(reinterpret_cast<char*>(writableData()), length(),
60 throw std::runtime_error("error formatting printf() data");
62 if (size_t(ret) >= length()) {
63 // This shouldn't ever happen.
64 throw std::runtime_error("unexpectedly out of buffer space on second "
65 "vsnprintf() attmept");