2b924abe7cb3ebcf509a8c3cc6c6cee472c9afbf
[folly.git] / folly / experimental / logging / Init.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 /*
19  * This file contains function to help configure the logging library behavior
20  * during program start-up.
21  */
22
23 #include <stdexcept>
24 #include <string>
25 #include <vector>
26
27 #include <folly/Range.h>
28 #include <folly/experimental/logging/LogLevel.h>
29
30 namespace folly {
31
32 /**
33  * Configure log category levels based on a configuration string.
34  *
35  * This can be used to process a logging configuration string (such as received
36  * via a command line flag) during program start-up.
37  */
38 void initLogLevels(
39     folly::StringPiece configString = "",
40     LogLevel defaultRootLevel = LogLevel::WARNING);
41
42 /**
43  * Initialize the logging library to write glog-style messages to stderr.
44  *
45  * This initializes the log category levels as specified (using
46  * initLogLevels()), and adds a log handler that prints messages in glog-style
47  * format to stderr.
48  */
49 void initLoggingGlogStyle(
50     folly::StringPiece configString = "",
51     LogLevel defaultRootLevel = LogLevel::WARNING,
52     bool asyncWrites = true);
53
54 /**
55  * LoggingConfigError may be thrown by initLogLevels() if an error occurs
56  * parsing the configuration string.
57  */
58 class LoggingConfigError : public std::invalid_argument {
59  public:
60   explicit LoggingConfigError(const std::vector<std::string>& errors);
61
62  private:
63   std::string computeMessage(const std::vector<std::string>& errors);
64 };
65 } // namespace folly