allow AsyncSignalHandler to attach and detach from an EventBase
[folly.git] / folly / io / Cursor.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
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 #include <folly/io/Cursor.h>
17
18 #include <cstdio>
19 #include <folly/ScopeGuard.h>
20
21 namespace folly { namespace io {
22
23 void Appender::printf(const char* fmt, ...) {
24   va_list ap;
25   va_start(ap, fmt);
26   vprintf(fmt, ap);
27   va_end(ap);
28 }
29
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.
34   va_list apCopy;
35   va_copy(apCopy, ap);
36   SCOPE_EXIT {
37     va_end(apCopy);
38   };
39
40   // First try writing into our available data space.
41   int ret = vsnprintf(reinterpret_cast<char*>(writableData()), length(),
42                       fmt, ap);
43   if (ret < 0) {
44     throw std::runtime_error("error formatting printf() data");
45   }
46   auto len = size_t(ret);
47   // vsnprintf() returns the number of characters that would be printed,
48   // not including the terminating nul.
49   if (len < length()) {
50     // All of the data was successfully written.
51     append(len);
52     return;
53   }
54
55   // There wasn't enough room for the data.
56   // Allocate more room, and then retry.
57   ensure(len + 1);
58   ret = vsnprintf(reinterpret_cast<char*>(writableData()), length(),
59                   fmt, apCopy);
60   if (ret < 0) {
61     throw std::runtime_error("error formatting printf() data");
62   }
63   len = size_t(ret);
64   if (len >= length()) {
65     // This shouldn't ever happen.
66     throw std::runtime_error("unexpectedly out of buffer space on second "
67                              "vsnprintf() attmept");
68   }
69   append(len);
70 }
71
72 }}  // folly::io