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