Consistently have the namespace closing comment
[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 #include <folly/portability/Stdlib.h>
20
21 DEFINE_string(logging, "", "Logging category configuration string");
22 DEFINE_string(
23     handler_style,
24     "async",
25     "Log handler style: async, immediate, or none");
26
27 DEFINE_string(
28     category,
29     "",
30     "Crash with a message to this category instead of the default");
31 DEFINE_bool(crash, true, "Crash with a fatal log message.");
32
33 using folly::LogLevel;
34
35 namespace {
36 /**
37  * Helper class to optionally log a fatal message during static initialization
38  * or destruction.
39  *
40  * Since command line arguments have not been processed during static
41  * initialization, we check an environment variable.
42  */
43 class InitChecker {
44  public:
45   InitChecker() : value_{getenv("CRASH_DURING_INIT")} {
46     if (value_ && strcmp(value_, "shutdown") != 0) {
47       XLOG(FATAL) << "crashing during static initialization";
48     }
49   }
50   ~InitChecker() {
51     if (value_) {
52       XLOG(FATAL) << "crashing during static destruction";
53     }
54   }
55
56   const char* value_{nullptr};
57 };
58
59 static InitChecker initChecker;
60 } // namespace
61
62 namespace {
63 int runHelper() {
64   if (FLAGS_handler_style == "async") {
65     initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO, true);
66   } else if (FLAGS_handler_style == "immediate") {
67     initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO, false);
68   } else if (FLAGS_handler_style != "none") {
69     XLOGF(FATAL, "unknown log handler style \"{}\"", FLAGS_handler_style);
70   }
71
72   if (!FLAGS_category.empty()) {
73     folly::Logger logger{FLAGS_category};
74     FB_LOG(logger, FATAL, "crashing to category ", FLAGS_category);
75   }
76
77   if (!FLAGS_crash) {
78     return 0;
79   }
80
81   XLOG(FATAL) << "test program crashing!";
82   // Even though this function is defined to return an integer, the compiler
83   // should be able to detect that XLOG(FATAL) never returns.  It shouldn't
84   // complain that we don't return an integer here.
85 }
86 } // namespace
87
88 std::string fbLogFatalCheck() {
89   folly::Logger logger("some.category");
90   FB_LOG(logger, FATAL) << "we always crash";
91   // This function mostly exists to make sure the compiler does not warn
92   // about a missing return statement here.
93 }
94
95 /*
96  * This is a simple helper program to exercise the LOG(FATAL) functionality.
97  */
98 int main(int argc, char* argv[]) {
99   // Call folly::init() and then initialize log levels and handlers
100   folly::init(&argc, &argv);
101
102   // Do most of the work in a separate helper function.
103   //
104   // The main reason for putting this in a helper function is to ensure that
105   // the compiler does not warn about missing return statements on XLOG(FATAL)
106   // code paths.  Unfortunately it appears like some compilers always suppress
107   // this warning for main().
108   return runHelper();
109 }