LogConfig.h 2.9 KB

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