2 * Copyright 2004-present 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/experimental/logging/LogStreamProcessor.h>
20 #include <folly/experimental/logging/LogStream.h>
23 void LogStreamProcessor::operator&(std::ostream& stream) noexcept {
24 // Note that processMessage() is not noexcept and theoretically may throw.
25 // However, the only exception that should be possible is std::bad_alloc if
26 // we fail to allocate memory. We intentionally let our noexcept specifier
27 // crash in that case, since the program likely won't be able to continue
30 // Any other error here is unexpected and we also want to fail hard
31 // in that situation too.
32 auto& logStream = static_cast<LogStream&>(stream);
33 category_->processMessage(LogMessage{category_,
37 extractMessageString(logStream)});
40 void LogStreamProcessor::operator&(LogStream&& stream) noexcept {
41 // This version of operator&() is generally only invoked when
42 // no streaming arguments were supplied to the logging macro.
43 // Therefore we don't bother calling extractMessageString(stream),
44 // and just directly use message_.
45 DCHECK(stream.empty());
47 category_->processMessage(LogMessage{
48 category_, level_, filename_, lineNumber_, std::move(message_)});
51 std::string LogStreamProcessor::extractMessageString(
52 LogStream& stream) noexcept {
54 return std::move(message_);
57 if (message_.empty()) {
58 return stream.extractString();
60 message_.append(stream.extractString());
61 return std::move(message_);