2 * Copyright 2017-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/FileWriterFactory.h>
18 #include <folly/Conv.h>
19 #include <folly/File.h>
20 #include <folly/experimental/logging/AsyncFileWriter.h>
21 #include <folly/experimental/logging/ImmediateFileWriter.h>
23 using std::make_shared;
28 bool FileWriterFactory::processOption(StringPiece name, StringPiece value) {
29 if (name == "async") {
30 async_ = to<bool>(value);
32 } else if (name == "max_buffer_size") {
33 auto size = to<size_t>(value);
35 throw std::invalid_argument(to<string>("must be a positive integer"));
37 maxBufferSize_ = size;
44 std::shared_ptr<LogWriter> FileWriterFactory::createWriter(File file) {
45 // Determine whether we should use ImmediateFileWriter or AsyncFileWriter
47 auto asyncWriter = make_shared<AsyncFileWriter>(std::move(file));
48 if (maxBufferSize_.hasValue()) {
49 asyncWriter->setMaxBufferSize(maxBufferSize_.value());
53 if (maxBufferSize_.hasValue()) {
54 throw std::invalid_argument(to<string>(
55 "the \"max_buffer_size\" option is only valid for async file "
58 return make_shared<ImmediateFileWriter>(std::move(file));