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