Adds writer test case for RCU
[folly.git] / folly / experimental / logging / LogConfig.cpp
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 #include <folly/experimental/logging/LogConfig.h>
17
18 #include <folly/Conv.h>
19
20 namespace folly {
21
22 bool LogConfig::operator==(const LogConfig& other) const {
23   return handlerConfigs_ == other.handlerConfigs_ &&
24       categoryConfigs_ == other.categoryConfigs_;
25 }
26
27 bool LogConfig::operator!=(const LogConfig& other) const {
28   return !(*this == other);
29 }
30
31 void LogConfig::update(const LogConfig& other) {
32   // Update handlerConfigs_ with all of the entries from the other LogConfig.
33   // Any entries already present in our handlerConfigs_ are replaced wholesale.
34   for (const auto& entry : other.handlerConfigs_) {
35     if (entry.second.type.hasValue()) {
36       // This is a complete LogHandlerConfig that should be inserted
37       // or completely replace an existing handler config with this name.
38       auto result = handlerConfigs_.insert(entry);
39       if (!result.second) {
40         result.first->second = entry.second;
41       }
42     } else {
43       // This config is updating an existing LogHandlerConfig rather than
44       // completely replacing it.
45       auto iter = handlerConfigs_.find(entry.first);
46       if (iter == handlerConfigs_.end()) {
47         throw std::invalid_argument(to<std::string>(
48             "cannot update configuration for unknown log handler \"",
49             entry.first,
50             "\""));
51       }
52       iter->second.update(entry.second);
53     }
54   }
55
56   // Update categoryConfigs_ with all of the entries from the other LogConfig.
57   //
58   // Any entries already present in our categoryConfigs_ are merged: if the new
59   // configuration does not include handler settings our entry's settings are
60   // maintained.
61   for (const auto& entry : other.categoryConfigs_) {
62     auto result = categoryConfigs_.insert(entry);
63     if (!result.second) {
64       auto* existingEntry = &result.first->second;
65       auto oldHandlers = std::move(existingEntry->handlers);
66       *existingEntry = entry.second;
67       if (!existingEntry->handlers.hasValue()) {
68         existingEntry->handlers = std::move(oldHandlers);
69       }
70     }
71   }
72 }
73
74 } // namespace folly