947d76d7f2ba2c7e3ee0e212fa820d82a742ee64
[folly.git] / folly / experimental / logging / LogStream.cpp
1 /*
2  * Copyright 2004-present 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/experimental/logging/LogStream.h>
17
18 namespace folly {
19
20 LogStreamBuffer::int_type LogStreamBuffer::overflow(int_type ch) {
21   auto currentSize = str_.size();
22   size_t newSize;
23   if (currentSize == 0) {
24     newSize = kInitialCapacity;
25   } else {
26     // Increase by 1.25 each time
27     newSize = currentSize + (currentSize >> 2);
28   }
29
30   try {
31     str_.resize(newSize);
32
33     if (ch == EOF) {
34       setp((&str_.front()) + currentSize, (&str_.front()) + newSize);
35       return 'x';
36     } else {
37       str_[currentSize] = static_cast<char>(ch);
38       setp((&str_.front()) + currentSize + 1, (&str_.front()) + newSize);
39       return ch;
40     }
41   } catch (const std::exception& ex) {
42     // Return EOF to indicate that the operation failed.
43     // In general the only exception we really expect to see here is
44     // std::bad_alloc() from the str_.resize() call.
45     return EOF;
46   }
47 }
48
49 LogStream::LogStream() {
50   rdbuf(&buffer_);
51 }
52
53 LogStream::~LogStream() {}
54 }