alaCarte Maps
Renderer for OpenStreetMap tiles
configuration.cpp
Go to the documentation of this file.
1 
22 #include <boost/program_options.hpp>
23 #include <boost/filesystem.hpp>
24 
25 #include "config.hpp"
27 
28 
45 Configuration::Configuration( boost::program_options::options_description& cmd_desc,
46  boost::program_options::options_description& config_desc,
47  boost::program_options::positional_options_description& pos_desc,
48  int argc, char** argv)
49  : ConfigFileUsed(false)
50 {
51  using boost::filesystem::path;
52  using boost::filesystem::is_regular_file;
53  using boost::program_options::store;
54  using boost::program_options::notify;
55  using boost::program_options::command_line_parser;
56  using boost::program_options::parse_config_file;
57  using boost::program_options::positional_options_description;
58 
59  store(command_line_parser(argc, argv).options(cmd_desc).positional(pos_desc).run(), options);
60 
61  std::queue<path> directories;
62  // Iterate over different directories ans search for config file
63  // Adding a Directory? So don't forget to update the help of -c command of the server and importer
64  directories.push(""); //root
65  directories.push(path(argv[0]).parent_path()); // executable path
66  directories.push(SYSCONFDIR);
67 
68  assert(options.count(opt::config));
69  path configFile = options[opt::config].as<string>();
70 
71  while(directories.size())
72  {
73  searchDirectories.push_back(directories.front().string());
74  path configPath = directories.front() / configFile;
75 
76  if(is_regular_file(configPath))
77  {
78  ConfigFileUsed = true;
79  store(parse_config_file<char>(configPath.string().c_str(), config_desc, true), options);
80  break;
81  }
82 
83  directories.pop();
84  }
85 
86  notify(options);
87 }
88 
95 bool Configuration::has(const string& key)
96 {
97  return options.count(key) != 0;
98 }
99 
100 
102 {
103  return ConfigFileUsed;
104 }
105 
106 const std::vector<string> & Configuration::getSearchDirectories() const
107 {
108  return searchDirectories;
109 }
115 {
116  for(auto it = options.begin();
117  it != options.end();
118  ++it)
119  {
120  boost::any& v = it->second.value();
121 
122 
123  if(v.type() == typeid(int))
124  {
125  LOG_SEV(app_log, info) << it->first << ": " << it->second.as<int>();
126  }else if (v.type() == typeid(std::string))
127  {
128  if (it->first == opt::config && ConfigFileUsed && !searchDirectories.back().empty()) {
129  LOG_SEV(app_log, info) << it->first << ": " << searchDirectories.back() << "/" << it->second.as<std::string>();
130  } else {
131  LOG_SEV(app_log, info) << it->first << ": " << it->second.as<std::string>();
132  }
133  }else if (v.type() == typeid(std::vector<string>))
134  {
135  std::stringstream ss;
136  for(string name : it->second.as<std::vector<string>>()) {
137  ss << name << ", ";
138  }
139  LOG_SEV(app_log, info) << it->first << ": " << ss.str();
140  }else{
141  // We only take care of int and string, so if this message is printed on the log
142  // feel free to add other types.
143  // boost::any does not support implicit casting!!!
144  LOG_SEV(app_log, info) << "Unknown type[" << v.type().name() << "]";
145  }
146  }
147 }
148 
149 
150 
151 const boost::any& Configuration::getValueByKey(const string& key) const
152 {
153  return options.at(key).value();
154 }
void printConfigToLog()
Prints the given configurations to the log.
std::string string
Definition: settings.hpp:110
#define LOG_SEV(log, lvl)
Definition: settings.hpp:78
boost::program_options::variables_map options
Internal map holding the options.
const std::vector< string > & getSearchDirectories() const
Returns a queue of strings.
TESTABLE const boost::any & getValueByKey(const string &key) const
bool usedConfigFile() const
Returns true if the configuration was loaded form a file.
Configuration()=default
bool has(const string &key)
Tests if a key was specified in the command line or in the config file.
static const char * config
Option to get the configuration filename (type: string)
std::vector< string > searchDirectories