33c7bb03b38c16d7a94acb4992f319ffbcd5fb2c
[folly.git] / folly / init / Init.cpp
1 /*
2  * Copyright 2017 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
17 #include <folly/init/Init.h>
18
19 #include <glog/logging.h>
20
21 #include <folly/Singleton.h>
22
23 #ifdef FOLLY_USE_SYMBOLIZER
24 #include <folly/experimental/symbolizer/SignalHandler.h>
25 #endif
26 #include <folly/portability/GFlags.h>
27
28 namespace folly {
29
30 void init(int* argc, char*** argv, bool removeFlags) {
31 #ifdef FOLLY_USE_SYMBOLIZER
32   // Install the handler now, to trap errors received during startup.
33   // The callbacks, if any, can be installed later
34   folly::symbolizer::installFatalSignalHandler();
35 #elif !defined(_WIN32)
36   google::InstallFailureSignalHandler();
37 #endif
38
39   gflags::ParseCommandLineFlags(argc, argv, removeFlags);
40
41   auto programName = argc && argv && *argc > 0 ? (*argv)[0] : "unknown";
42   google::InitGoogleLogging(programName);
43
44 #ifdef FOLLY_USE_SYMBOLIZER
45   // Don't use glog's DumpStackTraceAndExit; rely on our signal handler.
46   google::InstallFailureFunction(abort);
47 #endif
48
49   // Move from the registration phase to the "you can actually instantiate
50   // things now" phase.
51   folly::SingletonVault::singleton()->registrationComplete();
52
53 #ifdef FOLLY_USE_SYMBOLIZER
54   // Actually install the callbacks into the handler.
55   folly::symbolizer::installFatalSignalCallbacks();
56 #endif
57 }
58 } //!folly