LogConfigParser.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <stdexcept>
  18. #include <folly/CPortability.h>
  19. #include <folly/Range.h>
  20. #include <folly/logging/LogConfig.h>
  21. /*
  22. * This file contains utility functions for parsing and serializing
  23. * LogConfig strings.
  24. *
  25. * This is separate from the LogConfig class itself, to reduce the dependencies
  26. * of the core logging library. Other code that wants to use the logging
  27. * library to log messages but does not need to parse log config strings
  28. * therefore does not need to depend on the folly JSON library.
  29. */
  30. namespace folly {
  31. struct dynamic;
  32. class FOLLY_EXPORT LogConfigParseError : public std::invalid_argument {
  33. public:
  34. using std::invalid_argument::invalid_argument;
  35. };
  36. /**
  37. * Parse a log configuration string.
  38. *
  39. * See the documentation in logging/docs/Config.md for a description of the
  40. * configuration string syntax.
  41. *
  42. * Throws a LogConfigParseError on error.
  43. */
  44. LogConfig parseLogConfig(StringPiece value);
  45. /**
  46. * Parse a JSON configuration string.
  47. *
  48. * See the documentation in logging/docs/Config.md for a description of the
  49. * JSON configuration object format.
  50. *
  51. * This function uses relaxed JSON parsing, allowing C and C++ style
  52. * comments, as well as trailing commas.
  53. */
  54. LogConfig parseLogConfigJson(StringPiece value);
  55. /**
  56. * Parse a folly::dynamic object.
  57. *
  58. * The input should be an object data type, and is parsed the same as a JSON
  59. * object accpted by parseLogConfigJson().
  60. */
  61. LogConfig parseLogConfigDynamic(const dynamic& value);
  62. /**
  63. * Convert a LogConfig object to a folly::dynamic object.
  64. *
  65. * This can be used to serialize it as a JSON string, which can later be read
  66. * back using parseLogConfigJson().
  67. */
  68. dynamic logConfigToDynamic(const LogConfig& config);
  69. dynamic logConfigToDynamic(const LogHandlerConfig& config);
  70. dynamic logConfigToDynamic(const LogCategoryConfig& config);
  71. } // namespace folly