585468d8b614a8ac8a1d9cb360e0248985661292
[folly.git] / folly / experimental / logging / LogHandler.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 <atomic>
19
20 #include <folly/experimental/logging/LogLevel.h>
21
22 namespace folly {
23
24 class LogCategory;
25 class LogMessage;
26
27 /**
28  * LogHandler represents a generic API for processing log messages.
29  *
30  * LogHandlers have an associated log level.  The LogHandler will discard any
31  * messages below its log level.  This allows specific LogHandlers to perform
32  * additional filtering of messages even if the messages were enabled at the
33  * LogCategory level.  For instance, a single LogCategory may have two
34  * LogHandlers attached, one that logs locally to a file, and one that sends
35  * messages to a remote logging service.  The local LogHandler may be
36  * configured to record all messages, but the remote LogHandler may want to
37  * only process ERROR messages and above, even when debug logging is enabled
38  * for this LogCategory.
39  *
40  * By default the LogHandler level is set to LogLevel::NONE, which means that
41  * all log messages will be processed.
42  */
43 class LogHandler {
44  public:
45   LogHandler() = default;
46   virtual ~LogHandler() = default;
47
48   /**
49    * log() is called when a log message is processed by a LogCategory that this
50    * handler is attached to.
51    *
52    * log() performs a level check, and calls handleMessage() if it passes.
53    *
54    * @param message The LogMessage objet.
55    * @param handlerCategory  The LogCategory that invoked log().  This is the
56    *     category that this LogHandler is attached to.  Note that this may be
57    *     different than the category that this message was originally logged
58    *     at.  message->getCategory() returns the category of the log message.
59    */
60   void log(const LogMessage& message, const LogCategory* handlerCategory);
61
62   LogLevel getLevel() const {
63     return level_.load(std::memory_order_acquire);
64   }
65   void setLevel(LogLevel level) {
66     return level_.store(level, std::memory_order_release);
67   }
68
69  protected:
70   /**
71    * handleMessage() is invoked to process a LogMessage.
72    *
73    * This must be implemented by LogHandler subclasses.
74    *
75    * handleMessage() will always be invoked from the thread that logged the
76    * message.  LogMessage::getThreadID() contains the thread ID, but the
77    * LogHandler can also include any other thread-local state they desire, and
78    * this will always be data for the thread that originated the log message.
79    */
80   virtual void handleMessage(
81       const LogMessage& message,
82       const LogCategory* handlerCategory) = 0;
83
84  private:
85   std::atomic<LogLevel> level_{LogLevel::NONE};
86 };
87 }