ConfigHelpers.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/logging/test/ConfigHelpers.h>
  17. #include <ostream>
  18. #include <folly/String.h>
  19. #include <folly/logging/LogConfig.h>
  20. #include <folly/logging/LogConfigParser.h>
  21. #include <folly/logging/LogHandler.h>
  22. namespace folly {
  23. std::ostream& operator<<(std::ostream& os, const LogConfig& config) {
  24. // We could just use folly::toPrettyJson(logConfigToDynamic(config))
  25. // However, the format here is much more compact and easier to read if there
  26. // are discrepancies between configs in a test check.
  27. // Sort the categories by name before printing
  28. os << "{\n categories: {\n";
  29. std::vector<std::string> names;
  30. const auto& catConfigs = config.getCategoryConfigs();
  31. for (const auto& cc : catConfigs) {
  32. names.push_back(cc.first);
  33. }
  34. std::sort(names.begin(), names.end());
  35. for (const auto& name : names) {
  36. os << " " << name << "=" << catConfigs.at(name) << "\n";
  37. }
  38. // Sort the handlers by name before printing
  39. os << " }\n handlers: {\n";
  40. const auto& handlerConfigs = config.getHandlerConfigs();
  41. names.clear();
  42. for (const auto& cc : handlerConfigs) {
  43. names.push_back(cc.first);
  44. }
  45. std::sort(names.begin(), names.end());
  46. for (const auto& name : names) {
  47. os << " " << name << "=" << handlerConfigs.at(name) << "\n";
  48. }
  49. os << " }\n}";
  50. return os;
  51. }
  52. std::ostream& operator<<(std::ostream& os, const LogCategoryConfig& config) {
  53. // Rather than printing the JSON configuration, we print a shorter
  54. // representation closer to the basic config string format.
  55. os << logLevelToString(config.level);
  56. if (!config.inheritParentLevel) {
  57. os << "!";
  58. }
  59. if (config.handlers.hasValue()) {
  60. os << ":" << join(",", config.handlers.value());
  61. }
  62. return os;
  63. }
  64. std::ostream& operator<<(std::ostream& os, const LogHandlerConfig& config) {
  65. // Rather than printing the JSON configuration, we print a shorter
  66. // representation closer to the basic config string format.
  67. os << (config.type ? config.type.value() : "[no type]");
  68. bool first = true;
  69. for (const auto& opt : config.options) {
  70. if (!first) {
  71. os << ",";
  72. } else {
  73. os << ":";
  74. first = false;
  75. }
  76. os << opt.first << "=" << opt.second;
  77. }
  78. return os;
  79. }
  80. void PrintTo(const std::shared_ptr<LogHandler>& handler, std::ostream* os) {
  81. *os << "Handler(" << handler->getConfig() << ")";
  82. }
  83. } // namespace folly