diff options
Diffstat (limited to 'project2/common/environment.cpp')
-rw-r--r-- | project2/common/environment.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp new file mode 100644 index 0000000..0c563cc --- /dev/null +++ b/project2/common/environment.cpp @@ -0,0 +1,121 @@ +#include "environment.h" +#include "loggers.h" +#include "xmlObjectLoader.h" +#include <stdio.h> +#include <fstream> +#include <boost/filesystem/convenience.hpp> +#include <boost/foreach.hpp> +#include <boost/bind.hpp> + +namespace po = boost::program_options; + +const Environment * Environment::currentEnv(NULL); +int Environment::clLevel(-1); +int Environment::slLevel(-1); +bool Environment::optionsBuilt(false); +po::options_description Environment::allOptions("Project2 options"); +po::positional_options_description Environment::posOptions; + +Environment::Environment(int c, char ** v) : + argc(c), + argv(v) +{ + currentEnv = this; +} + +static +po::variables_map +injectSettingsHelper(int argc, char ** argv, const po::options_description * opts, po::positional_options_description * posOpts) +{ + po::variables_map settings; + if (!opts) { + return settings; + } + if (argc > 0 && argv != NULL) { + if (posOpts) { + po::store(po::command_line_parser(argc, argv).options(*opts).positional(*posOpts).allow_unregistered().run(), settings); + } + else { + po::store(po::command_line_parser(argc, argv).options(*opts).allow_unregistered().run(), settings); + } + } + po::store(po::parse_environment(*opts, "P2_"), settings); + if (boost::filesystem::exists(".p2config")) { + std::ifstream f(".p2config"); + po::store(po::parse_config_file(f, *opts, true), settings); + } + po::notify(settings); + return settings; +} + +void +Environment::init() +{ + if (!optionsBuilt) { + po::options_description common("Project2 Common options"); + common.add_options() + ("sysloglevel,s", po::value(&slLevel)->default_value(-1), + "Log to syslog with level <arg> (default OFF)") + ("consoleloglevel,c", po::value(&clLevel)->default_value(LOG_WARNING), + "Log to console with level <arg> (default WARNING)") + ("datasourceroot", boost::program_options::value(&datasourceRoot)->default_value("datasources"), + "The folder in which to find datasource definitions") + ("xmlnamespace", boost::program_options::value(&xmlNamespace)->default_value("http://project2.randomdan.homeip.net"), + "The XML namespace to use for Project2 components and responses") + ("xmlprefix", boost::program_options::value(&xmlPrefix)->default_value("project2"), + "The XML namespace prefix to use for the Project2 XML namespace") + ("sessionTimeOut", boost::program_options::value(&sessionTimeOut)->default_value(3600), + "The time after which idle sessions are forgetten") + ; + allOptions.add(common).add(addOptions(posOptions)); + optionsBuilt = true; + } + po::variables_map settings(injectSettingsHelper(argc, argv, &allOptions, &posOptions)); + postinit(allOptions, settings); + LoaderBase::onAllComponents(boost::bind( + injectSettingsHelper, argc, argv, boost::bind(&ComponentLoader::options, _1), (po::positional_options_description*)NULL)); + + Logger()->clear(); + if (clLevel >= 0) { + Logger()->addLogger(new ConsoleLogDriver(stderr, clLevel, false)); + } + if (slLevel >= 0) { + Logger()->addLogger(new SyslogLogDriver(getScriptName().c_str(), slLevel)); + } +} + +Environment::~Environment() +{ + currentEnv = NULL; +} + +const Environment * +Environment::getCurrent() +{ + return currentEnv; +} + +boost::filesystem::path +Environment::resolveScript(const std::string & group, const std::string & name) const +{ + boost::filesystem::path script(boost::filesystem::current_path() / group); + BOOST_FOREACH(const boost::filesystem::path & e, boost::filesystem::path(name)) { + if (boost::filesystem::is_directory(script / e)) { + script /= e; + } + else { + if (boost::filesystem::is_regular_file((script / e).replace_extension(".xml"))) { + return ((script / e).replace_extension(".xml")); + } + } + } + return script; +} + +boost::filesystem::path +Environment::resolveScript(const std::string & path) const +{ + boost::filesystem::path script(boost::filesystem::current_path()); + return script / path; +} + |