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/FileHandlerFactory.h>
18 #include <folly/Conv.h>
19 #include <folly/experimental/logging/AsyncFileWriter.h>
20 #include <folly/experimental/logging/ImmediateFileWriter.h>
21 #include <folly/experimental/logging/StandardLogHandler.h>
22 #include <folly/experimental/logging/StandardLogHandlerFactory.h>
24 using std::make_shared;
25 using std::shared_ptr;
30 class FileHandlerFactory::WriterFactory
31 : public StandardLogHandlerFactory::WriterFactory {
33 bool processOption(StringPiece name, StringPiece value) override {
35 if (!stream_.empty()) {
36 throw std::invalid_argument(
37 "cannot specify both \"path\" and \"stream\" "
38 "parameters for FileHandlerFactory");
42 } else if (name == "stream") {
44 throw std::invalid_argument(
45 "cannot specify both \"path\" and \"stream\" "
46 "parameters for FileHandlerFactory");
48 stream_ = value.str();
50 } else if (name == "async") {
51 async_ = to<bool>(value);
53 } else if (name == "max_buffer_size") {
54 auto size = to<size_t>(value);
56 throw std::invalid_argument(to<string>("must be a postive number"));
58 maxBufferSize_ = size;
65 std::shared_ptr<LogWriter> createWriter() override {
66 // Get the output file to use
69 outputFile = File{path_, O_WRONLY | O_APPEND | O_CREAT};
70 } else if (stream_ == "stderr") {
71 outputFile = File{STDERR_FILENO, /* ownsFd */ false};
72 } else if (stream_ == "stdout") {
73 outputFile = File{STDOUT_FILENO, /* ownsFd */ false};
75 throw std::invalid_argument(to<string>(
76 "unknown stream for FileHandlerFactory: \"",
78 "\" expected one of stdout or stderr"));
81 // Determine whether we should use ImmediateFileWriter or AsyncFileWriter
83 auto asyncWriter = make_shared<AsyncFileWriter>(std::move(outputFile));
84 if (maxBufferSize_.hasValue()) {
85 asyncWriter->setMaxBufferSize(maxBufferSize_.value());
89 if (maxBufferSize_.hasValue()) {
90 throw std::invalid_argument(to<string>(
91 "the \"max_buffer_size\" option is only valid for async file "
94 return make_shared<ImmediateFileWriter>(std::move(outputFile));
101 Optional<size_t> maxBufferSize_;
104 std::shared_ptr<LogHandler> FileHandlerFactory::createHandler(
105 const Options& options) {
106 WriterFactory writerFactory;
107 return StandardLogHandlerFactory::createHandler(
108 getType(), &writerFactory, options);