1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 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 17 #include <folly/logging/FileWriterFactory.h> 18 19 #include <folly/Conv.h> 20 #include <folly/File.h> 21 #include <folly/logging/AsyncFileWriter.h> 22 #include <folly/logging/ImmediateFileWriter.h> 23 24 using std::make_shared; 25 using std::string; 26 27 namespace folly { 28 processOption(StringPiece name,StringPiece value)29bool FileWriterFactory::processOption(StringPiece name, StringPiece value) { 30 if (name == "async") { 31 async_ = to<bool>(value); 32 return true; 33 } else if (name == "max_buffer_size") { 34 auto size = to<size_t>(value); 35 if (size == 0) { 36 throw std::invalid_argument(to<string>("must be a positive integer")); 37 } 38 maxBufferSize_ = size; 39 return true; 40 } else { 41 return false; 42 } 43 } 44 createWriter(File file)45std::shared_ptr<LogWriter> FileWriterFactory::createWriter(File file) { 46 // Determine whether we should use ImmediateFileWriter or AsyncFileWriter 47 if (async_) { 48 auto asyncWriter = make_shared<AsyncFileWriter>(std::move(file)); 49 if (maxBufferSize_.has_value()) { 50 asyncWriter->setMaxBufferSize(maxBufferSize_.value()); 51 } 52 return asyncWriter; 53 } else { 54 if (maxBufferSize_.has_value()) { 55 throw std::invalid_argument(to<string>( 56 "the \"max_buffer_size\" option is only valid for async file " 57 "handlers")); 58 } 59 return make_shared<ImmediateFileWriter>(std::move(file)); 60 } 61 } 62 63 } // namespace folly 64