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