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