diff options
author | Andy Schwerin <schwerin@10gen.com> | 2012-09-17 12:19:26 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@10gen.com> | 2012-09-25 15:57:08 -0400 |
commit | 635aed09b0de72588422f1b864cca3810979375c (patch) | |
tree | 110365fc2105146c248025ef06e95db70ef3721b /src/mongo/base/initializer.cpp | |
parent | 1c9b2a7d236cce265db1b29a708959fe87ba56ca (diff) | |
download | mongo-635aed09b0de72588422f1b864cca3810979375c.tar.gz |
SERVER-5112 Introduce runGlobalInitializers(), macros and tools for global initializer declaration.
These utilities simplify adding global (process-wide) initializers that run at the beginning of
main().
Also, run the global initializers in the main() of unittests.
Diffstat (limited to 'src/mongo/base/initializer.cpp')
-rw-r--r-- | src/mongo/base/initializer.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/mongo/base/initializer.cpp b/src/mongo/base/initializer.cpp index f1921ba5637..4e1f36a83ce 100644 --- a/src/mongo/base/initializer.cpp +++ b/src/mongo/base/initializer.cpp @@ -15,6 +15,8 @@ #include "mongo/base/initializer.h" +#include "mongo/base/global_initializer.h" + namespace mongo { Initializer::Initializer() {} @@ -44,4 +46,40 @@ namespace mongo { return Status::OK(); } + Status runGlobalInitializers(const InitializerContext::ArgumentVector& args, + const InitializerContext::EnvironmentMap& env) { + + return getGlobalInitializer().execute(args, env); + } + + void runGlobalInitializersOrDie(const InitializerContext::ArgumentVector& args, + const InitializerContext::EnvironmentMap& env) { + + Status status = runGlobalInitializers(args, env); + if (Status::OK() != status) { + std::cerr << "Failed global initialization: " << status << std::endl; + ::_exit(1); + } + } + + void runGlobalInitializersOrDie(int argc, const char* const* argv, const char* const* envp) { + InitializerContext::ArgumentVector args(argc); + std::copy(argv, argv + argc, args.begin()); + + InitializerContext::EnvironmentMap env; + + if (envp) { + for(; *envp; ++envp) { + const char* firstEqualSign = strchr(*envp, '='); + if (!firstEqualSign) { + std::cerr << "Failed global initialization: malformed environment block\n"; + ::_exit(1); + } + env[std::string(*envp, firstEqualSign)] = std::string(firstEqualSign + 1); + } + } + + runGlobalInitializersOrDie(args, env); + } + } // namespace mongo |