2 * Copyright 2004-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
21 DEFINE_string(logging, "", "Logging category configuration string");
25 "Log handler style: async, immediate, or none");
30 "Crash with a message to this category instead of the default");
31 DEFINE_bool(crash, true, "Crash with a fatal log message.");
33 using folly::LogLevel;
37 * Helper class to optionally log a fatal message during static initialization
40 * Since command line arguments have not been processed during static
41 * initialization, we check an environment variable.
45 InitChecker() : value_{getenv("CRASH_DURING_INIT")} {
46 if (value_ && strcmp(value_, "shutdown") != 0) {
47 XLOG(FATAL) << "crashing during static initialization";
52 XLOG(FATAL) << "crashing during static destruction";
56 const char* value_{nullptr};
59 static InitChecker initChecker;
63 * This is a simple helper program to exercise the LOG(FATAL) functionality.
65 int main(int argc, char* argv[]) {
66 // Call folly::init() and then initialize log levels and handlers
67 folly::init(&argc, &argv);
69 if (FLAGS_handler_style == "async") {
70 initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO, true);
71 } else if (FLAGS_handler_style == "immediate") {
72 initLoggingGlogStyle(FLAGS_logging, LogLevel::INFO, false);
73 } else if (FLAGS_handler_style != "none") {
74 XLOGF(FATAL, "unknown log handler style \"{}\"", FLAGS_handler_style);
77 if (!FLAGS_category.empty()) {
78 folly::Logger logger{FLAGS_category};
79 FB_LOG(logger, FATAL, "crashing to category ", FLAGS_category);
86 XLOG(FATAL) << "test program crashing!";
87 // Even though main() is defined to return an integer, the compiler
88 // should be able to detect that XLOG(FATAL) never returns. It shouldn't
89 // complain that we don't return an integer here.