31 #include <boost/tuple/tuple.hpp> 32 #include <boost/lexical_cast.hpp> 33 #include <boost/format.hpp> 34 #include <boost/filesystem/fstream.hpp> 36 #include "../logging/Logger.h" 37 #include "../logging/LoggerFactory.h" 62 = logging::LoggerFactory::getInstance().getLogger(
"rsc.config.configure()");
66 std::vector<std::string>
68 boost::shared_ptr<std::string> files;
72 std::vector<std::string> result;
73 result.push_back(CONFIG_FILE_KEY_PREFIX);
74 result.push_back(CONFIG_FILE_KEY_USER);
75 result.push_back(CONFIG_FILE_KEY_PWD);
80 std::pair<boost::filesystem::path, std::string>
82 const boost::filesystem::path& prefix,
83 const std::string& configFileName) {
84 if (spec == CONFIG_FILE_KEY_PREFIX) {
87 "Prefix wide config file");
88 }
else if (spec == CONFIG_FILE_KEY_USER) {
92 }
else if (spec == CONFIG_FILE_KEY_PWD) {
94 "Current directory file");
97 "User specified config file");
102 const boost::filesystem::path& path,
103 const istream& stream) {
104 cerr <<
" " << label <<
" " << path <<
" " 105 << (stream ?
"exists" :
"does not exist") << endl;
109 const std::string& spec,
110 const boost::filesystem::path& prefix,
111 const std::string& configFileName,
114 boost::filesystem::path file;
115 std::string description;
117 boost::tie(file, description)
119 }
catch (
const runtime_error& e) {
121 "Failed to resolve configuration file designated by " 122 << spec <<
"': " << e.what());
127 boost::filesystem::ifstream stream(file);
137 }
catch (
const runtime_error& e) {
139 "Failed to process " << description <<
" `" 140 << file <<
"': " << e.what());
145 const string& configFileName,
146 const string& environmentVariablePrefix,
149 bool stripEnvironmentVariablePrefix,
150 const boost::filesystem::path& prefix,
151 const std::string& debugVariableName,
152 const std::vector<std::string>& configurationFiles) {
157 cerr <<
"Configuring with sources (lowest priority first)" << endl;
163 cerr <<
" 1. Configuration files" << endl;
165 unsigned int index = 1;
166 for (std::vector<std::string>::const_iterator it = configurationFiles.begin();
167 it != configurationFiles.end(); ++it) {
174 stripEnvironmentVariablePrefix);
176 cerr <<
" 2. Environment variables with prefix " 177 << environmentVariablePrefix << endl;
179 if (!matches.empty()) {
180 for (EnvironmentVariableSource::Matches::const_iterator it
181 = matches.begin(); it != matches.end(); ++it) {
184 if (it->getRawName() != it->getTransformedName()) {
185 cerr <<
" (mapped to " 186 << it->getTransformedName()
189 cerr <<
" -> " << it->getValue()
193 cerr <<
" <none>" << endl;
202 cerr <<
" 3. Commandline options" << endl;
206 for (
int i = 0; i < argc; ++i) {
207 cerr <<
" " << argv[i] << endl;
214 cerr <<
" <none>" << endl;
void provideOptions(OptionHandler &handler)
Implementations should pass all configuration options to handler.
path userConfigDirectory()
Return the canonical directory for configuration files of the user associated with the current proces...
const std::string CONFIG_FILE_KEY_PWD
void describeFileStream(const std::string &label, const boost::filesystem::path &path, const istream &stream)
Matches getMatches()
Return matching environment variables.
Objects of this class analyze the environment of the current process, finding environment variables w...
Objects of this class parse streams that contain configuration information in "ini-file" syntax...
A source for options from the command line using a -D java properties-like syntax.
vector< string > splitSequenceValue(const string &value)
void processConfigFile(unsigned int index, const std::string &spec, const boost::filesystem::path &prefix, const std::string &configFileName, bool debug, OptionHandler &handler)
void provideOptions(OptionHandler &handler)
Implementations should pass all configuration options to handler.
const std::string DEFAULT_DEBUG_VARIABLE_NAME
Implementations of this interface receive options from ConfigSource objects.
std::vector< Match > Matches
pair< _T1, _T2 > make_pair(_T1 __x, _T2 __y)
A convenience wrapper for creating a pair from two objects.
std::pair< boost::filesystem::path, std::string > resolveConfigurationFile(const std::string &spec, const boost::filesystem::path &prefix, const std::string &configFileName)
Return a pair of configuration file path and description derived from spec.
boost::shared_ptr< std::string > getEnvironmentVariable(const std::string &name)
Return the value of the environment value name or an empty pointer.
path prefixConfigDirectory(const path &prefix)
std::vector< std::string > defaultConfigurationFiles(const std::string &fileVariableName)
#define RSCWARN(logger, msg)
const std::string DEFAULT_FILE_VARIABLE_NAME
const std::string CONFIG_FILE_KEY_PREFIX
Provides a hierarchical logging system with the possibility to install different backends, which are instances of LoggingSystem.
const std::string CONFIG_FILE_KEY_USER
void configure(OptionHandler &handler, const string &configFileName, const string &environmentVariablePrefix, int argc, const char **argv, bool stripEnvironmentVariablePrefix, const boost::filesystem::path &prefix, const std::string &debugVariableName, const std::vector< std::string > &configurationFiles)
Pass configuration options in from configuration files derived from configFileName and environment va...
void provideOptions(OptionHandler &handler)
Implementations should pass all configuration options to handler.
boost::shared_ptr< Logger > LoggerPtr