Fix copyright lines
[folly.git] / folly / experimental / logging / FileHandlerFactory.cpp
1 /*
2  * Copyright 2017-present Facebook, Inc.
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 #include <folly/experimental/logging/FileHandlerFactory.h>
17
18 #include <folly/experimental/logging/FileWriterFactory.h>
19 #include <folly/experimental/logging/StandardLogHandler.h>
20 #include <folly/experimental/logging/StandardLogHandlerFactory.h>
21
22 namespace folly {
23
24 class FileHandlerFactory::WriterFactory
25     : public StandardLogHandlerFactory::WriterFactory {
26  public:
27   bool processOption(StringPiece name, StringPiece value) override {
28     if (name == "path") {
29       path_ = value.str();
30       return true;
31     }
32
33     // TODO: In the future it would be nice to support log rotation, and
34     // add parameters to control when the log file should be rotated.
35
36     return fileWriterFactory_.processOption(name, value);
37   }
38
39   std::shared_ptr<LogWriter> createWriter() override {
40     // Get the output file to use
41     if (path_.empty()) {
42       throw std::invalid_argument("no path specified for file handler");
43     }
44     return fileWriterFactory_.createWriter(
45         File{path_, O_WRONLY | O_APPEND | O_CREAT});
46   }
47
48   std::string path_;
49   FileWriterFactory fileWriterFactory_;
50 };
51
52 std::shared_ptr<LogHandler> FileHandlerFactory::createHandler(
53     const Options& options) {
54   WriterFactory writerFactory;
55   return StandardLogHandlerFactory::createHandler(
56       getType(), &writerFactory, options);
57 }
58
59 } // namespace folly