logging: add a LogConfig::update() method
[folly.git] / folly / experimental / logging / LogConfig.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/LogConfig.h>
17
18 namespace folly {
19
20 bool LogConfig::operator==(const LogConfig& other) const {
21   return handlerConfigs_ == other.handlerConfigs_ &&
22       categoryConfigs_ == other.categoryConfigs_;
23 }
24
25 bool LogConfig::operator!=(const LogConfig& other) const {
26   return !(*this == other);
27 }
28
29 void LogConfig::update(const LogConfig& other) {
30   // Update handlerConfigs_ with all of the entries from the other LogConfig.
31   // Any entries already present in our handlerConfigs_ are replaced wholesale.
32   for (const auto& entry : other.handlerConfigs_) {
33     auto result = handlerConfigs_.insert(entry);
34     if (!result.second) {
35       result.first->second = entry.second;
36     }
37   }
38
39   // Update categoryConfigs_ with all of the entries from the other LogConfig.
40   //
41   // Any entries already present in our categoryConfigs_ are merged: if the new
42   // configuration does not include handler settings our entry's settings are
43   // maintained.
44   for (const auto& entry : other.categoryConfigs_) {
45     auto result = categoryConfigs_.insert(entry);
46     if (!result.second) {
47       auto* existingEntry = &result.first->second;
48       auto oldHandlers = std::move(existingEntry->handlers);
49       *existingEntry = entry.second;
50       if (!existingEntry->handlers.hasValue()) {
51         existingEntry->handlers = std::move(oldHandlers);
52       }
53     }
54   }
55 }
56
57 } // namespace folly