e89ef2cdaf7f32b444ef6e4dc71051efa9f59dc9
[folly.git] / folly / experimental / logging / test / FatalHelper.cpp
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/experimental/logging/Init.h>
17 #include <folly/experimental/logging/xlog.h>
18 #include <folly/init/Init.h>
19
20 DEFINE_string(logging, "", "Logging category configuration string");
21 DEFINE_string(
22     handler_style,
23     "async",
24     "Log handler style: async, immediate, or none");
25
26 DEFINE_string(
27     category,
28     "",
29     "Crash with a message to this category instead of the default");
30
31 using folly::LogLevel;
32
33 /*
34  * This is a simple helper program to exercise the LOG(FATAL) functionality.
35  */
36 int main(int argc, char* argv[]) {
37   // Call folly::init() and then initialize log levels and handlers
38   folly::init(&argc, &argv);
39
40   if (FLAGS_handler_style == "async") {
41     initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO, true);
42   } else if (FLAGS_handler_style == "immediate") {
43     initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO, false);
44   } else if (FLAGS_handler_style != "none") {
45     XLOGF(FATAL, "unknown log handler style \"{}\"", FLAGS_handler_style);
46   }
47
48   if (!FLAGS_category.empty()) {
49     folly::Logger logger{FLAGS_category};
50     FB_LOG(logger, FATAL, "crashing to category ", FLAGS_category);
51   }
52
53   XLOG(FATAL) << "test program crashing!";
54   // Even though main() is defined to return an integer, the compiler
55   // should be able to detect that XLOG(FATAL) never returns.  It shouldn't
56   // complain that we don't return an integer here.
57 }