Consistently have the namespace closing comment
[folly.git] / folly / experimental / logging / test / FatalHelper.cpp
index e89ef2cdaf7f32b444ef6e4dc71051efa9f59dc9..3d65511f4e64dcb1d54300de455bdfeae66ee80f 100644 (file)
@@ -16,6 +16,7 @@
 #include <folly/experimental/logging/Init.h>
 #include <folly/experimental/logging/xlog.h>
 #include <folly/init/Init.h>
+#include <folly/portability/Stdlib.h>
 
 DEFINE_string(logging, "", "Logging category configuration string");
 DEFINE_string(
@@ -27,16 +28,39 @@ DEFINE_string(
     category,
     "",
     "Crash with a message to this category instead of the default");
+DEFINE_bool(crash, true, "Crash with a fatal log message.");
 
 using folly::LogLevel;
 
-/*
- * This is a simple helper program to exercise the LOG(FATAL) functionality.
+namespace {
+/**
+ * Helper class to optionally log a fatal message during static initialization
+ * or destruction.
+ *
+ * Since command line arguments have not been processed during static
+ * initialization, we check an environment variable.
  */
-int main(int argc, char* argv[]) {
-  // Call folly::init() and then initialize log levels and handlers
-  folly::init(&argc, &argv);
+class InitChecker {
+ public:
+  InitChecker() : value_{getenv("CRASH_DURING_INIT")} {
+    if (value_ && strcmp(value_, "shutdown") != 0) {
+      XLOG(FATAL) << "crashing during static initialization";
+    }
+  }
+  ~InitChecker() {
+    if (value_) {
+      XLOG(FATAL) << "crashing during static destruction";
+    }
+  }
+
+  const char* value_{nullptr};
+};
 
+static InitChecker initChecker;
+} // namespace
+
+namespace {
+int runHelper() {
   if (FLAGS_handler_style == "async") {
     initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO, true);
   } else if (FLAGS_handler_style == "immediate") {
@@ -50,8 +74,36 @@ int main(int argc, char* argv[]) {
     FB_LOG(logger, FATAL, "crashing to category ", FLAGS_category);
   }
 
+  if (!FLAGS_crash) {
+    return 0;
+  }
+
   XLOG(FATAL) << "test program crashing!";
-  // Even though main() is defined to return an integer, the compiler
+  // Even though this function is defined to return an integer, the compiler
   // should be able to detect that XLOG(FATAL) never returns.  It shouldn't
   // complain that we don't return an integer here.
 }
+} // namespace
+
+std::string fbLogFatalCheck() {
+  folly::Logger logger("some.category");
+  FB_LOG(logger, FATAL) << "we always crash";
+  // This function mostly exists to make sure the compiler does not warn
+  // about a missing return statement here.
+}
+
+/*
+ * This is a simple helper program to exercise the LOG(FATAL) functionality.
+ */
+int main(int argc, char* argv[]) {
+  // Call folly::init() and then initialize log levels and handlers
+  folly::init(&argc, &argv);
+
+  // Do most of the work in a separate helper function.
+  //
+  // The main reason for putting this in a helper function is to ensure that
+  // the compiler does not warn about missing return statements on XLOG(FATAL)
+  // code paths.  Unfortunately it appears like some compilers always suppress
+  // this warning for main().
+  return runHelper();
+}