2 * Copyright 2017-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/experimental/logging/LogConfig.h>
18 #include <folly/Conv.h>
22 bool LogConfig::operator==(const LogConfig& other) const {
23 return handlerConfigs_ == other.handlerConfigs_ &&
24 categoryConfigs_ == other.categoryConfigs_;
27 bool LogConfig::operator!=(const LogConfig& other) const {
28 return !(*this == other);
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);
40 result.first->second = entry.second;
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 \"",
52 iter->second.update(entry.second);
56 // Update categoryConfigs_ with all of the entries from the other LogConfig.
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
61 for (const auto& entry : other.categoryConfigs_) {
62 auto result = categoryConfigs_.insert(entry);
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);