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/StreamHandlerFactory.h>
18 #include <folly/Conv.h>
19 #include <folly/experimental/logging/FileWriterFactory.h>
20 #include <folly/experimental/logging/StandardLogHandler.h>
21 #include <folly/experimental/logging/StandardLogHandlerFactory.h>
25 class StreamHandlerFactory::WriterFactory
26 : public StandardLogHandlerFactory::WriterFactory {
28 bool processOption(StringPiece name, StringPiece value) override {
29 if (name == "stream") {
30 stream_ = value.str();
33 return fileWriterFactory_.processOption(name, value);
36 std::shared_ptr<LogWriter> createWriter() override {
37 // Get the output file to use
39 if (stream_.empty()) {
40 throw std::invalid_argument(
41 "no stream name specified for stream handler");
42 } else if (stream_ == "stderr") {
43 outputFile = File{STDERR_FILENO, /* ownsFd */ false};
44 } else if (stream_ == "stdout") {
45 outputFile = File{STDOUT_FILENO, /* ownsFd */ false};
47 throw std::invalid_argument(to<std::string>(
50 "\": expected one of stdout or stderr"));
53 return fileWriterFactory_.createWriter(std::move(outputFile));
57 FileWriterFactory fileWriterFactory_;
60 std::shared_ptr<LogHandler> StreamHandlerFactory::createHandler(
61 const Options& options) {
62 WriterFactory writerFactory;
63 return StandardLogHandlerFactory::createHandler(
64 getType(), &writerFactory, options);