LogStream.cpp 1.6 KB

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