logging: add a FileHandlerFactory class
[folly.git] / folly / experimental / logging / StandardLogHandler.h
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 #pragma once
17
18 #include <memory>
19
20 #include <folly/File.h>
21 #include <folly/Range.h>
22 #include <folly/experimental/logging/LogHandler.h>
23
24 namespace folly {
25
26 class LogFormatter;
27 class LogWriter;
28
29 /**
30  * StandardLogHandler is a LogHandler implementation that uses a LogFormatter
31  * class to serialize the LogMessage into a string, and then gives it to a
32  * LogWriter object.
33  *
34  * This basically is a simple glue class that helps chain together
35  * configurable LogFormatter and LogWriter objects.
36  *
37  * StandardLogHandler also supports ignoring messages less than a specific
38  * LogLevel.  By default it processes all messages.
39  */
40 class StandardLogHandler : public LogHandler {
41  public:
42   StandardLogHandler(
43       std::shared_ptr<LogFormatter> formatter,
44       std::shared_ptr<LogWriter> writer);
45   ~StandardLogHandler();
46
47   /**
48    * Get the LogFormatter used by this handler.
49    */
50   const std::shared_ptr<LogFormatter>& getFormatter() const {
51     return formatter_;
52   }
53
54   /**
55    * Get the LogWriter used by this handler.
56    */
57   const std::shared_ptr<LogWriter>& getWriter() const {
58     return writer_;
59   }
60
61   /**
62    * Get the handler's current LogLevel.
63    *
64    * Messages less than this LogLevel will be ignored.  This defaults to
65    * LogLevel::NONE when the handler is constructed.
66    */
67   LogLevel getLevel() const {
68     return level_.load(std::memory_order_acquire);
69   }
70
71   /**
72    * Set the handler's current LogLevel.
73    *
74    * Messages less than this LogLevel will be ignored.
75    */
76   void setLevel(LogLevel level) {
77     return level_.store(level, std::memory_order_release);
78   }
79
80   void handleMessage(
81       const LogMessage& message,
82       const LogCategory* handlerCategory) override;
83
84   void flush() override;
85
86  private:
87   std::atomic<LogLevel> level_{LogLevel::NONE};
88
89   // The formatter_ and writer_ member variables are const, and cannot be
90   // modified after the StandardLogHandler is constructed.  This allows them to
91   // be accessed without locking when handling a message.  To change these
92   // values, create a new StandardLogHandler object and replace the old handler
93   // with the new one in the LoggerDB.
94
95   const std::shared_ptr<LogFormatter> formatter_;
96   const std::shared_ptr<LogWriter> writer_;
97 };
98 } // namespace folly