LogConfig.cpp 2.6 KB

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