alaCarte Maps
Renderer for OpenStreetMap tiles
applications.cpp
Go to the documentation of this file.
1 
22 #include <boost/filesystem.hpp>
23 
24 #include <boost/log/utility/setup/file.hpp>
25 #include <boost/log/utility/setup/console.hpp>
26 #include <boost/log/utility/setup/common_attributes.hpp>
27 #include <boost/log/utility/setup/formatter_parser.hpp>
28 
29 #include "utils/application.hpp"
31 
32 using boost::make_shared;
33 
39 {
40 
41 }
42 
50 int Application::start(int argc, char** argv)
51 {
52  // Use ALACARTE_NO_BACKBONE to prevent alacarte from catching every exception
53 #ifdef ALACARTE_NO_BACKBONE
54  appRun(argc, argv);
55 #else
56  try
57  {
58  appRun(argc, argv);
59  }
60  catch (...)
61  {
62  // We don't even have the slightest idea, what might have happened
63  std::string info = boost::current_exception_diagnostic_information();
64 
65  BOOST_LOG_TRIVIAL(fatal) << "Boost diagnostic:\n" << info;
66  return 1;
67  }
68 #endif
69 
70  return 0;
71 }
72 
79 void Application::appRun(int argc, char** argv)
80 {
81  shared_ptr<Configuration> config;
82 
83  // create and parse config
84  try{
85  config = make_shared<Configuration>(cmd_desc, config_desc, pos_desc, argc, argv);
86  } catch(boost::program_options::error& e)
87  {
88  std::cout << "Error: " << e.what() << std::endl;
89 
90  std::cout << cmd_desc << std::endl;
91 
92  return;
93  }
94 
95  // handle help
96  if(config->has(opt::help))
97  {
98  std::cout << cmd_desc << std::endl;
99  return;
100  }
101 
102 
103  initLog(config);
104 
105  config->printConfigToLog();
106 
107 
108  if (config->get<string>(opt::config) != DEFAULT_CONFIG_NAME && !config->usedConfigFile())
109  {
110  const std::vector<string> &dirs = config->getSearchDirectories();
111  LOG_SEV(app_log, error) << "The given config file was not found. Searched for:";
112 
113  std::for_each(begin(dirs), end(dirs), [&](const string &dir)
114  {
115  LOG_SEV(app_log, error) << opt::config << " = \"" << dir << "/" << config->get<string>(opt::config) << "\"";
116  });
117  return;
118  }
119 
120  if(!startupDiagnostic(config)) return;
121  // now do what you have to do!
122  onRun(config);
123 }
124 
130 void Application::initLog(const shared_ptr<Configuration>& config)
131 {
132  fileLogger = logging::add_file_log(
133  keywords::file_name = config->get<string>(opt::logfile),
134  keywords::rotation_size = 10 * 1024 * 1024,
135  keywords::time_based_rotation = logging::sinks::file::rotation_at_time_point(0, 0, 0),
136  keywords::format = "<%TimeStamp%>: [%Channel%] %Severity%: %Message%"
137  );
138 
139  consoleLogger = logging::add_console_log(std::clog,
140  keywords::format = "[%Channel%] %Severity%: %Message%"
141  );
142 
143  logging::add_common_attributes();
144 
145  logging::core::get()->set_filter
146  (
147  logging::trivial::severity >= logging::trivial::info
148  );
149 
150  customInitLog(config);
151 }
152 
153 bool Application::diagnosticCheckFile(const shared_ptr<Configuration>& config, const string& key)
154 {
155  if (config->has(key)) {
156  boost::filesystem::path file = config->get<string>(key);
157  bool exists = boost::filesystem::exists(file);
158  if (!exists) {
159  LOG_SEV(app_log, error) << key << " = \"" << file.string() << "\" does not exist.";
160  }
161  return exists;
162  } else {
163  LOG_SEV(app_log, error) << key << " is not set.";
164  return false;
165  }
166 }
boost::program_options::positional_options_description pos_desc
Definition: application.hpp:53
virtual bool startupDiagnostic(const shared_ptr< Configuration > &config)=0
virtual void onRun(const shared_ptr< Configuration > &config)=0
shared_ptr< logging::sinks::synchronous_sink< logging::sinks::basic_text_ostream_backend< char > > > consoleLogger
Definition: application.hpp:58
Application()
Inits the new Application.
shared_ptr< logging::sinks::synchronous_sink< logging::sinks::text_file_backend > > fileLogger
Definition: application.hpp:57
std::string string
Definition: settings.hpp:110
#define LOG_SEV(log, lvl)
Definition: settings.hpp:78
virtual void customInitLog(const shared_ptr< Configuration > &config)
Definition: application.hpp:48
bool diagnosticCheckFile(const shared_ptr< Configuration > &config, const string &key)
boost::program_options::options_description config_desc
Definition: application.hpp:52
void appRun(int argc, char **argv)
Creates the Configuration and runs the application.
#define DEFAULT_CONFIG_NAME
Definition: settings.hpp:127
boost::program_options::options_description cmd_desc
Definition: application.hpp:51
static const char * config
Option to get the configuration filename (type: string)
static const char * help
Use Configuration::has to check weather the user wanted help.
int start(int argc, char **argv)
Starts the aplication with or without Exceptionhandling.
void initLog(const shared_ptr< Configuration > &config)
Inits the logfile.
static const char * logfile
Option to get the filename of the log (type: string)