logging: rename the `DEBUG` log level to `DBG`
[folly.git] / folly / experimental / logging / StreamHandlerFactory.cpp
1 /*
2  * Copyright 2004-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/StreamHandlerFactory.h>
17
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>
22
23 namespace folly {
24
25 class StreamHandlerFactory::WriterFactory
26     : public StandardLogHandlerFactory::WriterFactory {
27  public:
28   bool processOption(StringPiece name, StringPiece value) override {
29     if (name == "stream") {
30       stream_ = value.str();
31       return true;
32     }
33     return fileWriterFactory_.processOption(name, value);
34   }
35
36   std::shared_ptr<LogWriter> createWriter() override {
37     // Get the output file to use
38     File outputFile;
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};
46     } else {
47       throw std::invalid_argument(to<std::string>(
48           "unknown stream \"",
49           stream_,
50           "\": expected one of stdout or stderr"));
51     }
52
53     return fileWriterFactory_.createWriter(std::move(outputFile));
54   }
55
56   std::string stream_;
57   FileWriterFactory fileWriterFactory_;
58 };
59
60 std::shared_ptr<LogHandler> StreamHandlerFactory::createHandler(
61     const Options& options) {
62   WriterFactory writerFactory;
63   return StandardLogHandlerFactory::createHandler(
64       getType(), &writerFactory, options);
65 }
66
67 } // namespace folly