proxygen
FatalHelper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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/init/Init.h>
17 #include <folly/logging/xlog.h>
19 #include <iostream>
20 
22  category,
23  "",
24  "Crash with a message to this category instead of the default");
25 DEFINE_bool(crash, true, "Crash with a fatal log message.");
27  check_debug,
28  false,
29  "Print whether this binary was built in debug mode "
30  "and then exit successfully");
31 
32 DEFINE_bool(fail_fatal_xlog_if, false, "Fail an XLOG_IF(FATAL) check.");
33 DEFINE_bool(fail_dfatal_xlog_if, false, "Fail an XLOG_IF(DFATAL) check.");
34 DEFINE_bool(fail_xcheck, false, "Fail an XCHECK() test.");
36  fail_xcheck_nomsg,
37  false,
38  "Fail an XCHECK() test with no additional message.");
39 DEFINE_bool(fail_xdcheck, false, "Fail an XDCHECK() test.");
40 
41 using folly::LogLevel;
42 
43 namespace {
51 class InitChecker {
52  public:
53  InitChecker() : value_{getenv("CRASH_DURING_INIT")} {
54  if (value_ && strcmp(value_, "shutdown") != 0) {
55  XLOG(FATAL) << "crashing during static initialization";
56  }
57  }
58  ~InitChecker() {
59  if (value_) {
60  XLOG(FATAL) << "crashing during static destruction";
61  }
62  }
63 
64  const char* value_{nullptr};
65 };
66 
67 static InitChecker initChecker;
68 } // namespace
69 
70 namespace {
71 int runHelper() {
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 
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  auto init = folly::Init(&argc, &argv);
100 
101  if (FLAGS_check_debug) {
102  std::cout << "DEBUG=" << static_cast<int>(folly::kIsDebug) << "\n";
103  return 0;
104  }
105 
106  XLOG_IF(FATAL, FLAGS_fail_fatal_xlog_if) << "--fail_fatal_xlog_if specified!";
107  XLOG_IF(DFATAL, FLAGS_fail_dfatal_xlog_if)
108  << "--fail_dfatal_xlog_if specified!";
109  XCHECK(!FLAGS_fail_xcheck) << ": --fail_xcheck specified!";
110  XCHECK(!FLAGS_fail_xcheck_nomsg);
111  XDCHECK(!FLAGS_fail_xdcheck) << ": --fail_xdcheck specified!";
112 
113  // Do most of the work in a separate helper function.
114  //
115  // The main reason for putting this in a helper function is to ensure that
116  // the compiler does not warn about missing return statements on XLOG(FATAL)
117  // code paths. Unfortunately it appears like some compilers always suppress
118  // this warning for main().
119  return runHelper();
120 }
constexpr auto kIsDebug
Definition: Portability.h:264
DEFINE_bool(crash, true,"Crash with a fatal log message.")
int main(int argc, char *argv[])
Definition: FatalHelper.cpp:98
#define FB_LOG(logger, level,...)
Definition: Logger.h:34
DEFINE_string(category,"","Crash with a message to this category instead of the default")
void init()
char ** argv
#define XCHECK(cond,...)
Definition: xlog.h:332
std::string fbLogFatalCheck()
Definition: FatalHelper.cpp:88
#define XLOG_IF(level, cond,...)
Definition: xlog.h:68
LogLevel
Definition: LogLevel.h:38
const char * string
Definition: Conv.cpp:212
#define XDCHECK(cond,...)
Definition: xlog.h:346
#define XLOG(level,...)
Definition: xlog.h:57