The Battle for Wesnoth  1.17.0-dev
options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 by Iris Morelle <shadowm@wesnoth.org>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 
17 #include "commandline_argv.hpp"
18 #include "formatter.hpp"
19 #include "log.hpp"
21 
22 #include <boost/program_options/cmdline.hpp>
23 #include <boost/program_options/parsers.hpp>
24 #include <boost/program_options/variables_map.hpp>
25 
26 namespace po = boost::program_options;
27 
28 namespace campaignd {
29 
30 command_line::command_line(int argc, char** argv)
31  : command_line(read_argv(argc, argv))
32 {
33 }
34 
35 command_line::command_line(const std::vector<std::string>& args)
36  : help(false)
37  , version(false)
38  , config_file()
39  , server_dir()
40  , port()
41  , show_log_domains(false)
43  , log_precise_timestamps(false)
44  , report_timings(false)
45  , argv0_(args.at(0))
46  , args_(args.begin() + 1, args.end())
47  , help_text_()
48 {
49  po::options_description opts_general{"General options"};
50  opts_general.add_options()
51  ("help,h", "prints this message and exits.")
52  ("version,v", "displays the server version and exits.")
53  ;
54 
55  po::options_description opts_server{"Server configuration"};
56  opts_server.add_options()
57  ("config,c", po::value<std::string>(), "specifies the path to the server configuration file. By default this is server.cfg in the server directory.")
58  ("server-dir,s", po::value<std::string>(), "specifies the path to the server directory. By default this is the process working directory.")
59  ("port,p", po::value<unsigned short>(), "specifies the port number on which the server will listen for client connections.")
60  ;
61 
62  po::options_description opts_log{"Logging options"};
63  opts_log.add_options()
64  ("logdomains", "lists defined log domains and exits")
65  ("log-error", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'error'. <arg> should be given as a comma-separated list of domains.")
66  ("log-warning", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'warning'. This is the default for all log domains other than 'campaignd' and 'server'.")
67  ("log-info", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'info'. This is the default for the 'campaignd' and 'server' log domains.")
68  ("log-debug", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'debug'.")
69  ("log-none", po::value<std::string>(), "disables logging for the specified log domain(s).")
70  ("log-precise", "shows the timestamps in log output with more precision.")
71  ("timings", "outputs timings for serviced requests to stderr.")
72  ;
73 
74  po::options_description opts;
75  opts.add(opts_general).add(opts_server).add(opts_log);
76 
77  static const int style = po::command_line_style::default_style ^ po::command_line_style::allow_guessing;
78 
79  po::variables_map vm;
80  po::store(po::command_line_parser(args_).options(opts).style(style).run(), vm);
81 
82  static const std::map<std::string, int> log_levels = {
83  { "error", lg::err().get_severity() },
84  { "warning", lg::warn().get_severity() },
85  { "info", lg::info().get_severity() },
86  { "debug", lg::debug().get_severity() },
87  { "none", -1 }
88  };
89 
90  if(vm.count("help")) {
91  if(!help) {
92  help_text_ = formatter() << "Usage: " << argv0_ << " [<options>]\n" << opts;
93  }
94 
95  help = true;
96  }
97  if(vm.count("version")) {
98  version = true;
99  }
100 
101  if(vm.count("config")) {
102  config_file = vm["config"].as<std::string>();
103  }
104  if(vm.count("server-dir")) {
105  server_dir = vm["server-dir"].as<std::string>();
106  }
107  if(vm.count("port")) {
108  port = vm["port"].as<unsigned short>();
109  }
110 
111  if(vm.count("logdomains")) {
112  show_log_domains = true;
113  }
114  for(const auto& lvl : log_levels) {
115  const auto& swtch = std::string{"log-"} + lvl.first;
116  const auto severity = lvl.second;
117  if(vm.count(swtch)) {
118  const auto& domains = utils::split(vm[swtch].as<std::string>());
119  for(const auto& d : domains) {
120  log_domain_levels[d] = severity;
121  }
122  }
123  }
124  if(vm.count("log-precise")) {
125  log_precise_timestamps = true;
126  }
127  if(vm.count("timings")) {
128  report_timings = true;
129  }
130 }
131 
132 } // end namespace campaignd
static domain_map * domains
Definition: log.cpp:71
logger & info()
Definition: log.cpp:88
bool report_timings
Whether to report timing information for server requests.
Definition: options.hpp:67
std::optional< std::string > server_dir
Path to the add-ons server storage dir.
Definition: options.hpp:56
std::string argv0_
Definition: options.hpp:70
#define d
std::vector< std::string > args_
Definition: options.hpp:71
const config & options()
Definition: game.cpp:568
static std::string at(const std::string &file, int line)
std::optional< std::string > config_file
Path to the add-ons server configuration file.
Definition: options.hpp:54
std::ostringstream wrapper.
Definition: formatter.hpp:38
std::optional< unsigned short > port
Port number on which the server will listen for incoming connections.
Definition: options.hpp:58
bool version
True if –version was passed.
Definition: options.hpp:51
logger & debug()
Definition: log.cpp:94
std::string help_text_
Definition: options.hpp:72
bool log_precise_timestamps
Whether to use higher precision for log timestamps.
Definition: options.hpp:65
logger & err()
Definition: log.cpp:76
command_line(int argc, char **argv)
Reads the command line.
Definition: options.cpp:30
std::vector< std::string > read_argv([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
logger & warn()
Definition: log.cpp:82
std::vector< std::string > split(const config_attribute_value &val)
campaignd command line options parsing.
Standard logging facilities (interface).
bool show_log_domains
True if –logdomains was passed.
Definition: options.hpp:61
std::map< std::string, int > log_domain_levels
Log domain/severity configuration.
Definition: options.hpp:63
Definition: help.cpp:56
int get_severity() const
Definition: log.hpp:145