logging: add a LogConfig::update() method
[folly.git] / folly / experimental / logging / LogConfig.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 <string>
19 #include <unordered_map>
20
21 #include <folly/experimental/logging/LogCategoryConfig.h>
22 #include <folly/experimental/logging/LogHandlerConfig.h>
23
24 namespace folly {
25
26 /**
27  * LogConfig contains configuration for the LoggerDB.
28  *
29  * This includes information about the log levels for log categories,
30  * as well as what log handlers are configured and which categories they are
31  * attached to.
32  */
33 class LogConfig {
34  public:
35   using CategoryConfigMap = std::unordered_map<std::string, LogCategoryConfig>;
36   using HandlerConfigMap = std::unordered_map<std::string, LogHandlerConfig>;
37
38   LogConfig() = default;
39   explicit LogConfig(
40       HandlerConfigMap handlerConfigs,
41       CategoryConfigMap catConfigs)
42       : handlerConfigs_{std::move(handlerConfigs)},
43         categoryConfigs_{std::move(catConfigs)} {}
44
45   const CategoryConfigMap& getCategoryConfigs() const {
46     return categoryConfigs_;
47   }
48   const HandlerConfigMap& getHandlerConfigs() const {
49     return handlerConfigs_;
50   }
51
52   bool operator==(const LogConfig& other) const;
53   bool operator!=(const LogConfig& other) const;
54
55   /**
56    * Update this LogConfig object by merging in settings from another
57    * LogConfig.
58    *
59    * All LogHandler settings from the other LogConfig will be inserted into
60    * this LogConfig.  If a log handler with the same name was already defined
61    * in this LogConfig it will be replaced with the new settings.
62    *
63    * All LogCategory settings from the other LogConfig will be inserted into
64    * this LogConfig.  If a log category with the same name was already defined
65    * in this LogConfig, its settings will be updated with settings from the
66    * other LogConfig.  However, if the other LogConfig does not define handler
67    * settings for the category it will retain its current handler settings.
68    *
69    * This method allows LogConfig objects to be combined before applying them.
70    * Using LogConfig::update() will produce the same results as if
71    * LoggerDB::updateConfig() had been called with both configs sequentially.
72    * In other words, this operation:
73    *
74    *   configA.update(configB);
75    *   loggerDB.updateConfig(configA);
76    *
77    * will produce the same results as:
78    *
79    *   loggerDB.updateConfig(configA);
80    *   loggerDB.updateConfig(configA);
81    */
82   void update(const LogConfig& other);
83
84  private:
85   HandlerConfigMap handlerConfigs_;
86   CategoryConfigMap categoryConfigs_;
87 };
88
89 } // namespace folly